Circular Linked List in C
Here you will learn and get the program code of Circular Linked List in C and C++ programming using data structure.
What is Circular Linked List?
A circular linked list in c is like a looped chain of nodes where each node points to the next and the last points back to the first. There’s no clear start or end, allowing continuous traversal. It’s used in programming for tasks requiring circular, never-ending structures.
Program code of Circular Linked List in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | //circular linked list in c #include <stdio.h> #include <stdlib.h> // structure node of circular linked list struct Node { int data; struct Node* next; }; // Function create new node struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (!newNode) { perror("Memory allocation failed"); exit(1); } newNode->data = data; newNode->next = NULL; return newNode; } // Function add node in last of circular linked list struct Node* insertAtEnd(struct Node* tail, int data) { struct Node* newNode = createNode(data); if (!tail) { newNode->next = newNode; return newNode; } newNode->next = tail->next; tail->next = newNode; return newNode; } // delete a node with data value struct Node* deleteNode(struct Node* tail, int data) { if (!tail) { printf("Circular linked list is empty.\n"); return NULL; } struct Node* current = tail->next; // Start from the first node struct Node* prev = tail; // Initialize the previous node // Traverse the circular linked list to find the node with the specified data while (current != tail && current->data != data) { prev = current; current = current->next; } // Check if the node with the specified data is avaliable if (current->data != data) { printf("Data %d not found in the circular linked list.\n", data); return tail; } // If the node to be deleted is the only node, set tail to NULL if (current == tail && current->next == tail) { free(current); return NULL; } // Update the pointers to bypass the node to be deleted prev->next = current->next; // If the node to be deleted is the tail, update the tail pointer if (current == tail) { tail = prev; } free(current); return tail; } // Function to display the circular linked list void display(struct Node* tail) { if (!tail) { printf("Circular linked list is empty.\n"); return; } struct Node* current = tail->next; do { printf("%d -> ", current->data); current = current->next; } while (current != tail->next); printf(" (Back to the beginning)\n"); } int main() { struct Node* tail = NULL; int choice, data; while (1) { printf("\n Circular Linked List\n"); printf("1. Insert node\n"); printf("2. Delete node\n"); printf("3. Display circular linked list\n"); printf("4. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter data to insert: "); scanf("%d", &data); tail = insertAtEnd(tail, data); break; case 2: printf("Enter data to delete: "); scanf("%d", &data); tail = deleteNode(tail, data); break; case 3: display(tail); break; case 4: printf("Exiting the program.\n"); exit(0); default: printf("Invalid choice.\n"); } } return 0; } |
Program code of Circular Linked List in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | //circular linked list in c++ #include <stdio.h> #include <stdlib.h> #include<iostream.h> // structure node of circular linked list struct Node { int data; struct Node* next; }; // Function create new node struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (!newNode) { perror("Memory allocation failed"); exit(1); } newNode->data = data; newNode->next = NULL; return newNode; } // Function add node in last of circular linked list struct Node* insertAtEnd(struct Node* tail, int data) { struct Node* newNode = createNode(data); if (!tail) { newNode->next = newNode; return newNode; } newNode->next = tail->next; tail->next = newNode; return newNode; } // delete a node with data value struct Node* deleteNode(struct Node* tail, int data) { if (!tail) { cout<<"Circular linked list is empty.\n"; return NULL; } struct Node* current = tail->next; // Start from the first node struct Node* prev = tail; // Initialize the previous node // Traverse the circular linked list to find the node with the specified data while (current != tail && current->data != data) { prev = current; current = current->next; } // Check if the node with the specified data is avaliable if (current->data != data) { cout<<"Data"<<data<<"not found in the circular linked list.\n"; return tail; } // If the node to be deleted is the only node, set tail to NULL if (current == tail && current->next == tail) { free(current); return NULL; } // Update the pointers to bypass the node to be deleted prev->next = current->next; // If the node to be deleted is the tail, update the tail pointer if (current == tail) { tail = prev; } free(current); return tail; } // Function to display the circular linked list void display(struct Node* tail) { if (!tail) { cout<<"Circular linked list is empty.\n"; return; } struct Node* current = tail->next; do { cout<<current->data<<" "; current = current->next; } while (current != tail->next); // cout<<" (Back to the beginning)\n"; } int main() { struct Node* tail = NULL; int choice, data; while (1) { cout<<"\n Circular Linked List\n"; cout<<"1. Insert node\n"; cout<<"2. Delete node\n"; cout<<"3. Display circular linked list\n"; cout<<"4. Exit\n"; cout<<"Enter your choice: "; cin>>choice; switch (choice) { case 1: cout<<"Enter data to insert: "; cin>>data; tail = insertAtEnd(tail, data); break; case 2: cout<<"Enter data to delete: "; cin>>data; tail = deleteNode(tail, data); break; case 3: display(tail); break; case 4: cout<<"Exiting the program.\n"; exit(0); default: cout<<"Invalid choice.\n"; } } return 0; } |
Output
Circular Linked List
1. Insert node
2. Delete node
3. Display circular linked list
4. Exit
Enter your choice: 1
Enter data to insert: 100
Circular Linked List
1. Insert node
2. Delete node
3. Display circular linked list
4. Exit
Enter your choice: 1
Enter data to insert: 200
Circular Linked List
1. Insert node
2. Delete node
3. Display circular linked list
4. Exit
Enter your choice: 1
Enter data to insert: 500
Circular Linked List
1. Insert node
2. Delete node
3. Display circular linked list
4. Exit
Enter your choice: 3
100 -> 200 -> 500 -> (Back to the beginning)
Circular Linked List
1. Insert node
2. Delete node
3. Display circular linked list
4. Exit
Enter your choice: 2
Enter data to delete: 200
Circular Linked List
1. Insert node
2. Delete node
3. Display circular linked list
4. Exit
Enter your choice: 3
100 -> 500 -> (Back to the beginning)
Circular Linked List
1. Insert node
2. Delete node
3. Display circular linked list
4. Exit
Enter your choice: 4
Exiting the program.