A structure is defined, called a node, that consists of two members: an integer and a pointer called next. I am creating a circular linked list comprised of integer numbers, which is why I have taken an integer member. However, you can use any number of members you want, as well as any data type.
We define a pointer called startList and initialize it to NULL. The startList pointer will be used to point at the first node of the circular linked list.
A menu is displayed on the screen that shows three options: 1, to add elements to the circular linked list; 2, to display elements of the circular linked list; and 3, to quit. Obviously, the first step is to add elements to the circular linked list. Let's assume that the user enters 1. On entering 1, the user will be asked to specify how many values they want to enter in the list. The limit that's entered by the user will be assigned to a variable called k. Assuming that the user wants to enter five elements in the list, a for loop is set to run five times. Within the for loop, a new node is created called newNode. The value that's entered by the user is assigned to the data member of newNode. Assuming the value that's entered by the user is 10, it will be assigned to the data member of newNode, as follows:
The addlist function will be invoked and newNode will be passed to it as an argument. In the addlist function, it is confirmed whether it is the first node of the circular linked list or not; that is, if startList is NULL, it is set to point at newNode:
To make it a circular linked list, the next pointer of startList is set to point at startList itself:
The addlist function ends. Control goes back to the main function and resumes the for loop's execution. Within the for loop, a newNode node is created. The value that's entered by the user is assigned to the data member of newNode. Assuming that the user has entered a value of 20, it will be assigned to the data member of newNode:
Again, the addlist function is invoked and newNode is passed to it. In the addlist function, because the startList pointer is no longer NULL, the else block will be executed. In the else block, a temporary pointer called temp is set to point at startList. A while loop is set to execute until the next pointer of temp points at startList; that is, until the temp pointer reaches the last node of the circular linked list, the temp pointer will keep moving further so that it points at its next node. Because there is only a single node in the circular linked list, the temp pointer is already pointing at the last node of the list:
Once the temp pointer reaches the last node of the circular linked list, the next pointer of temp is set to point at newNode:
Thereafter, the temp pointer is set to point at newNode:
Finally, to make the linked list appear circular, the next pointer of temp is set to point at startList:
This procedure is repeated for the other three elements of the circular linked list. Assuming the other the three elements that are entered are 30, 40, and 50, the circular linked list will appear as follows:
After creating the circular linked list, the user will see the display menu again. Assuming the user wants to display the elements of the circular linked list, they will enter a value as per the menu choices. Upon entering the value, the disp function will be invoked. In the disp function, it's ensured that the startList pointer is NULL. If the startList pointer is NULL, it means the circular linked list is empty. In that case, the disp function will terminate after displaying the message that the circular linked list is empty. If the startList pointer is not empty, the value in the data member of the node being pointed at by the startList pointer is displayed on the screen; that is, a value of 10 will appear on the screen. A temporary pointer, temp, is set to point at the node being pointed at by the next pointer of startList:
A while loop is set to execute until the temp pointer reaches the node being pointed at by the startList pointer. Within the while loop, the data member of the node being pointed at by the temp pointer is displayed on the screen; that is, a value of 20 will appear on the screen. Thereafter, the temp pointer is set to point at the node that its next pointer is pointing to. This way, the while loop will execute and display all of the elements of the circular linked list. When the while loop ends, the disp function also ends. Control goes back to the main function, where the menu will be displayed once more. To exit the program, the user has to enter 3. On entering 3, the program will terminate.
The program is compiled using GCC. Because no error appears on compilation, this means the circularlinkedlist.c program has successfully compiled into the circularlinkedlist.exe file. On executing the file, we get a menu that will not only add elements to the circular linked list but will display them too. By doing this, we get the output shown in the following screenshot:
VoilĂ ! We have successfully implemented a circular linked list. Now, let's move on to the next recipe!