Circular Linked List in C

Here you will get program for circular linked list in C.

What is Circular Linked List?

A circular linked list is a linked list in which the last node points to the head or front node making the data structure to look like a circle.  A circularly linked list node can be implemented using singly linked or doubly linked list.

The below representation shows how a circular linked list looks like. Unlike the linear linked list, the last node is pointed to the head using a rear pointer.

Circular Linked List in C
 
Program for Circular Linked List in C

We basically have 3 nodes head, rear and temp to implement a circular linked list. The rear points to the last node of the list, head points to first node. This helps to maintain a track on where is the front and rear nodes of the circle. Let us see the basic operations such as creation, deletion and displaying of the elements of the circular linked list.

 

Creation of Node

This is much similar to that of creating a node in singly linked list. It involves creating a new node and assigning data and pointing the current node to the head of the circular linked list. The code is as shown below.

 

 
Deletion of Node

We conventionally delete the front node from the list in this program. To delete a node, we need to check if the list is empty. If it is not empty then point the rear node to the front->next and rear->next to front. This removes the first node.

 

 
Traversing Circular Linked List

Traversing the circular list starts from front node and iteratively continues until the rear node. The following function is used for this purpose.

 

Complete Program

 

Output

The above program results in the following output. You can see that the last node points to the first node address that is the main theme of circular linked list in C.
 Program Outptut for Circular Linked List in C

Category: DSA

3 thoughts on “Circular Linked List in C

Leave a Reply

Your email address will not be published. Required fields are marked *