A Circular linked list in C programming performs a reverse traversal with less memory usage than a doubly linked list. We require extra memory for the previous pointer in a doubly linked list.Â

When we compare the circular linked list with both singly linked list and doubly linked list, we don't find null in any of the nodes; instead, the last node's address stores the address of the first node. So basically, the tail node reconnects to the head node, making it a circular linked list.

#### Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN Stack

## What Is a Circular Linked List in C?

A Circular singly linked list is a collection of data called nodes, where the last node links to the first node. The traversal of a circular singly linked list is until we reach the same node we started in a list.

Now we know what a circular linked list is, letâ€™s look at the memory representation with the help of a diagram.Â

## Memory Representation of a Circular Linked List

Let's consider the below-given diagram and understand the memory representation of a circular linked list.

So first, Let's consider four elements to insert into the list. For which creat four nodes, where each node consists of a data and address part stored at some random address. In the singly linked list, the last node next pointer points to Null. But When we compare a circular singly linked list with the singly linked list, we don't find a null.

In a circular singly linked list, the last node's next pointer holds the address of the first node. In other words, we can say that â€śThe Tail node's address is pointing toward the Head node of the linked list.â€ťÂ

Every node in a linked list connects with the other through a pointer that points to the address of the next node, and the arrows in this diagram represent that.

### For Example:

Let our elements to insert be 10, 20, 30, 40.

• As you can notice from the above diagram, The last node's next pointer is pointing to the address of the first node, address1.
• The next part of the first node holds the address of the next node, address2.
• Similarly, the second node has the address of the third node, address 3.
• Following, the third node holds the address of its next node. And the last node contains the address of the first node. In this way, they have linked together.Â Â Â Â Â Â Â
• A reverse traversal is possible with less memory usage in a circular linked list than in a doubly linked list.
• The traversal is until we reach the same node we started from the list.

Moving ahead, Letâ€™s look at the general syntax of circular singly linked list in c.

#### Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN Stack

## Syntax of Circular Linked List

struct node

{

Â Â int data;

Â Â struct node* next;

};

Let's look at the simple, circular linked list program.

#include <stdio.h>

#include <stdlib.h>

void display();

struct Node {

int data;

struct Node* next;

};

int main()

{

struct Node* last;

struct Node* first;

struct Node* second;

struct Node* third;

struct Node* fourth;

first = (struct Node*)malloc(sizeof(struct Node));

second = (struct Node*)malloc(sizeof(struct Node));

third = (struct Node*)malloc(sizeof(struct Node));

fourth = (struct Node*)malloc(sizeof(struct Node));

first->data = 10;Â

second->data = 20;

third->data = 30;

fourth->data = 40;

Â Â Â Â Â Â Â Â Â first->next = second;

Â Â Â Â Â Â Â Â Â second->next = third;

third->next = fourth;

fourth->next = first;

last = fourth;

display(last);

return 0;

}

void display(struct Node* last_node) {

Â Â struct Node* ptr;

Â Â if (last_node == NULL) {

Â Â printf("The list is empty");

Â Â return;

Â Â }

Â Â ptr = last_node->next;

Â Â do {

Â Â printf("%d ", ptr->data);

Â Â ptr = ptr->next;

Â Â } while (ptr != last_node->next);

}

#### Output:

Now, you will go through the operations performed on a circular linked list.

## Operations on Circular Linked List

1. Insertion operation
2. Deletion operation
3. Traversal operation

#### Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN Stack

### Insertion Operation

#### DescriptionÂ

Insertion at beginning

Inserts the element at the beginning of the linked list

Insertion at end

Insert the element at the end of the list

Insertion after certain node

Insert element after specific element

Let's understand it practically.

#include <stdio.h>

#include <stdlib.h>

struct Node {

Â Â int data;

Â Â struct Node* next;

};

struct Node* insertToEmptylist(struct Node* head, int data) {

Â Â if (head != NULL)

Â Â struct Node* new = (struct Node*)malloc(sizeof(struct Node));Â Â

Â Â new->data = data;

}

struct Node* insertAtbegin(struct Node* head, int data) {

Â Â if (head == NULL)

Â Â struct Node* new = (struct Node*)malloc(sizeof(struct Node));Â

Â Â new->data = data;

}

struct Node* insertAtend(struct Node* last, int data) {

Â Â if (last == NULL)Â

Â Â return insertToEmptylist(last, data);

Â Â struct Node* new = (struct Node*)malloc(sizeof(struct Node));

Â Â new->data = data;

Â Â new->next = last->next;

Â Â last->next = new;

Â Â last = new;

Â Â return last;

}

struct Node* insertionAfternode(struct Node* last, int data, int ele) {

Â Â if (last == NULL)

Â Â return NULL;

Â Â struct Node *new, *ptr;

Â Â ptr = last->next;

Â Â do {

Â Â if (ptr->data == ele) {

Â Â Â Â new = (struct Node*)malloc(sizeof(struct Node));

Â Â Â Â new->data = data;

Â Â Â Â new->next = ptr->next;

Â Â Â Â ptr->next = new;

Â Â Â Â if (ptr == last) last = new;

Â Â Â Â return last;

Â Â }

Â Â ptr = ptr->next;

Â Â } while (ptr != last->next);

Â Â printf("\nThe node is not present in the linked list");

Â Â return last;

}

Â Â struct Node* ptr;

Â Â if (head == NULL) {

Â Â printf("The list is empty");

Â Â return;

Â Â }

Â Â do {

Â Â printf("%d ", ptr->data);

Â Â ptr = ptr->next;

Â Â } while (ptr != head->next);

}

int main() {

Â Â struct Node* head = NULL;

Â Â return 0;

}

#### Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN Stack

### Deletion OperationÂ

#### DescriptionÂ

Deletion at beginning

delete the element from the beginning of the linked list

Deletion at end

delete the element from the end of the list

Deletion after specific node

delete element after specific element

Program to perform deletion operation on Circular Linked List.

#include <stdio.h>

#include <stdlib.h>

struct Node {

Â Â int data;

Â Â struct Node* next;

};

struct Node* insertToEmptylist(struct Node* head, int data)Â

{

Â Â if (head != NULL)

Â Â struct Node* new = (struct Node*)malloc(sizeof(struct Node));Â Â

Â Â new->data = data;

}

struct Node* insertAtbegin(struct Node* head, int data)Â

{

Â Â if (head == NULL)

Â Â struct Node* new = (struct Node*)malloc(sizeof(struct Node));Â

Â Â new->data = data;

}

struct Node* insertAtend(struct Node* last, int data)Â

{

Â Â if (last == NULL)Â

Â Â return insertToEmptylist(last, data);

Â Â struct Node* new = (struct Node*)malloc(sizeof(struct Node));

Â Â new->data = data;

Â Â new->next = last->next;

Â Â last->next = new;

Â Â last = new;

Â Â return last;

}

struct Node* insertionAfternode(struct Node* last, int data, int ele)Â

{

Â Â if (last == NULL)

Â Â return NULL;

Â Â struct Node *new, *ptr;

Â Â ptr = last->next;

Â Â do {

Â Â if (ptr->data == ele)Â

{

Â Â Â Â new = (struct Node*)malloc(sizeof(struct Node));

Â Â Â Â new->data = data;

Â Â Â Â new->next = ptr->next;

Â Â Â Â ptr->next = new;

Â Â Â Â if (ptr == last) last = new;

Â Â Â Â return last;

Â Â }

Â Â ptr = ptr->next;

Â Â } while (ptr != last->next);

Â Â printf("\nThe node is not present in the linked list");

Â Â return last;

}

void deleteNode(struct Node** last, int key_node)Â

{

Â Â if (*last == NULL)Â

Â Â return;

Â Â if ((*last)->data == key_node && (*last)->next == *last)

Â {

Â Â free(*last);

Â Â *last = NULL;

Â Â return;

Â Â }

Â Â struct Node *temp = *last, *dele_node;

Â Â if ((*last)->data == key_node)

Â {

Â Â while (temp->next != *last)Â

Â Â temp = temp->next;

Â Â temp->next = (*last)->next;

Â Â free(*last);

Â Â *last = temp->next;

Â Â }

Â Â while (temp->next != *last && temp->next->data != key_node)Â

{

Â Â temp = temp->next;

Â Â }

Â Â if (temp->next->data == key_node)Â

{

Â Â dele_node = temp->next;

Â Â temp->next = dele_node->next;

Â Â free(dele_node);

Â Â }

}

Â {

Â Â struct Node* ptr;

Â Â if (head == NULL)

Â {

Â Â printf("The list is empty");

Â Â return;

Â Â }

Â Â do {

Â Â printf("%d ", ptr->data);

Â Â ptr = ptr->next;

Â Â } while (ptr != head->next);

}

int main()Â

{

Â Â struct Node* head = NULL;

printf("\n after deletion, the Linked list elements are: ");

Â Â printf("\n");

Â Â return 0;

}

#### Learn the Ins & Outs of Software Development

Caltech Coding Bootcamp

### Traversal Operation

#### DescriptionÂ

TraversalÂ

Visiting every node in a list at least once

Â In a circular linked list traversal operation, we visit every node at least once to display all the data elements or perform operations.

Function to perform traversal operation on circular singly linked list

void traversal (struct Node* head) {

Â Â struct Node* ptr;

Â Â if (head == NULL) {

Â Â printf("The list is empty");

Â Â return;

Â Â }

Â Â do {

Â Â printf("%d ", ptr->data);

Â Â ptr = ptr->next;

Â Â } while (ptr != head->next);

}

Therefore, we have reached the end of this tutorial on Circular Linked List In C.

Accelerate your career as a skilled MERN Stack Developer by enrolling in a unique Full Stack Developer - MERN Stack Master's program. Get complete development and testing knowledge on the latest technologies by opting for the MERN Stack Developer Course. Contact us TODAY!

## Next Steps

"Circular Doubly Linked List In C" can be your next topic. So far, you have learned the Circular Linked List In C. The following fundamentals will be the data structures and the varieties in data structures used for different purposes.

If you are interested in software development, you can explore Simplilearn's Courses that will give you the work-ready software development training you need to succeed today. Are you looking for a comprehensive training program in the most in-demand software development skills, tools, and languages? Our Full Stack Developer - MERN Stack should be the right thing for your career. Explore the course and enroll soon.

Please let us know in the comment section below if you have questions regarding the "Circular Linked List In C" tutorial. Our experts will get back to you at the earliest.