Follow these steps to implement a circular linked list:
- Define a structure called node. To store data in a circular linked list, define a data member in the node structure. Besides a data member, define a pointer that will point at the next node.
- A pointer called startList is initialized to NULL. The startList pointer will designate the start of the circular linked list.
- A menu is displayed and the user is asked to press 1 to add elements to the circular linked list, 2 to display elements of the circular linked list, and 3 to quit the program. If the user enters 1, go to step 4. If they enter 2, go to step 16. If they enter 3, it means they want to quit the program, so go to step 23.
- The user is prompted to specify how many numbers they want to add to the circular linked list. A loop is set to execute for the specified number of times; that is, steps 5 to 14 are repeated for the specified number of times.
- Allocate memory for the new node.
- Ask the user for the value to be added to the circular linked list. The value that's entered by the user is assigned to the data member of the node.
- If startList is NULL—that is, if it is the first node of the circular linked list—then make the startList pointer point at a new node.
- To make a linked list appear as circular, make the next pointer of startList point at startList.
- If startList is not NULL—that is, if it is not the first node of the circular linked list—follow steps 10 to 14.
- Make the temp pointer point at startList.
- Until the next pointer of temp is equal to startList, make the temp pointer point to where its next pointer is pointing; that is, set the temp pointer so that it points at the last node of the circular linked list.
- Once the temp pointer reaches the last node of the circular linked list, the next pointer of temp is set to point at the new node.
- Then, the temp pointer is set to point at the new node.
- The next pointer of temp is set to point at startLIst.
- Go to step 3 to display the menu.
- The previous step ensures startList is not NULL. If startList is NULL, it means the circular linked list is empty. In that case, a message is displayed, informing the user that the circular linked list is empty. Then, control jumps to step 3 to display the menu.
- If startList is not NULL, the data member of the node being pointed at by the startList pointer is displayed on the screen.
- A temporary pointer, temp, is set to point to where the next pointer of startList is pointing to.
- Repeat steps 20 and 21 until the temp pointer reaches the node being pointed at by the startList pointer.
- Display the contents of the node being pointed at by the data member of temp.
- The temp pointer is set to point to where its next pointer is pointing.
- Jump to step 3 to display the menu.
- Terminate the program.
The program for implementing a circular linked list is as follows:
//circularlinkedlist.c
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * next;
};
struct node * startList = NULL;
void addlist(struct node ** h);
void disp();
int main() {
struct node * newNode;
int n = 0, i, k;
while (n != 3) {
printf("\n1. Adding elements to the circular linked list\n");
printf("2. Displaying elements of the circular linked list\n");
printf("3. Quit\n");
printf("Enter your choice 1/2/3: ");
scanf("%d", & n);
switch (n) {
case 1:
printf("How many values are there ");
scanf("%d", & k);
printf("Enter %d values\n", k);
for (i = 1; i <= k; i++) {
newNode = (struct node * ) malloc(sizeof(struct node));
scanf("%d", & newNode - > data);
addlist( & newNode);
}
printf("Values added in Circular Linked List \n");
break;
case 2:
disp();
break;
}
}
return 0;
}
void addlist(struct node ** NewNode) {
struct node * temp;
if (startList == NULL) {
startList = * NewNode;
startList - > next = startList;
} else {
temp = startList;
while (temp - > next != startList)
temp = temp - > next;
temp - > next = * NewNode;
temp = * NewNode;
temp - > next = startList;
}
}
void disp() {
struct node * temp;
if (startList == NULL)
printf("The circular linked list is empty\n");
else {
printf("Following are the elements in circular linked list:\n");
printf("%d\n", startList - > data);
temp = startList - > next;
while (temp != startList) {
printf("%d\n", temp - > data);
temp = temp - > next;
}
}
}
Now, let's go behind the scenes so that we can understand the code.