Turn Desktop View Off
by Dinesh Thakur

Circularly linked lists are useful to traverse an entire list starting at any point. In a linear linked list, it is required to know the head pointer to traverse the entire list. The linear linked list cannot be traversed completely with the help of an intermediate pointer.

 

Access to any element in a doubly circularly linked list is much easier than in a linearly linked list since the particular element can be approached in two directions. For example to access an element present in the fourth node of a circularly linked list having five elements, it is enough to start from the last node and traverse the list in the reverse direction to get the value in the fourth node.

 

Implementation of a circular linked list:

 

Creating the list

 

while (input_element != -999)

{

new_node=(struct node *) malloc (size);

new_node->info=input_element;

if ( root_node==NULL )

root_node=new_node;

else

( *last_node )->next=new_node;

(*last_node)=new_node;

scanf("%d",&input_element);

}

if(root!=NULL)

new->next=root;

return root;

 

Inserting elements into the list

 

After getting the position and value to be inserted, the following code can be followed:

 

new_node=(struct node *)malloc(sizeof(struct node));

new_node-> info=input_element;

 

if((position==1)||((*root_node)==NULL))

{

new_node->next =*root_node;

*root_node = new_node;

if((*last_node)!=NULL)

(*last_node)->next=*root_node;

else

*last_node=*start_node;

}

else

{

temp_node=*root_node;

counter=2;

while ( (counter<position) && (temp_node->next !=

(*root_node) ) )

{

temp_node=temp_node->next;

++counter;

}

if(temp_node->next==(*root_node))

*last_node=new_node;

new_node->next=temp_node->next;

temp_node->next=new_node;

}

 

Deleting an element from the list

 

After getting the element to be deleted, the following code can be used:

 

If(*front_node != NULL)

{

printf(“The item deleted is %d”,(*front_node->info));

If (*front_node == *rear_node)

{

*front_node = *rear_node = NULL;

}

else

{

*front_node = *front_node->next;

*rear_node->link = *front_node;

}

}