
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Circular Singly Linked List
A circular singly linked list is a type of data structure that is made up of nodes that are created using self-referential structures. Each node contains two components, namely the data element and the reference to the next node in the list.
Only the reference to the head node is required to access the whole linked list. The last node of the list points to the head node, which makes it a circular linked list. Let's see a diagram of circular linked list.

Circular linked lists are mainly useful for scheduling tasks and managing playlists, enabling smooth navigation.
Circular Singly Linked List in C++
First, create the structure Node that forms the linked list node. It contains the data and a pointer to the next.
struct Node { int data; struct Node *next; };
The function insert() inserts the data into the beginning of the linked list. It creates a new node and inserts the number in the data field of the new node. If the head is NULL, then new node points to itself; otherwise, the last node in the circular linked list points to new node.
void insert(int newdata) { struct Node *newnode = (struct Node *)malloc(sizeof(struct Node)); struct Node *ptr = head; newnode->data = newdata; newnode->next = head; if (head!= NULL) { while (ptr->next != head) ptr = ptr->next; ptr->next = newnode; } else newnode->next = newnode; head = newnode; }
The function display() displays the whole linked list. First ptr points to head. Then it is continuously forwarded to the next node until all the data values of the nodes are printed.
void display() { struct Node* ptr; ptr = head; do { cout<< ptr->data <<" "; ptr = ptr->next; } while(ptr != head); }
In the function main(), first, various values are inserted into the circular linked list by calling insert(). Then the linked list is displayed.
int main() { insert(3); insert(1); insert(7); insert(2); insert(9); cout<<"The circular linked list is: "; display(); return 0; }
Example of Circular Singly Linked List
Following is the C++ example implementation of a circular singly linked list:
#include <iostream> using namespace std; struct Node { int data; struct Node * next; }; struct Node * head = NULL; void insert(int newdata) { struct Node * newnode = (struct Node * ) malloc(sizeof(struct Node)); struct Node * ptr = head; newnode -> data = newdata; newnode -> next = head; if (head != NULL) { while (ptr -> next != head) ptr = ptr -> next; ptr -> next = newnode; } else newnode -> next = newnode; head = newnode; } void display() { struct Node * ptr; ptr = head; do { cout << ptr -> data << " "; ptr = ptr -> next; } while (ptr != head); } int main() { insert(3); insert(1); insert(7); insert(2); insert(9); cout << "The circular linked list is: "; display(); return 0; }
Following is the circular singly linked list:
The circular linked list is: 9 2 7 1 3