0% found this document useful (0 votes)
74 views7 pages

Circular Double Linked List

A circular double linked list has pointers pointing in both directions in a circular manner. This simplifies insertion and deletion operations compared to a regular double linked list. Nodes have both a left and right pointer, with the rightmost node pointing to the first node and the leftmost node pointing to the last node, forming a circle. Basic operations on this data structure are creation, insertion, deletion, and traversal from left to right or right to left.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views7 pages

Circular Double Linked List

A circular double linked list has pointers pointing in both directions in a circular manner. This simplifies insertion and deletion operations compared to a regular double linked list. Nodes have both a left and right pointer, with the rightmost node pointing to the first node and the leftmost node pointing to the last node, forming a circle. Basic operations on this data structure are creation, insertion, deletion, and traversal from left to right or right to left.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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

start 300 10 200 100 20 300 200 30 100

100 200 300

Figure 3.8.1. Circular Double Linked List

The basic operations in a circular double linked list are:

Creation.
Insertion.
Deletion.
Traversing.

Dept. of CSE, KSRMCE Page 1


Creating a Circular Double Linked List with n number of nodes:

The following steps are to be followed to create n number of nodes:

Get the new node using


getnode(). newnode =
getnode();

If the list is empty, then do the following


start = newnode;
newnode -> left = start;
newnode ->right = start;

If the list is not empty, follow the steps given


below: newnode -> left = start -> left;
newnode -> right = start; start
-> left->right = newnode; start
-> left = newnode;

Repeat the above steps n times.

The function cdll_createlist(), is used to create n number of nodes:

Inserting a node at the beginning:

The following steps are to be followed to insert a new node at the beginning of the list:

Get the new node using


getnode().
newnode=getnode();

If the list is empty, then


start = newnode;
newnode -> left = start;
newnode -> right = start;

If the list is not empty, follow the steps given below:


newnode -> left = start -> left;
newnode -> right = start;
start -> left -> right = newnode;
start -> left = newnode;
start = newnode;

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

400 10 200 100 20 300 200 30 400

100 200 300

300 40 100

400

Figure 3.8.2. Inserting a node at the beginning

Dept. of CSE, KSRMCE Page 2


Inserting a node at the end:

The following steps are followed to insert a new node at the end of the list:

Get the new node using


getnode() newnode=getnode();

If the list is empty, then


start = newnode;
newnode -> left = start;
newnode -> right = start;

If the list is not empty follow the steps given below:


newnode -> left = start -> left;
newnode -> right = start;
start -> left -> right = newnode;
start -> left = newnode;

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

400 10 200 100 20 300 200 30 400

100 200 300

300 40 100

400

Figure 3.8.3. Inserting a node at the end

Inserting a node at an intermediate position:

The following steps are followed, to insert a new node in an intermediate position in the
list:

Get the new node using


getnode().
newnode=getnode();

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++;

Dept. of CSE, KSRMCE Page 3


Dept. of CSE, KSRMCE Page 4
The function cdll_insert_mid(), is used for inserting a node in the intermediate position.
Figure 3.8.4 shows inserting a node into the circular double linked list at a specified
intermediate position other than beginning and end.

start
100 40 200
100
400
300 10 400 400 20 300

100 200

200 30 100

300

Figure 3.8.4. Inserting a node at an intermediate position

Deleting a node at the beginning:

The following steps are followed, to delete a node at the beginning of the list:

If list is empty then display Empty List message.

If the list is not empty, follow the steps given below:

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

300 10 200 300 20 300 200 30 200


100 200 300

Figure 3.8.5. Deleting a node at beginning

Deleting a node at the end:

The following steps are followed to delete a node at the end of the list:

If list is empty then display Empty List message

If the list is not empty, follow the steps given below:


temp = start;
while(temp -> right != start)
{
temp = temp -> right;
}
temp -> left -> right = temp -> right;
temp -> right -> left = temp -> left;

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

200 10 200 100 20 100 200 30 100

100 200 300

Figure 3.8.6. Deleting a node at the end

Deleting a node at Intermediate position:

The following steps are followed, to delete a node from an intermediate position in the
list (List must contain more than two node).

If list is empty then display Empty List message.

If the list is not empty, follow the steps given below:

Get the position of the node to delete.

Ensure that the specified position is in between first node and last
node. If not, specified position is invalid.

Then perform the following steps:

if(pos > 1 && pos < nodectr)


{

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

300 10 300 100 20 300 100 30 100

100 200 300

Figure 3.8.7. Deleting a node at an intermediate position

Traversing a circular double linked list from left to right:

The following steps are followed, to traverse a list from left to right:

If list is empty then display Empty List message.

If the list is not empty, follow the steps given below:


temp = start;
Print temp -> data; temp
= temp -> right;
while(temp != start)
{
print temp -> data; temp
= temp -> right;
}

The function cdll_display_left _right(), is used for traversing from left to right.

Traversing a circular double linked list from right to left:

The following steps are followed, to traverse a list from right to left:

If list is empty then display Empty List message.

If the list is not empty, follow the steps given below:


temp = start;
do
{
temp = temp -> left;
print temp -> data;
} while(temp != start);

The function cdll_display_right_left(), is used for traversing from right to left.

You might also like