Unit 2 Linked List
Unit 2 Linked List
5. Implement Non-Linear Data Structures like Trees and graphs using programming language.
3. Apply and analyze use of stack and queue with their applications
delete temp;
return head;
}
Deletion at the end of Singly Linked List:
To delete the last node, traverse the list until the second-to-last node and update its next field to None.
Steps-
1. Check if the head is NULL.
a. If it is, return NULL (the list is empty).
2. Check if the head’s next is NULL (only one node in the list).
a. If true, delete the head and return NULL.
3. Traverse the list to find the second last node (second_last).
4. Delete the last node (the node after second_last).
5. Set the next pointer of the second last node to NULL.
6. Return the head of the linked list.
Deletion at the end of Singly Linked List:
// C++ Function to remove the last node of the linked list
Node* removeLastNode(struct Node* head)
{
if (head == NULL)
return NULL;
if (head->next == NULL) {
delete head;
return NULL;
}
// Find the second last node
Node* second_last = head;
while (second_last->next->next != NULL)
second_last = second_last->next;
// Delete last node
delete (second_last->next);
// Change next of second last
second_last->next = NULL;
return head; }
Deletion at the specific position of Singly Linked List:
To delete a node at a specific position, traverse the list to the desired position, update the links to bypass the
node to be deleted.
Steps-
1. Check if the list is empty or the position is invalid, return if so.
2. If the head needs to be deleted, update the head and delete the node.
3. Traverse to the node before the position to be deleted.
4. If the position is out of range, return.
5. Store the node to be deleted.
6. Update the links to bypass the node.
7. Delete the stored node.
Deletion at specific location of Singly Linked List:
void deleteAtPosition(Node** head, int position)
{
// If the list is empty or the position is invalid
if (*head == nullptr || position < 1) {
return;
}
// Constructor
Node(int d) {
data = d;
prev = next = nullptr;
}
};
Operations on Doubly Linked Lists
Operations on Doubly Linked List
● Insertion:
○ Insert at the beginning
○ Insert at the end
○ Insert at a specific position
● Deletion:
○ Delete from the beginning
○ Delete from the end
○ Delete a specific node
● Traversal - 1)Forward Traversal 2)Backward Traversal
● Searching - looking for a specific element or value within the elements of the linked list.
● Length - determining the total number of nodes in a singly linked list.
Insertion Operation on Doubly Linked Lists
Insertion of a node in Doubly Linked List
● Insert a node at the beginning of a Doubly Linked List:
Steps-
1. Create a new node, say new_node with the given data and set its previous pointer to null,
new_node->prev = NULL.
2. Set the next pointer of new_node to current head, new_node->next = head.
3. If the linked list is not empty, update the previous pointer of the current head to new_node,
head->prev = new_node.
4. Return new_node as the head of the updated linked list.
Insert a node at the beginning of a Doubly Linked List:
// Insert a node at the beginning
Node* insertBegin(Node* head, int data) {
// If the linked list is empty, set the new node as the head of linked list
if (head == NULL) {
head = new_node;
}
else {
Node *curr = head;
while (curr->next != NULL) {
curr = curr->next;
}
head = new_node; // Set the new node as the head of linked list
return head;
}
// If the new node is not the last node, update prev of next node to new node
if (new_node->next != NULL)
new_node->next->prev = new_node;
// If empty, return
if (head == nullptr)
return nullptr;
if (head == NULL)
return NULL;
if (head->next == NULL) {
delete head;
return NULL;
}
// Traverse to the last node
Node *curr = head;
while (curr->next != NULL)
curr = curr->next;
b. Backward Traversal:
● Initialize a pointer to the tail of the linked list.
● While the pointer is not null:
○ Visit the data at the current node.
○ Move the pointer to the previous node.
Forward Traversal in Doubly Linked List
// Function to traverse the doubly linked list in forward direction
void forwardTraversal(Node* head) {
○
Applications of Doubly Linked List
● Implementation of undo and redo functionality in text editors.
● Cache implementation where quick insertion and deletion of elements are required.
● Browser history management to navigate back and forth between visited pages.
● Music player applications to manage playlists and navigate through songs efficiently.
● Implementing data structures like Deque (double-ended queue) for efficient insertion and
deletion at both ends.
Circular Linked List
● In singly linked lists and doubly linked lists, the end of lists are indicated with NULL value.
But circular linked lists do not have ends.
● 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. This means that you can keep
traversing the list without ever reaching a NULL value.
● The first element points to the last element and the last element points to the first element.
Both Singly Linked List and Doubly Linked List can be made into a circular linked list.
○
Types of Circular Linked List
Circular linked list can be created from both singly linked lists and doubly linked lists. So, there
are two types of linked lists-
.
Circular Singly Linked List
● In a circular Singly linked list, the last node of the list contains a pointer to the first node of
the list and this results in forming a circle
● In a circular Singly linked list,we can only move through the list in one direction.
● We traverse a circular singly linked list until we reach the same node where we started. The
circular singly linked list has no beginning and no ending. There is no null value present in
the next part of any of the nodes.
Memory Representation of Circular Singly Linked List
Memory Representation of Circular Singly Linked List
● Here, memory representation of a circular linked list containing marks of a student in 4
subjects.
● The start or head of the list is pointing to the element with the index 1 and containing 13
marks in the data part and 4 in the next part. Which means that it is linked with the node
that is being stored at 4th index of the list.
● However, due to the fact that we are considering circular linked list in the memory therefore
the last node of the list contains the address of the first node of the list.
● The last node is identified by its next part which contains the address of the start node of
the list.
Basic Operations on Circular Linked List
● Insertion:
○ Insert at the beginning
○ Insert at the end
○ Insert at a specific/given position
● Deletion:
○ Delete from the beginning
○ Delete from the end
○ Delete a specific node
● Searching
Advantages of Circular Linked List
● In circular linked list, the last node points to the first node. There are no null references,
making traversal easier and reducing the chances of encountering null pointer
exceptions.
● 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 List
● 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 List
● Circular Buffers (Ring Buffers): Circular linked lists are used to implement circular
buffers that are used in the systems where the data needs to be overwritten in cyclic
manner. They are commonly used in the buffering data streams.
● Round-Robin Scheduling: They are also used in operating systems to implement
Round-robin scheduling CPU scheduling algorithm.
● Implementing Undo Functionality:Circular linked lists are also used in various
applications to keep track of the sequence of activities in order to implement the undo
functionality.
● Music Playlists: Music players that supports the repeat modes uses the circular linked list
to cycle through the songs.
● Graphical User Interfaces: Circular linked lists are often used in various smart devices
like TV, smartwatches to implement the menu where the users can navigate through the
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.
Memory Representation of Circular Doubly Linked List
Memory Representation of Circular Singly Linked List
The diagram shows-
● The variable head contains the address of the first element of the list i.e. 1 hence the
starting node of the list contains data A is stored at address 1.
● Since, each node of the list is supposed to have three parts therefore, the starting node of
the list contains address of the last node i.e. 8 and the next node i.e. 4.
● The last node of the list that is stored at address 8 and containing data as 6, contains
address of the first node of the list as shown in the image i.e. 1.
● In circular doubly linked list, the last node is identified by the address of the first node
which is stored in the next part of the last node therefore the node which contains the
address of the first node, is actually the last node of the list.
Applications of Circular Doubly Linked List
● Implementation of Circular Data Structures: Circular doubly linked lists are extremely
helpful in the construction of circular data structures like circular queues and circular
buffers, which are both circular in nature.
● Implementing Undo-Redo Operations: Text editors and other software programs can use
circular doubly linked lists to implement undo-redo operations.
● Music Player Playlist: Playlists in music players are frequently implemented using
circular doubly linked lists. Each song is kept as a node in the list in this scenario, and the
list can be circled to play the songs in the order they are listed.
● Cache Memory Management: To maintain track of the most recently used cache blocks,
circular doubly linked lists are employed in cache memory management.
Difference between Circular SLL and Circular DLL
Learning Resources Text Books:
1. Herbert Schildt, “C++: The Complete Reference”, McGraw Hill Education, 2003.
2. John R. Hubbard, “Data Structures with C++”, Schaum’s Outlines, Tata McGraw Hill Education, 2000.
Reference Books:
1. Michael T. Goodrich, Roberto Tamassia, David Mount, “Data Structures and Algorithms in C++”, Wiley
India Pvt. Ltd., 2004.
2. Seymour Lipschutz, “Data Structures”, Schaum’s Outlines, Tata McGraw Hill Education, 2006
Online Resources/E-Learning Resources:
1. https://nptel.ac.in/courses/106102064 Data Structures and Algorithms, IIT Delhi Prof. Naveen Garg
Date of Reference 18-4-2024
2. https://nptel.ac.in/courses/106103069 Date of Reference 18-4-2024
3. https://www.geeksforgeeks.org/cpp-linked-list/
THANK YOU