Circular Double Linked List
Circular Double Linked List
A circular double linked list has both successor pointer and predecessor pointer in
circular manner. The objective behind considering circular double linked list is to
simplify the insertion and deletion operations performed on double linked list. In circular
double linked list the right link of the right most node points back to the start node and
left link of the first node points to the last node. A circular double linked list is shown in
figure 3.8.1.
100
Creation.
Insertion.
Deletion.
Traversing.
The following steps are to be followed to insert a new node at the beginning of the list:
The function cdll_insert_beg(), is used for inserting a node at the beginning. Figure
3.8.2 shows inserting a node into the circular double linked list at the beginning.
start
400
300 40 100
400
The following steps are followed to insert a new node at the end of the list:
The function cdll_insert_end(), is used for inserting a node at the end. Figure 3.8.3
shows inserting a node into the circular linked list at the end.
start
100
300 40 100
400
The following steps are followed, to insert a new node in an intermediate position in the
list:
Ensure that the specified position is in between first node and last node. If
not, specified position is invalid. This is done by countnode() function.
Store the starting address (which is in start pointer) in temp. Then traverse
the temp pointer upto the specified position.
After reaching the specified position, follow the steps given below:
newnode -> left = temp; newnode
-> right = temp -> right; temp ->
right -> left = newnode; temp ->
right = newnode; nodectr++;
start
100 40 200
100
400
300 10 400 400 20 300
100 200
200 30 100
300
The following steps are followed, to delete a node at the beginning of the list:
temp = start;
start = start -> right;
temp -> left -> right = start;
start -> left = temp -> left;
The function cdll_delete_beg(), is used for deleting the first node in the list. Figure
3.8.5 shows deleting a node at the beginning of a circular double linked list.
start
200
The following steps are followed to delete a node at the end of the list:
The function cdll_delete_last(), is used for deleting the last node in the list. Figure 3.8.6
shows deleting a node at the end of a circular double linked list.
start
100
The following steps are followed, to delete a node from an intermediate position in the
list (List must contain more than two node).
Ensure that the specified position is in between first node and last
node. If not, specified position is invalid.
temp = start;
i = 1;
while(i < pos)
{
temp = temp -> right ;
i++;
}
temp -> right -> left = temp -> left;
temp -> left -> right = temp -> right;
free(temp);
printf("\n node deleted..");
nodectr--;
}
The function cdll_delete_mid(), is used for deleting the intermediate node in the list.
Figure 3.8.7 shows deleting a node at a specified intermediate position other than beginning
and end from a circular double linked list.
start
100
The following steps are followed, to traverse a list from left to right:
The function cdll_display_left _right(), is used for traversing from left to right.
The following steps are followed, to traverse a list from right to left: