Circular Linked List
Circular Linked List
In a Circular linked list, every element has a link to its next element in the
sequence, and the last element has a link to the first element. A circular
linked list is similar to the singly linked list except that the last node points
to the first node. Below is the image to illustrate the same:
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
void viewList()
// If list is empty
if (last == NULL)
printf("\nList is empty\n");
else {
struct node* temp;
temp = last->next;
do {
temp = temp->next;
// Driver Code
int main()
// Function Call
insertAtFront(10);
insertAtFront(20);
insertAtFront(30);
// Print list
viewList();
return 0;
Output
Data = 30
Data = 20
Data = 10
The time complexity of the insertAtFront function is O(1) as it performs a
constant amount of work to insert a node at the front of the linked list.
The time complexity of the viewList function is O(n) as it has to traverse
the entire linked list to print its elements.
2. Insertion at the end:
Inserting a new node as the last node. The next pointer of last will point to
this node and this new node will point to the first node.
Below is the implementation of the above operation:
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
void viewList()
// If list is empty
if (last == NULL)
printf("\nList is empty\n");
else {
temp = last->next;
do {
printf("\nData = %d", temp->info);
temp = temp->next;
// Driver Code
int main()
// Function Call
addatlast(10);
addatlast(20);
addatlast(30);
// Print list
viewList();
return 0;
Output
Data = 10
Data = 20
Data = 30
This C program defines a circular linked list, where each node points to the next node in the
list, and the last node points back to the first node, creating a circular structure. Let’s break
down the key components of the code:
Structure Definition
struct node {
int info;
struct node* next;
};
struct node: This structure defines a node in the linked list. It contains:
o info: An integer to store the data.
o next: A pointer to the next node in the list.
last: A pointer that keeps track of the last node in the list. It is initialized to NULL, indicating
that the list is initially empty.
This function adds a new node containing data at the end of the circular linked list.
1. Memory Allocation:
o A new node (temp) is created using malloc, which allocates memory for a new
node.
2. Check if List is Empty:
if (last == NULL) {
temp->info = data;
temp->next = temp;
last = temp;
}
else {
temp->info = data;
temp->next = last->next;
last->next = temp;
last = temp;
}
o If the list is not empty, the new node needs to be added after the current last node.
o temp->info is set to data.
o temp->next is set to last->next, which is the first node in the list.
o The next pointer of the current last node is updated to point to the new node (temp).
o Finally, last is updated to point to temp, making it the new last node.