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

Circular List

The document discusses circular linked lists which are similar to singly linked lists except the last node's pointer links to the first node, forming a circle. It describes circular singly and doubly linked lists. It provides algorithms for inserting nodes at the beginning or end of a circular linked list, and deleting the first or last node. Inserting or deleting involves updating the pointers of the preceding and following nodes.

Uploaded by

S.MENAKA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views7 pages

Circular List

The document discusses circular linked lists which are similar to singly linked lists except the last node's pointer links to the first node, forming a circle. It describes circular singly and doubly linked lists. It provides algorithms for inserting nodes at the beginning or end of a circular linked list, and deleting the first or last node. Inserting or deleting involves updating the pointers of the preceding and following nodes.

Uploaded by

S.MENAKA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CIRCULAR LINKED LISTs

Definition

In a circular linked list is similar to singly linked list except that the last node contains a pointer to the first node of

the list.

Types

 Circular singly linked list

 Circular doubly linked list.

Memory representation of a circular linked list

 We can traverse the list until we find the NEXT entry that contains the address of the first node of the list.

 This denotes the end of the linked list, that is, the node that contains the address of the first node is actually

the last node of the list.

 When we traverse the DATA and NEXT in this manner, we will finally see that the linked list in the above

figure stores characters that when put together form the word HELLO.
Inserting a New Node in a Circular Linked List

Case 1: The new node is inserted at the beginning of the circular linked list.

Case 2: The new node is inserted at the end of the circular linked list.

Case 1: The new node is inserted at the beginning of the circular linked list.

We want to add a new node with data 9 as the first node of the list. Then the following changes will be done in the

linked list.

Algorithm to insert a new node at the beginning


 In Step 1, we first check whether memory is available for the new node.

 If the free memory has exhausted, then an OVERFLOW message is printed.

 Otherwise, if free memory cell is available, then we allocate space for the new node.

 Set its DATA part with the given VAL and the NEXT part is initialized with the address of the first node of

the list, which is stored in START.

 Now, since the new node is added as the first node of the list, it will now be known as the START node,

that is, the START pointer variable will now hold the address of the NEW_NODE.

 While inserting a node in a circular linked list, we have to use a while loop to traverse to the last node of the

list.

 Because the last node contains a pointer to START, its NEXT field is updated so that after insertion it

points to the new node which will be now known as START.

Case 2: The new node is inserted at the end of the circular linked list.

We want to add a new node with data 9 as the last node of the list. Then the following changes will be done in the

linked list.
Algorithm to insert a new node at the end

In Step 6, we take a pointer variable PTR and initialize it with START. That is, PTR now points to the first node of the

linked list. In the while loop, we traverse through the linked list to reach the last node. Once we reach the last node,

in Step 9, we change the NEXT pointer of the last node to store the address of the new node. Remember that the

NEXT field of the new node contains the address of the first node which is denoted by START.

Deleting a Node from a Circular Linked List

Case 1: The first node is deleted.

Case 2: The last node is deleted.

Case 1: The first node is deleted.

When we want to delete a node from the beginning of the list, then the following changes will be done in the linked

list.
Algorithm to delete the first node

 In Step 1 of the algorithm, we check if the linked list exists or not. If START = NULL, then it signifies that there

are no nodes in the list and the control is transferred to the last statement of the algorithm.

 However, if there are nodes in the linked list, then we use a pointer variable PTR which will be used to

traverse the list to ultimately reach the last node.

 In Step 5, we change the next pointer of the last node to point to the second node of the circular linked list.

 In Step 6, the memory occupied by the first node is freed.


 Finally, in Step 7, the second node now becomes the first node of the list and its address is stored in the

pointer variable START.

Case 2: The last node is deleted.

we want to delete the last node from the linked list, then the following changes will be done in the linked list.

Algorithm to delete the last node

 In Step 2, we take a pointer variable PTR and initialize it with START.

 That is, PTR now points to the first node of the linked list. In the while loop, we take another pointer variable

PREPTR such that PREPTR always points to one node before PTR.
 Once we reach the last node and the second last node, we set the next pointer of the second last node to

START, so that it now becomes the (new) last node of the linked list.

 The memory of the previous last node is freed and returned to the free pool.

You might also like