0% found this document useful (0 votes)
28 views6 pages

CLL

Uploaded by

yashitaa276
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)
28 views6 pages

CLL

Uploaded by

yashitaa276
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/ 6

09/08/2024

CIRCULAR LINKED LIST

A circular linked list is a data structure where the last node connects back to the first,
forming a loop. This structure allows for continuous traversal without any interruptions.
Circular linked lists are especially helpful for tasks like scheduling and managing
playlists, this allowing for smooth navigation.

1. Circular Singly Linked List


In Circular Singly Linked List, each node has just one pointer called the “next” pointer. The
next pointer of last node points back to the first node and this results in forming a circle. In
this type of Linked list we can only move through the list in one direction.

2. Circular Doubly Linked List:


In circular doubly linked list, each node has two pointers prev and next, similar to doubly
linked list. The prev pointer points to the previous node and the next points to the next node.
Here, in addition to the last node storing the address of the first node, the first node will also
store the address of the last node.

Operations on the Circular Linked list:


We can do some operations on the circular linked list similar to the singly and doubly linked
list which are:
 Insertion
o Insertion at the empty list
o Insertion at the beginning
o Insertion at the end
o Insertion at the given position
 Deletion
o Delete the first node
o Delete the last node
o Delete the node from any position
 Searching

1. Insertion at the beginning in circular linked list


To insert a new node at the beginning of a circular linked list, 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), and then
update the last node’s next pointer to point to the new node. This maintains the circular
structure of the list.

2. Insertion at the end in circular linked list


To insert a new node at the end of a circular linked list, we first create the new node and
allocate memory for it. 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.
3. Insertion at specific position in circular linked list
To insert a new node at a specific position in a circular linked list, 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.
DELETION FROM A CIRCULAR LINKED LIST
Deletion involves removing a node from the linked list. The main difference is that we need to
ensure the list remains circular after the deletion. We can delete a node in a circular linked list
in three ways:
1. Delete the first node in circular linked list
To delete the first node of a circular linked list, we first check if the list is empty. If it is then
we print a message and return NULL. If the list contains only one node (the head is the same
as the last) then we delete that node and set the last pointer to NULL. If there are multiple
nodes then we update the last->next pointer to skip the head node and effectively removing it
from the list. We then delete the head node to free the allocated memory. Finally, we return
the updated last pointer, which still points to the last node in the list.
2. Delete a specific node in circular linked list
To delete a specific node from a circular linked list, we first check if the list is empty. If it is
then we print a message and return nullptr. If the list contains only one node and it matches
the key then we delete that node and set last to nullptr. If the node to be deleted is the first
node then we update the next pointer of the last node to skip the head node and delete
the head. For other nodes, we traverse the list using two pointers: curr (to find the node)
and prev (to keep track of the previous node). If we find the node with the matching key then
we update the next pointer of prev to skip the curr node and delete it. If the node is found and
it is the last node, we update the last pointer accordingly. If the node is not found then do
nothing and tail or last as it is. Finally, we return the updated last pointer.
3. Deletion at the end of Circular linked list
To delete the last node in a circular linked list, we first check if the list is empty. If it is, we
print a message and return nullptr. If the list contains only one node (where the head is the
same as the last), we delete that node and set last to nullptr. For lists with multiple nodes, we
need to traverse the list to find the second last node. We do this by starting from the head and
moving through the list until we reach the node whose next pointer points to last. Once we find
the second last node then we update its next pointer to point back to the head, this effectively
removing the last node from the list. We then delete the last node to free up memory and return
the updated last pointer, which now points to the last node.

Advantages of Circular Linked Lists


 In circular linked list, the last node points to the first node.
 We can traverse the list from any node and return to it without needing to restart from the
head, which is useful in applications requiring a circular iteration.
 Circular linked lists can easily implement circular queues, where the last element connects
back to the first, allowing for efficient resource management.
 In a circular linked list, each node has a reference to the next node in the sequence.
 Although it doesn’t have a direct reference to the previous node like a doubly linked list,
we can still find the previous node by traversing the list.
Disadvantages of Circular Linked Lists
 Circular linked lists are more complex to implement than singly linked lists.
 Traversing a circular linked list without a clear stopping condition can lead to infinite loops
if not handled carefully.
 Debugging can be more challenging due to the circular nature, as traditional methods of
traversing linked lists may not apply.
Applications of Circular Linked Lists
 It is used for time-sharing among different users, typically through a Round-Robin
scheduling mechanism.
 In multiplayer games, a circular linked list can be used to switch between players. After the
last player’s turn, the list cycles back to the first player.
 Circular linked lists are often used in buffering applications, such as streaming data, where
data is continuously produced and consumed.
 In media players, circular linked lists can manage playlists, this allowing users to loop
through songs continuously.
 Browsers use circular linked lists to manage the cache. This allows you to navigate back
through your browsing history efficiently by pressing the BACK button.
STACK :
A Stack is a linear data structure that follows a particular order in which the operations are
performed. The order may be LIFO(Last In First Out) or FILO(First In Last
Out). LIFO implies that the element that is inserted last, comes out first and FILO implies
that the element that is inserted first, comes out last.

A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. It
behaves like a stack of plates, where the last plate added is the first one to be removed.
Think of it this way:
 Pushing an element onto the stack is like adding a new plate on top.
 Popping an element removes the top plate from the stack.
Key Operations on Stack Data Structures
 Push: Adds an element to the top of the stack.
 Pop: Removes the top element from the stack.
 Peek: Returns the top element without removing it.
 IsEmpty: Checks if the stack is empty.
 IsFull: Checks if the stack is full (in case of fixed-size arrays).
Applications of Stack Data Structures
 Recursion
 Expression Evaluation and Parsing
 Depth-First Search (DFS)
 Undo/Redo Operations
 Browser History
 Function Calls

You might also like