0% found this document useful (0 votes)
17 views

Circular Linked List

The document discusses circular linked lists, which are linked lists where the last node points to the first node to form a circular structure. It describes two types of circular linked lists and how to represent them with nodes. It also explains how to perform common operations like insertion and deletion on circular linked lists.

Uploaded by

Bidhan Barai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Circular Linked List

The document discusses circular linked lists, which are linked lists where the last node points to the first node to form a circular structure. It describes two types of circular linked lists and how to represent them with nodes. It also explains how to perform common operations like insertion and deletion on circular linked lists.

Uploaded by

Bidhan Barai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Introduction to Circular Linked List

What is Circular linked list?


The circular linked list is a linked list where all nodes are connected to form a circle. In a circular
linked list, the first node and the last node are connected to each other which forms a circle. There is
no NULL at the end.

There are generally two types of circular linked lists:


• Circular singly linked list: In a circular Singly linked list, the last node of the list contains a
pointer to the first node of the list. We traverse the circular singly linked list until we reach
the same node where we started. The circular singly linked list has no beginning or end. No
null value is present in the next part of any of the nodes.
• Circular Doubly linked list: Circular Doubly Linked List has properties of both doubly
linked list and circular linked list in which two consecutive elements are linked or connected
by the previous and next pointer and the last node points to the first node by the next pointer
and also the first node points to the last node by the previous pointer.

Representation of circular linked list:


Circular linked lists are similar to single Linked Lists with the exception of connecting the last node
to the first node.
Node representation of a Circular Linked List:
struct Node {
int data;
struct Node *next;
};
Operations on the circular linked list:
We can do some operations on the circular linked list similar to the singly linked list which are:
1. Insertion
2. Deletion

1. Insertion in the circular linked list:


A node can be added in three ways:
1. Insertion at the beginning of the list
2. Insertion at the end of the list
3. Insertion in between the nodes

1) Insertion at the beginning of the list: To insert a node at the beginning of the list, follow these
steps:
• Create a node, say T.
• Make T -> next = last -> next.
• last -> next = T.

And then,

Below is the code implementation to insert a node at the beginning of the list:

void insertAtBeginning(int data) {


struct Node *newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;

if (!head) {
head = newNode;
newNode->next = head;
} else {
struct Node *current = head;

while (current->next != head) {


current = current->next;
}

newNode->next = head;
current->next = newNode;
head = newNode;
}
}
Time complexity: O(1) to insert a node at the beginning no need to traverse list it takes constant
time
Auxiliary Space: O(1)

2) Insertion at the end of the list: To insert a node at the end of the list, follow these steps:
• Create a node, say T.
• Make T -> next = last -> next;
• last -> next = T.
• last = T.

After insertion,

Below is the code implementation to insert a node at the beginning of the list:

void insertAtEnd(int newData) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = newData;

if (!head) {
head = newNode;
newNode->next = head;
} else {
struct Node* current = head;

while (current->next != head) {


current = current->next;
}

current->next = newNode;
newNode->next = head;
}
}
Time Complexity: O(1) to insert a node at the end of the list. No need to traverse the list as we are
utilizing the last pointer, hence it takes constant time.
Auxiliary Space: O(1)

3) Insertion in between the nodes: To insert a node in between the two nodes, follow these steps:
• Create a node, say T.
• Search for the node after which T needs to be inserted, say that node is P.
• Make T -> next = P -> next;
• P -> next = T.
Suppose 12 needs to be inserted after the node has the value 10,

Below is the code to insert a node at the specified position of the List:

void insertAtPosition(int position, Node *newNode) {


if (position < 1 || position > getSize() + 1) {
printf("Invalid position!");
return;
}

if (position == 1) {
insertAtBeginning(newNode);
} else if (position == getSize() + 1) {
insertAtEnd(newNode);
} else {
Node *current = head;
for (int i = 1; i < position - 1; i++) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
Time Complexity: O(N)
Auxiliary Space: O(1)
2. Deletion in a circular linked list:
1) Delete the node only if it is the only node in the circular linked list:
• Free the node’s memory
• The last value should be NULL A node always points to another node, so NULL assignment
is not necessary.
Any node can be set as the starting point.
Nodes are traversed quickly from the first to the last.
2) Deletion of the last node:
• Locate the node before the last node (let it be temp)
• Keep the address of the node next to the last node in temp
• Delete the last memory
• Put temp at the end
3) Delete any node from the circular linked list: We will be given a node and our task is to delete
that node from the circular linked list.
void deleteNode(struct Node** head, int key)
{

// If linked list is empty


if (*head == NULL)
return;

// If the list contains only a


// single node
if ((*head)->data == key && (*head)->next == *head) {
free(*head);
*head = NULL;
return;
}

struct Node *last = *head, *d;

// If head is to be deleted
if ((*head)->data == key) {

// Find the last node of the list


while (last->next != *head)
last = last->next;

// Point last node to the next of


// head i.e. the second node
// of the list
last->next = (*head)->next;
free(*head);
*head = last->next;
return;
}
// Either the node to be deleted is
// not found or the end of list
// is not reached
while (last->next != *head && last->next->data != key) {
last = last->next;
}

// If node to be deleted was found


if (last->next->data == key) {
d = last->next;
last->next = d->next;
free(d);
}
else
printf("Given node is not found in the list!!!\n");
}
Time Complexity: O(N), Worst case occurs when the element to be deleted is the last element and
we need to move through the whole list.
Auxiliary Space: O(1), As constant extra space is used.

Advantages of Circular Linked Lists:


• Any node can be a starting point. We can traverse the whole list by starting from any point.
We just need to stop when the first visited node is visited again.
• Useful for implementation of a queue. Unlike this implementation, we don’t need to
maintain two pointers for front and rear if we use a circular linked list. We can maintain a
pointer to the last inserted node and the front can always be obtained as next of last.

• Circular lists are useful in applications to repeatedly go around the list. For example, when
multiple applications are running on a PC, it is common for the operating system to put the
running applications on a list and then cycle through them, giving each of them a slice of
time to execute, and then making them wait while the CPU is given to another application. It
is convenient for the operating system to use a circular list so that when it reaches the end of
the list it can cycle around to the front of the list.
• Circular Doubly Linked Lists are used for the implementation of advanced data structures
like the Fibonacci Heap.
• Implementing a circular linked list can be relatively easy compared to other more complex
data structures like trees or graphs.

Disadvantages of circular linked list:


• Compared to singly linked lists, circular lists are more complex.
• Reversing a circular list is more complicated than singly or doubly reversing a circular list.
• It is possible for the code to go into an infinite loop if it is not handled carefully.
• It is harder to find the end of the list and control the loop.
• Although circular linked lists can be efficient in certain applications, their performance can
be slower than other data structures in certain cases, such as when the list needs to be sorted
or searched.
• Circular linked lists don’t provide direct access to individual nodes.

Why circular linked list?


• A node always points to another node, so NULL assignment is not necessary.
• Any node can be set as the starting point.
• Nodes are traversed quickly from the first to the last.

You might also like