The main purpose of this program is to create a circular link list or a list which contains nodes (with data and address part).
We will also learn how to insert or delete a node.
The opening of the code is done by include the header files
“stdio.h” “conio.h” “alloc.h”
Struct is a user define data type
Typedef is used so we don’t have to write “struct node”
In this example we have taken a list of 5 elements you can take more, by using for loop we have created 5 nodes and initialize each node with arrow operator and stored the address of the next node in the previous one and stored the address of the first node in “start” pointer as shown below
By using switch-case statements,
We have given the options of either :-
CASE 1: Insert a node at a specified position
A node is created using malloc and then it is adjusted on the position given by then user:
If the location given by user is 1 then it will assign the new nodes address to the start pointer and will also assign the new node the address of the node which was at first position before it.
In case the location isn’t 1 then we go to the location before it with help of “for” loop and swap the locations with help of arrow operator(—>) we change the address of other nodes.
Then afterwards print the list and “break” which ends the switch case and ends the program thus
CASE 2: Insert a node at last
In this case it works as same as case 1 but with a little difference that in this we do not ask the user to give the location instead add the new node at the last and give the node which was at last before the address of the new node and give the address of the first node to the new node to make it a circular list , shown below
CASE 3: Delete a node
It is also same as of case 1 and 2 , in this we don’t have to create a node but have to delete it, so we just ask the user the position of which he wants the node to be deleted , it can be done by understanding two steps.
We will reach the node before of the real node to be deleted in the list and just change the address as shown below
thus the node gets deleted.
I have used 5 nodes but you can take more nodes and even print more than 5 or 6 six nodes to check if the list is circular or not. It’s just a matter of two-three steps in every case, you just need to understand the logic.
Implementation of Circular Linked list Program Screenshots
Final Output: Implementation of Circular Linked list program
Download Source code
Click here to download C Program Implementation of Circular Linked list
zip password: codewithc.com