Circular Linked List
• A circular linked list is a special type of linked list where all
the nodes are connected to form a circle.
• Unlike a regular linked list, which ends with a node pointing
to NULL, the last node in a circular linked list points back to
the first node.
• Types of Circular Linked Lists
• Circular Singly Linked List
• Circular Doubly Linked List
Circular Singly Linked List
Circular Singly Linked List
• Insertion in Circular Singly Linked List
• There are four main ways to insert elements:
• Insertion in an empty list
• Insertion at the beginning of the list
• Insertion at the end of the list
• Insertion at a specific position in the list
Insertion in an Empty List
• To insert a node in empty circular linked list, creates a new node with
the given data, sets its next pointer to point to itself, and updates the
last pointer to reference this new node.
Insertion in an Empty List
STEPS:
• Check if last is not nullptr.
• If true, return last (the list is not empty).
• Otherwise, Create a new node with the provided data.
• Set the new node’s next pointer to point to itself (circular link).
• Update last to point to the new node and return it.
Insertion at the beginning
STEPS
• We first create the new node and allocate memory for it.
• If the list is empty (indicated by the last pointer being NULL), we make
the new node point to itself.
• If the list already contains nodes then we set the new node’s next
pointer to point to the current head of the list (which is last->next),
• Then update the last node’s next pointer to point to the new node. This
maintains the circular structure of the list.
Insertion at the beginning
Insertion at the End
STEPS
• If the list is empty (mean, last or tail pointer being NULL), we initialize
the list with the new node and making it point to itself to form a
circular structure.
• If the list already contains nodes then we set the new node’s next
pointer to point to the current head (which is tail->next)
• Then update the current tail’s next pointer to point to the new node.
• Finally, we update the tail pointer to the new node.
• This will ensure that the new node is now the last node in the list while
maintaining the circular linkage.
Insertion at the End
Insertion at the Specific Position
STEPS
• we first check if the list is empty.
• If it is and the position is not 1 then we print an error message because the position
doesn’t exist in the list.
• If the position is 1 then we create the new node and make it point to itself.
• If the list is not empty, we create the new node and traverse the list to find the
correct insertion point.
• If the position is 1, we insert the new node at the beginning by adjusting the pointers
accordingly.
• For other positions, we traverse through the list until we reach the desired position
and inserting the new node by updating the pointers.
• If the new node is inserted at the end, we also update the last pointer to reference the
new node, maintaining the circular structure of the list.
Insertion at the Specific Position
Circular Singly Linked List
• Deletion in Circular Singly Linked List
• There are Three main ways to delete elements:
• Deletion at the beginning
• Deletion at specific position
• Deletion at the end
Deletion at the beginning
STEPS
• Check if List is Empty:
• If last is nullptr, print “List is empty” and return nullptr.
• Get Head Node:
• Set head to last->next.
• Check for Single Node:
• If head equals last, delete head and set last to nullptr.
• Handle Multiple Nodes:
• Update last->next to point to head->next.
• Delete head.
• Return Updated last
Deletion at the beginning
Deletion at the End
STEPS
• Check if List is Empty (last is nullptr) then print “List is empty, nothing
to delete” and return nullptr.
• Get head node by Setting head to last->next.
• Check for single node, if head equals last, delete last and set last to
nullptr.
• Find second last node by traversing the list until curr->next equals last.
• Update Pointer by setting curr->next to point to head.
• Delete last and update last to curr.
• Return the updated last.
Deletion at the End
Deletion at the Specific Position
STEPS
• Check if List is Empty (last is nullptr) then print “List is empty, nothing to delete”
and return nullptr.
• Set curr to last->next (head) and prev to last.
• Check for single node, if node’s data matches key, delete it and set last to nullptr.
• Check for first node to delete, if head node’s data matches key, update last->next
and delete the head.
• Loop through the list to find the node with the specified key.
• If node found then update prev->next to skip the deleted node. If the deleted node
is last, update last to prev.
• Otherwise, print “Node with data [key] not found.”
• Return the modified last.
Deletion at the Specific Position
Circular Doubly Linked List
• A circular doubly linked list is defined as a circular linked
list in which each node has two links connecting it to the
previous node and the next node.
Circular Doubly Linked List
• Insertion in Circular Doubly Linked List
• There are Three main ways to insert elements:
• Insertion at the beginning of the list
• Insertion at the end of the list
• Insertion at a specific position in the list
Insertion at the beginning
STEPS
• Allocate memory for the new node.
• If the list is empty, set the new node’s next and prev to point to itself, and
update the head to this new node.
• For a non-empty list, insert the new node:
• Set the new node’s next to the current head.
• Set the new node’s prev to the last node.
• Update the current head’s prev to the new node.
• Update the last node’s next to the new node.
• Set the new node as the new head of the list.
Insertion at the End
STEPS
• Allocate memory for the new node.
• If the list is empty, set the new node’s next and prev pointers to point to
itself, and update the head to this new node.
• For a non-empty list, insert the new node:
• Find the current last node (the node whose next pointer points to the head).
• Set the new node’s next pointer to point to the head.
• Set the new node’s prev pointer to point to the current last node.
• Update the current last node’s next pointer to point to the new node.
• Update the head’s prev pointer to point to the new node.
Insertion at the Specific Position
STEPS
• Allocate memory for the new node.
• Initialize a pointer curr pointer to the head node and start traversing
the list we reach the node just before the desired position. Use a counter
to keep track of the curr position.
• Insert the New Node:
• Set newNode->next to curr->next.
• Set newNode->prev to curr.
• Update curr->next->prev to newNode.
• Update current->next to newNode.
• Update Head (if the insertion is at position 0 and the list is empty), set head to
newNode.
Insertion at the Specific Position
Deletion in Doubly Singly Linked List
• Case 1: Empty List(start = NULL)
• If the list is empty, simply return it.
• Case 2: The List initially contains some nodes, start points at the first
node of the List
1. If the list is not empty, then we define two pointers curr and
prev_1 and initialize the pointer curr points to the first node of the list,
and prev_1 = NULL.
2. Traverse the list using the curr pointer to find the node to be
deleted and before moving from curr to the next node, every time set
prev_1 = curr.
Deletion in Doubly Singly Linked List
3. If the node is found, check if it is the only node in the
list. If yes, set start = NULL and free the node pointing by curr.
4. If the list has more than one node, check if it is the
first node of the list. The condition to check this is (curr ==
start). If yes, then move prev_1 to the last node(prev_1 = start -
> prev). After prev_1 reaches the last node, set start = start ->
next and prev_1 -> next = start and start -> prev = prev_1. Free
the node pointing by curr.
Deletion in Doubly Singly Linked List
5. If curr is not the first node, we check if it is the last
node in the list. The condition to check this is (curr -> next ==
start). If yes, set prev_1 -> next = start and start -> prev =
prev_1. Free the node pointing by curr.
6. If the node to be deleted is neither the first node nor
the last node, declare one more pointer temp and initialize the
pointer temp points to the next of curr pointer (temp = curr-
>next). Now set, prev_1 -> next = temp and temp ->prev =
prev_1. Free the node pointing by curr.
Example
• If the given key(Say 4) matches with the first node of the
list(Step 4):
Example
• If the given key(Say 8) matches with the last node of the
list(Step 5):
Example
• If the given key(Say 6) matches with the middle node of the
list(Step 6):

Circular Linked List in Data Structures Design

  • 1.
    Circular Linked List •A circular linked list is a special type of linked list where all the nodes are connected to form a circle. • Unlike a regular linked list, which ends with a node pointing to NULL, the last node in a circular linked list points back to the first node. • Types of Circular Linked Lists • Circular Singly Linked List • Circular Doubly Linked List
  • 2.
  • 3.
    Circular Singly LinkedList • Insertion in Circular Singly Linked List • There are four main ways to insert elements: • Insertion in an empty list • Insertion at the beginning of the list • Insertion at the end of the list • Insertion at a specific position in the list
  • 4.
    Insertion in anEmpty List • To insert a node in empty circular linked list, creates a new node with the given data, sets its next pointer to point to itself, and updates the last pointer to reference this new node.
  • 5.
    Insertion in anEmpty List STEPS: • Check if last is not nullptr. • If true, return last (the list is not empty). • Otherwise, Create a new node with the provided data. • Set the new node’s next pointer to point to itself (circular link). • Update last to point to the new node and return it.
  • 6.
    Insertion at thebeginning STEPS • We first create the new node and allocate memory for it. • If the list is empty (indicated by the last pointer being NULL), we make the new node point to itself. • If the list already contains nodes then we set the new node’s next pointer to point to the current head of the list (which is last->next), • Then update the last node’s next pointer to point to the new node. This maintains the circular structure of the list.
  • 7.
  • 8.
    Insertion at theEnd STEPS • If the list is empty (mean, last or tail pointer being NULL), we initialize the list with the new node and making it point to itself to form a circular structure. • If the list already contains nodes then we set the new node’s next pointer to point to the current head (which is tail->next) • Then update the current tail’s next pointer to point to the new node. • Finally, we update the tail pointer to the new node. • This will ensure that the new node is now the last node in the list while maintaining the circular linkage.
  • 9.
  • 10.
    Insertion at theSpecific Position STEPS • we first check if the list is empty. • If it is and the position is not 1 then we print an error message because the position doesn’t exist in the list. • If the position is 1 then we create the new node and make it point to itself. • If the list is not empty, we create the new node and traverse the list to find the correct insertion point. • If the position is 1, we insert the new node at the beginning by adjusting the pointers accordingly. • For other positions, we traverse through the list until we reach the desired position and inserting the new node by updating the pointers. • If the new node is inserted at the end, we also update the last pointer to reference the new node, maintaining the circular structure of the list.
  • 11.
    Insertion at theSpecific Position
  • 12.
    Circular Singly LinkedList • Deletion in Circular Singly Linked List • There are Three main ways to delete elements: • Deletion at the beginning • Deletion at specific position • Deletion at the end
  • 13.
    Deletion at thebeginning STEPS • Check if List is Empty: • If last is nullptr, print “List is empty” and return nullptr. • Get Head Node: • Set head to last->next. • Check for Single Node: • If head equals last, delete head and set last to nullptr. • Handle Multiple Nodes: • Update last->next to point to head->next. • Delete head. • Return Updated last
  • 14.
    Deletion at thebeginning
  • 15.
    Deletion at theEnd STEPS • Check if List is Empty (last is nullptr) then print “List is empty, nothing to delete” and return nullptr. • Get head node by Setting head to last->next. • Check for single node, if head equals last, delete last and set last to nullptr. • Find second last node by traversing the list until curr->next equals last. • Update Pointer by setting curr->next to point to head. • Delete last and update last to curr. • Return the updated last.
  • 16.
  • 17.
    Deletion at theSpecific Position STEPS • Check if List is Empty (last is nullptr) then print “List is empty, nothing to delete” and return nullptr. • Set curr to last->next (head) and prev to last. • Check for single node, if node’s data matches key, delete it and set last to nullptr. • Check for first node to delete, if head node’s data matches key, update last->next and delete the head. • Loop through the list to find the node with the specified key. • If node found then update prev->next to skip the deleted node. If the deleted node is last, update last to prev. • Otherwise, print “Node with data [key] not found.” • Return the modified last.
  • 18.
    Deletion at theSpecific Position
  • 19.
    Circular Doubly LinkedList • A circular doubly linked list is defined as a circular linked list in which each node has two links connecting it to the previous node and the next node.
  • 20.
    Circular Doubly LinkedList • Insertion in Circular Doubly Linked List • There are Three main ways to insert elements: • Insertion at the beginning of the list • Insertion at the end of the list • Insertion at a specific position in the list
  • 21.
    Insertion at thebeginning STEPS • Allocate memory for the new node. • If the list is empty, set the new node’s next and prev to point to itself, and update the head to this new node. • For a non-empty list, insert the new node: • Set the new node’s next to the current head. • Set the new node’s prev to the last node. • Update the current head’s prev to the new node. • Update the last node’s next to the new node. • Set the new node as the new head of the list.
  • 22.
    Insertion at theEnd STEPS • Allocate memory for the new node. • If the list is empty, set the new node’s next and prev pointers to point to itself, and update the head to this new node. • For a non-empty list, insert the new node: • Find the current last node (the node whose next pointer points to the head). • Set the new node’s next pointer to point to the head. • Set the new node’s prev pointer to point to the current last node. • Update the current last node’s next pointer to point to the new node. • Update the head’s prev pointer to point to the new node.
  • 23.
    Insertion at theSpecific Position STEPS • Allocate memory for the new node. • Initialize a pointer curr pointer to the head node and start traversing the list we reach the node just before the desired position. Use a counter to keep track of the curr position. • Insert the New Node: • Set newNode->next to curr->next. • Set newNode->prev to curr. • Update curr->next->prev to newNode. • Update current->next to newNode. • Update Head (if the insertion is at position 0 and the list is empty), set head to newNode.
  • 24.
    Insertion at theSpecific Position
  • 25.
    Deletion in DoublySingly Linked List • Case 1: Empty List(start = NULL) • If the list is empty, simply return it. • Case 2: The List initially contains some nodes, start points at the first node of the List 1. If the list is not empty, then we define two pointers curr and prev_1 and initialize the pointer curr points to the first node of the list, and prev_1 = NULL. 2. Traverse the list using the curr pointer to find the node to be deleted and before moving from curr to the next node, every time set prev_1 = curr.
  • 26.
    Deletion in DoublySingly Linked List 3. If the node is found, check if it is the only node in the list. If yes, set start = NULL and free the node pointing by curr. 4. If the list has more than one node, check if it is the first node of the list. The condition to check this is (curr == start). If yes, then move prev_1 to the last node(prev_1 = start - > prev). After prev_1 reaches the last node, set start = start -> next and prev_1 -> next = start and start -> prev = prev_1. Free the node pointing by curr.
  • 27.
    Deletion in DoublySingly Linked List 5. If curr is not the first node, we check if it is the last node in the list. The condition to check this is (curr -> next == start). If yes, set prev_1 -> next = start and start -> prev = prev_1. Free the node pointing by curr. 6. If the node to be deleted is neither the first node nor the last node, declare one more pointer temp and initialize the pointer temp points to the next of curr pointer (temp = curr- >next). Now set, prev_1 -> next = temp and temp ->prev = prev_1. Free the node pointing by curr.
  • 28.
    Example • If thegiven key(Say 4) matches with the first node of the list(Step 4):
  • 29.
    Example • If thegiven key(Say 8) matches with the last node of the list(Step 5):
  • 30.
    Example • If thegiven key(Say 6) matches with the middle node of the list(Step 6):