Create and Traverse Circular linked list

Create Circular linked list

  1. create a node template with the basic code given below.
struct cll
{
int data;
struct cll *next;
};

*next pointer hs address of the next node for the actual current node.

2.use typedef keyword to create an alias name for structure cll (data type).here in the below code new datatype known as node is created using the typedef keyword

typedef struct cll node;
  1. write a function for creating a newnode using malloc function and taking node data value from the user with in the function
node* getnode()
{
node * newnode;
newnode = (node *) malloc(sizeof(node));
printf("\n Enter data: ");
scanf("%d", &newnode -> data);
newnode -> next = NULL;
return newnode;
}

In the above code getnode() function returns the node type of pointer, it returns the newnode from the function .when the getnode function is called in the program this creates a node in the memory.

4.create a *head pointer and assign NULL

node *head = NULL
  1. create two more pointers known as *newnode,*temp inside createnodes function, use FOR loop for condition (i<no of nodes).

6.call getnode( ) function for the newnode pointer ,inside for loop

newnode = getnode();
  1. create a if block for condtion (head==NULL) then assign newnode to the head
head = newnode
Create and Traverse Circular linked list

for the else block

  1. to add next nodes, initialize temp with a head pointer
Create and Traverse Circular linked list

use while condition (temp->next!=NULL),so that the temp will be at the last or if there is only one node the temp pointer will be there in the first place

if there are more then one nodes,this while condition is useful ,inside while loop change temp from one node to next node until it points to last node

while(temp -> next!=NULL)
temp = temp -> next;

10.link next address of temp node to newnode (temp -> next = newnode)

Create and Traverse Circular linked list

11.after the for loop,link last node next address to the head node ,the last node will be newnode (newnode ->next = head)

Create and Traverse Circular linked list

12.last node is pointing to starting node,this is circular list

Program to Create Circular linked list

void createlist(int n)
{
int i;
node *newnode;
node *temp;
for(i = 0; i < n ; i++)
{
newnode = getnode();
if(head == NULL)
{
head = newnode;
}
else
{
temp = head;
while(temp -> next != NULL)
temp = temp-> next;
temp -> next = newnode;
}
}
newnode ->next = head; /* last node is pointing to starting node */
}

Traverse Circular linked list

  1. create a *temp pointer from node structure, initialize this temp pointer with head
Create and Traverse Circular linked list
  1. move temp pointer to next node in linked list ,move temp pointer by replacing next nodes address (temp=temp->next ) until the condition (temp!=head)
Create and Traverse Circular linked list

3.display each node’s data

Create and Traverse Circular linked list

Program to Traverse Circular linked list

void traverse()
{
node *temp;
temp = head;
printf("\n The contents of List (Left to Right): ");
if(head == NULL )
printf("\n Empty List");
else
{
do
{
printf("\t %d ", temp -> data);
temp = temp -> next;
} while(temp != head);
printf(" X ");
}
}
Last updated on by vishal devxo