0% found this document useful (0 votes)
11 views37 pages

Circular Linked List and DLL 16

The document explains the concepts of Circular Linked Lists (CLL) and Doubly Linked Lists, detailing their structures, advantages, and algorithms for insertion and deletion operations. CLL allows for easier access to preceding nodes and constant time insertion at both ends, while Doubly Linked Lists facilitate traversal in both directions. The document also provides algorithms for various operations, including insertion at different positions and deletion of nodes.

Uploaded by

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

Circular Linked List and DLL 16

The document explains the concepts of Circular Linked Lists (CLL) and Doubly Linked Lists, detailing their structures, advantages, and algorithms for insertion and deletion operations. CLL allows for easier access to preceding nodes and constant time insertion at both ends, while Doubly Linked Lists facilitate traversal in both directions. The document also provides algorithms for various operations, including insertion at different positions and deletion of nodes.

Uploaded by

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

Circular Linked List and

Double Linked List


Why Circular?
• In a singly linked list, for accessing any node of
linked list, we start traversing from the first node.
• If we are at any node in the middle of the list,
then it is not possible to access nodes that
precede the given node.
• This problem can be solved by slightly altering the
structure of singly linked list.
• In a singly linked list, next part (pointer to next
node) is NULL, if we utilize this link to point to the
first node then we can reach preceding nodes.
The pointer last points to node Z and last -> next points to node P.
Why have we taken a pointer that points to the last node instead of
first node ?

• For insertion of node in the beginning we need


traverse the whole list.
• Also, for insertion at the end, the whole list has
to be traversed.
• If instead of start pointer we take a pointer to
the last node then in both the cases there
won’t be any need to traverse the whole list.
• So insertion in the beginning or at the end
takes constant time irrespective of the length of
the list.
Insertion
A node can be added in three ways:

Insertion in an empty list


Insertion at the beginning of the list
Insertion at the end of the list
Insertion in between the nodes
Memory Representation of CLL
Insertion into circular singly linked list at
beginning
Algorithm
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 11
[END OF IF]

Step 2: SET NEW_NODE = PTR


Step 3: SET PTR = PTR -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET TEMP = HEAD
Step 6: Repeat Step 7 while TEMP -> NEXT != HEAD
Step 7: SET TEMP = TEMP -> NEXT
[END OF LOOP]

Step 8: SET NEW_NODE -> NEXT = HEAD


Step 9: SET TEMP → NEXT = NEW_NODE
Step 10: SET HEAD = NEW_NODE
Step 11: EXIT
Insertion into circular singly linked list at the end
Algorithm
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 1
[END OF IF]

Step 2: SET NEW_NODE = PTR


Step 3: SET PTR = PTR -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> NEXT = HEAD
Step 6: SET TEMP = HEAD
Step 7: Repeat Step 8 while TEMP -> NEXT != HEAD
Step 8: SET TEMP = TEMP -> NEXT
[END OF LOOP]

Step 9: SET TEMP -> NEXT = NEW_NODE


Step 10: EXIT
• Insertion in between the nodes
To Insert a node in between the two nodes,
follow these step:
1. Create a node, say T.
2. Search the node after which T need to be
insert, say that node be P.
3. Make T -> next = P -> next;
4. P -> next = T.
Suppose 12 need to be insert before node
having value 10,
Deletion in CLL

• given a node and our task is to delete that


node from the circular linked list.
• Examples:

Algorithm
Case 1: List is empty.
If the list is empty we will simply return.
Deletion in circular singly linked list at
beginning
• The list is Empty
– If the list is empty then the condition head ==
NULL will become true, in this case, we just need
to print underflow on the screen and make exit.
if(head == NULL)
{
printf("\nUNDERFLOW");
return;
}
• The list contains single node
If the list contains single node then, the
condition head → next == head will become true.
In this case, we need to delete the entire list and
make the head pointer free.
if(head->next == head)
{
head = NULL;
free(head);
}
• The list contains more than one node
• we need to traverse the list by using the
pointer ptr to reach the last node of the list.
This will be done by using the following
statements.
ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
At the end of the loop,
ptr->next = head->next;
free(head);
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]

Step 2: SET PTR = HEAD


Step 3: Repeat Step 4 while PTR → NEXT != HEAD
Step 4: SET PTR = PTR → next
[END OF LOOP]

Step 5: SET PTR → NEXT = HEAD → NEXT


Step 6: FREE HEAD
Step 7: SET HEAD = PTR → NEXT
Step 8: EXIT
Deletion in Circular singly linked list at the
end
the list contains single element
if(head->next == head)
{
head = NULL;
free(head);
}

the list contains more than one element


ptr = head;
while(ptr ->next != head)
{
preptr=ptr;
ptr = ptr->next;
}
we need to make just one more pointer adjustment. We need to make the next pointer
of preptr point to the next of ptr (i.e. head) and then make pointer ptr free.

preptr->next = ptr -> next;


free(ptr);
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]

Step 2: SET PTR = HEAD


Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != HEAD
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]

Step 6: SET PREPTR -> NEXT = HEAD


Step 7: FREE PTR
Step 8: EXIT
Inset at given
position
Searching algorithm
Traversing in Circular Singly linked list
Traversing in circular singly linked list can be done through a loop. Initialize the temporary
pointer variable temp to head pointer and run the while loop until the next pointer of temp
becomes head.
Algorithm
STEP 1: SET PTR = HEAD
STEP 2: IF PTR = NULL
WRITE "EMPTY LIST"
GOTO STEP 8
END OF IF
STEP 4: REPEAT STEP 5 AND 6
UNTIL PTR → NEXT != HEAD
STEP 5: PRINT PTR → DATA
STEP 6: PTR = PTR → NEXT
[END OF LOOP]

STEP 7: PRINT PTR→ DATA


Doubly linked list
• Doubly linked list is a complex type of linked list in which
a node contains a pointer to the previous as well as the
next node in the sequence.
• Therefore, in a doubly linked list, a node consists of three
parts: node data, pointer to the next node in sequence
(next pointer) , pointer to the previous node (previous
pointer).
struct node
{
struct node *prev;
int data;
struct node *next;
}
Memory Representation of a doubly linked list

-1 indicates NULL
Insertion in doubly linked list at beginning

Algorithm :
Step 1: SET NEW_NODE = PTR
Step 2: SET NEW_NODE -> PREV = NULL
Step 3: SET NEW_NODE -> NEXT = START
Step 4: SET head -> PREV = NEW_NODE
Step 5: SET head = NEW_NODE
Step 6: EXIT
Insertion in doubly linked list at the end

Algorithm

Step 1: SET NEW_NODE = PTR


Step 2: SET NEW_NODE -> DATA = VAL
Step 3: SET NEW_NODE -> NEXT = NULL
Step 4: SET TEMP = START
Step 5: Repeat Step 6 while TEMP -> NEXT != NULL
Step 6: SET TEMP = TEMP -> NEXT
[END OF LOOP]
Step 7: SET TEMP -> NEXT = NEW_NODE
Step 8: SET NEW_NODE -> PREV = TEMP
Step 9: EXIT
Insertion in doubly linked list after Specified node

Algorithm

Step 1: SET NEW_NODE = PTR


Step 2: SET PTR = PTR -> NEXT Step 10: SET NEW_NODE -> NEXT = TEMP -> NEXT
Step 3: SET NEW_NODE -> DATA = VAL Step 11: SET NEW_NODE -> PREV = TEMP
Step 4: SET TEMP = START Step 12 : SET TEMP -> NEXT = NEW_NODE
Step 5: SET I = 0 Step 13: SET TEMP -> NEXT -> PREV = NEW_NODE
Step 6: REPEAT 7 to 9 Step 14: EXIT
Step 7: SET TEMP = TEMP -> NEXT
STEP 8: IF TEMP = NULL // no further node
STEP 9: WRITE "LESS THAN DESIRED NO. OF ELEMENTS"
GOTO STEP 14
[END OF IF]
[END OF LOOP]
Deletion at beginning

Algorithm
STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 6
STEP 2: SET PTR = HEAD
STEP 3: SET HEAD = HEAD → NEXT
STEP 4: SET HEAD → PREV = NULL
STEP 5: FREE PTR
STEP 6: EXIT
Deletion in doubly linked list at the end

ALGORITHM
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 7
[END OF IF]

Step 2: SET TEMP = HEAD


Step 3: REPEAT STEP 4 WHILE TEMP->NEXT != NULL
Step 4: SET TEMP = TEMP->NEXT
[END OF LOOP]

Step 5: SET TEMP ->PREV-> NEXT = NULL


Step 6: FREE TEMP
Step 7: EXIT
Deletion in doubly linked list after the specified node

Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 9
[END OF IF]

Step 2: SET TEMP = HEAD


Step 3: Repeat Step 4 while TEMP -> DATA != ITEM
Step 4: SET TEMP = TEMP -> NEXT
[END OF LOOP]

Step 5: SET PTR = TEMP -> NEXT


Step 6: SET TEMP -> NEXT = PTR -> NEXT
Step 7: SET PTR -> NEXT -> PREV = TEMP
Step 8: FREE PTR
Step 9: EXIT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy