0% found this document useful (0 votes)
55 views

Unit 2 Linked List

Uploaded by

collabswithnav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Unit 2 Linked List

Uploaded by

collabswithnav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

1

PCET’S Pimpri Chinchwad University

Department of Computer Science and Engineering


Course Name : Data Structures and Algorithms
Course Code/Course Type : UBTCE201/PCC
SY. B.Tech

Prepared By: Ms. Rupali Samarth Zambre

PIMPRI CHINCHWAD UNIVERSITY


2

Course Objectives (CO):


The objectives of Data Structures and Algorithms are:

1. To study different data structures and analysis of algorithms.

2. To gain the knowledge about the concept of linked list.

3. To use and apply the concept of stack and queue

4. To categorize the use of searching and sorting techniques.

5. Implement Non-Linear Data Structures like Trees and graphs using programming language.

PIMPRI CHINCHWAD UNIVERSITY


3

Course Learning Outcomes (CLO):


Students would be able to:

1. To use data structure concepts and analyze the algorithms.

2. Apply and analyze use of linked list as a data structure.

3. Apply and analyze use of stack and queue with their applications

4. Perform operations like searching, insertion, deletion, traversing mechanism etc. on


various data structures.

5. Apply advanced data structure strategies to solve real world problems.


UNIT- II
Linked List
UNIT II Hours-9
Understanding the basics of Linked List, Comparison between array and linked list.
Types and basic operations of Linked Lists: 1. Single Linked List. 2. Double Linked List. 3.
Circular Linked List. 4. Circular Doubly Linked List.
Basic operations: Creation. Insertion. Deletion, Traversing.
Introduction to Linked Lists
Linked List
● Linked List is a linear data structure which looks like a series of nodes, where each node has
two parts: data and next pointer.
● Unlike Arrays, Linked List elements are not stored at a contiguous location. In the linked list
there is a head pointer, which points to the first element of the linked list, and if the list is empty
then it simply points to null or nothing.
Linked List
● Head: The Head of a linked list is a pointer to the first node or reference of the first node of
linked list. This pointer marks the beginning of the linked list.
● Node: Linked List consists of a series of nodes where each node has two parts: data and next
pointer.
● Data: Data is the part of node which stores the information in the linked list.
● Next pointer: Next pointer is the part of the node which points to the next node of the linked
list.
Advantages of Linked List
● Dynamic Data structure: The size of memory can be allocated or de-allocated at run time
based on the operation insertion or deletion.
● Ease of Insertion/Deletion: The insertion and deletion of elements are simpler than arrays since
no elements need to be shifted after insertion and deletion, Just the address needed to be
updated.
● Efficient Memory Utilization: As we know Linked List is a dynamic data structure the size
increases or decreases as per the requirement so this avoids the wastage of memory.
● Implementation: Various advanced data structures can be implemented using a linked list like a
stack, queue, graph, hash maps, etc.
Arrays versus Linked Lists
Array Vs Linked List
• In arrays,
• elements are stored in a contiguous memory locations
• Arrays are static data structure unless we use dynamic memory
allocation
• Arrays are suitable for
▪ Inserting/deleting an element at the end.
▪ Randomly accessing any element.
▪ Searching the list for a particular value.
Array Vs Linked List
• In Linked lists,
• adjacency between any two elements are maintained by means of
links or pointers
• It is essentially a dynamic data structure.
• Linked lists are suitable for
▪ Inserting an element at any position.
▪ Deleting an element from anywhere.
▪ Applications where sequential access is required.
▪ In situations, where the number of elements cannot be predicted
beforehand.
Linked List: Non-Contiguous Storage
Types of Linked Lists
Types of Linked List
Depending on the way in which the links are used to maintain adjacency, several
different types of linked lists are possible.

● Single/Singly Linked List


● Double/Doubly Linked List
● Circular Linked List
○ Circular Singly Linked List
○ Circular Doubly Linked List
Single/ Singly Linked Lists
Singly Linked List
● It is a collection of nodes where each node contains a data field and a reference
(link) to the next node in the sequence.
● The last node in the list points to null, indicating the end of the list.
● This linear data structure allows for efficient insertion and deletion operations,
making it a popular choice for various applications.
Singly Linked List
● Node Structure:-
1)Each node consists of two parts:
1) data - stores the actual information
2) a pointer to the next node. - stores the address of the next node in the sequence.
2)This structure allows nodes to be dynamically linked together, forming a chain-like
sequence.
3)In this representation, each box represents a node, with an arrow indicating the link to the
next node. The last node points to NULL, indicating the end of the list.
Defining a node-Singly Linked List
In C++, a node in a singly linked list is typically defined using a class or a struct.

class Node { struct Node {


public: // Data part of the node
// Data part of the node int data;
int data; // Pointer to the next node in the list
// Pointer to the next node in the list Node* next;
Node* next;
// Constructor to initialize the node with data Node(int new_data)
Node(int data) {
{ data = new_data;
this->data = data; next = nullptr;
this->next = NULL; }
} };
};
Operations on Singly Linked Lists
Operations on Singly 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 - visiting each node in the linked list and performing some operation on the data.A simple
traversal function would print or process the data of each node.
● 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 in Singly Linked Lists
Insertion of a node in Singly Linked List
● Insert a node at the beginning / front of a Linked List:
Steps-
1.Create a new node with the given value.
2. Set the next pointer of the new node to the current head.
3. Move the head to point to the new node.
4. Return the new head of the linked list.
Insert a node at the beginning / front of a Linked List:
// C++ function to insert a new node at the beginning of the linked list
Node* insertAtBeginning(Node* head, int value)
{
// Create a new node with the given value
Node* newNode = new Node(value);
// Set the next pointer of the new node to the current head
newNode->next = head;

// Move the head to point to the new node


head = newNode;

// Return the new head of the linked list


return head;
}
Insert a node at the end of Linked List
● To insert a node at the end of the list, traverse the list until the last node is reached, and then link
the new node to the current last node.-
Steps-
1. Create a new node with the given value.
2. Check if the list is empty:
a.If it is, make the new node the head and return.
3. Traverse the list until the last node is reached.
4. Link the new node to the current last node by setting the last node’s next pointer to the new
node.
Insert a node at the end of a Linked List:
Insert a node at the end of a Linked List:
// C++ Function to insert a node at the end of the linked list

void insertAtEnd(Node** head, int value)


{
Node* newNode = new Node(value); // Create a new node with the given value
if (*head == nullptr) { // If the list is empty, make the new node the head
*head = newNode;
return;
}
// Traverse the list until the last node is reached
Node* current = *head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode; // Link the new node to the current last node
}
Insert a node at a specific location in Singly Linked List
● To insert a node at a specific position, traverse the list to the desired position, link the new node to
the next node, and update the links accordingly.
Steps-
1. Check if the given position is valid.
a. If invalid, print “Invalid position!” and exit.
2. Loop until pos reaches 0:
a. If pos is 0:
i. Create a new node with the given data.
ii. Set the new node’s next pointer to the current node.
iii. Update the current node to the new node.
b. Else, move to the next node by updating the double pointer.
3. Increment the size of the linked list.
Insert a node at specific location in a Linked List:
Insert a node at specific location in a Linked List:

// C++ function to insert a Node at specified position


void insertPos(Node** current, int pos, int data)
{
// This condition to check whether the position given is valid or not.
if (pos < 1 || pos > size + 1)
cout << "Invalid position!" << endl;
else {
while (pos--) { // Keep looping until the pos is zero
if (pos == 0) {
// adding Node at required position
Node* temp = getNode(data);
// Making the new Node to point to the old Node at the same position
temp->next = *current;
// Changing the pointer of the Node previous to the old Node to point to the new Node
*current = temp;
}
else
// Assign double pointer variable to point to the pointer pointing to the address of next Node
current = &(*current)->next;
}
size++;
}
}
Deletion in Singly Linked Lists
Deletion at beginning of Singly Linked List:
To delete the first node, update the head to point to the second node in the list.
Steps-
1. Check if the head is NULL.
a. If it is, return NULL (the list is empty).
2. Store the current head node in a temporary variable temp.
3. Move the head pointer to the next node.
4. Delete the temporary node.
5. Return the new head of the linked list.
Delete a node from beginning of a Linked List:
// C++ Function to remove the first node of the linked list
Node* removeFirstNode(struct Node* head)
{
if (head == NULL)
return NULL;
// Move the head pointer to the next node
Node* temp = head;
head = head->next;

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;
}

// If the head needs to be deleted


if (position == 1) {
Node* temp = *head;
*head = (*head)->next;
delete temp;
return;
}

// Traverse to the node before the position to be deleted


Node* current = *head;
for (int i = 1; i < position - 1 && current != NULL;
i++) {
current = current->next;
}
Deletion at the specific location of Singly Linked List:

// If the position is out of range


if (current == NULL || current->next == NULL) {
return;
}

// Store the node to be deleted


Node* temp = current->next;

// Update the links to bypass the node to be deleted


current->next = current->next->next;

// Delete the node


delete temp;
}
Traversal in Singly Linked List:
Traversal involves visiting each node in the linked list and performing some operation on the data. A
simple traversal function would print or process the data of each node.
Steps-
● Initialize a pointer current to the head of the list.
● Use a while loop to iterate through the list until the current pointer reaches NULL.
● Inside the loop, print the data of the current node and move the current pointer to the next
node.
Traversal in Singly Linked List:
// C++ Function to traverse and print the elements of the linked
// list
void traverseLinkedList(Node* head)
{
// Start from the head of the linked list
Node* current = head;

// Traverse the linked list until reaching the end


// (nullptr)
while (current != NULL) {

// Print the data of the current node


cout << current->data << " ";

// Move to the next node


current = current->next;
}

cout << std::endl;


}
Applications of Linked List
● Linked Lists can be used to implement stacks, queue, deque, sparse matrices and
adjacency list representation of graphs.
● Dynamic memory allocation in operating systems and compilers (linked list of free
blocks).
● Manipulation of polynomials.
● Arithmetic operations on long integers.
● In operating systems, they can be used in Memory management, process scheduling (for
example circular linked list for round robin scheduling) and file system.
● Algorithms that need to frequently insert or delete items from large collections of data.
● LRU cache, which uses a doubly linked list to keep track of the most recently used items
in a cache.
Double/ Doubly Linked Lists
Doubly Linked List
● A doubly linked list is a data structure that consists of a set of nodes, each of which
contains a value and two pointers, one pointing to the previous node in the list and
one pointing to the next node in the list.
● This allows for efficient traversal of the list in both directions, making it suitable for
applications where frequent insertions and deletions are required.
Doubly Linked List Representation
In a data structure, a doubly linked list is represented using nodes that have three fields:
1. Data
2. A pointer to the next node (next)
3. A pointer to the previous node (prev)
Define a node in Doubly Linked List
struct Node {
// To store the Value or data.
int data;
// Pointer to point the Previous Element
Node* prev;
// Pointer to point the Next Element
Node* next;

// 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) {

// Create a new node


Node* new_node = new Node(data);

// Make next of it as head


new_node->next = head;

// Set previous of head as new node


if (head != NULL) {
head->prev = new_node;
}

// Return new node as new head


return new_node;
}
Insertion of a node at the end in Doubly Linked List
● Steps-
1. Allocate memory for a new node and assign the provided value to its data field.
2. Initialize the next pointer of the new node to nullptr.
3. If the list is empty:
a. Set the previous pointer of the new node to nullptr.
b. Update the head pointer to point to the new node.
4. If the list is not empty:
a. Traverse the list starting from the head to reach the last node.
b. Set the next pointer of the last node to point to the new node.
c. Set the previous pointer of the new node to point to the last node.
Insertion of a node at the end in Doubly Linked List
Insert a node at the end of a Doubly Linked List:
Node *insertEnd(Node *head, int new_data) {

Node *new_node = new Node(new_data); // Create a new node

// 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;
}

curr->next = new_node; // Set the next of last node to new node

new_node->prev = curr; // Set prev of new node to last node


}
return head; // Return the head of the doubly linked list
}
Insertion at a specific position in Linked List
Steps:
1. If position = 1, create a new node and make it the head of the linked list and return it.
2. Otherwise, traverse the list to reach the node at position – 1, say curr.
3. If the position is valid, create a new node with given data, say new_node.
4. Update the next pointer of new node to the next of current node and prev pointer of new
node to current node, new_node->next = curr->next and new_node->prev = curr.
5. Similarly, update next pointer of current node to the new node, curr->next = new_node.
6. If the new node is not the last node, update prev pointer of new node’s next to the new
node, new_node->next->prev = new_node.
Insertion of a node at the specific position in Doubly Linked List
Insert a node at the specific position of a Doubly Linked List:
// Function to insert a new node at a given position
Node *insertAtPosition(Node *head, int pos, int new_data) {

Node *new_node = new Node(new_data); // Create a new node

if (pos == 1) { // Insertion at the beginning


new_node->next = head;
// If the linked list is not empty, set the prev of head to new node
if (head != NULL)
head->prev = new_node;

head = new_node; // Set the new node as the head of linked list
return head;
}

Node *curr = head;


// Traverse the list to find the node before the insertion point
for (int i = 1; i < pos - 1 && curr != NULL; ++i) {
curr = curr->next;
}
Insert a node at the specific position of a Doubly Linked List:
// If the position is out of bounds
if (curr == NULL) {
cout << "Position is out of bounds." << endl;
delete new_node;
return head;
}

new_node->prev = curr; // Set the prev of new node to curr

new_node->next = curr->next; // Set the new of new node to next of curr

// Update the next of current node to new node


curr->next = new_node;

// 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;

// Return the head of the doubly linked list


return head;
}
Deletion Operation on Doubly Linked Lists
Deletion at beginning of Doubly Linked List
Steps:
1. Check if the list is empty, there is nothing to delete. Return.
2. Store the head pointer in a variable, say temp.
3. Update the head of linked list to the node next to the current head, head = head->next.
4. If the new head is not NULL, update the previous pointer of new head to NULL,
head->prev = NULL.
Delete a node from beginning of Doubly Linked List
// Deletes the first node (head) of the list and returns the second node as new head
Node *delHead(Node *head) {

// If empty, return
if (head == nullptr)
return nullptr;

// Store in temp for deletion later


Node *temp = head;

// Move head to the next node


head = head->next;

// Set prev of the new head


if (head != nullptr)
head->prev = nullptr;

// Free memory and return new head


delete temp;
return head;
}
Deletion at end of Doubly Linked List
Steps:
1. Check if the doubly linked list is empty. If it is empty, then there is nothing to delete.
2. If the list is not empty, then move to the last node of the doubly linked list, say curr.
3. Update the second-to-last node’s next pointer to NULL, curr->prev->next = NULL.
4. Free the memory allocated for the node that was deleted.
Delete a node from end of Doubly Linked List
// Function to delete the last node of the doubly linked list
Node *delLast(Node *head) {

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;

// Update the previous node's next pointer


curr->prev->next = NULL;

// Delete the last node


delete curr;

// Return the updated head


return head;
}
Deletion at specific position in Doubly Linked List
Steps:
1. Traverse to the node at the specified position, say curr.
2. If the position is valid, adjust the pointers to skip the node to be deleted.
a. If curr is not the head of the linked list, update the next pointer of the node
before curr to point to the node after curr, curr->prev->next = curr-next.
b. If curr is not the last node of the linked list, update the previous pointer of
the node after curr to the node before curr, curr->next->prev =
curr->prev.
3. Free the memory allocated for the deleted node.
Deletion at specific position in Doubly Linked List
Deletion at specific position in Doubly Linked List
// Function to delete a node at a specific position in the doubly linked list
Node * delPos(Node * head, int pos) {

if (!head) // If the list is empty


return head;

Node * curr = head;

// Traverse to the node at the given position


for (int i = 1; curr && i < pos; ++i) {
curr = curr -> next;
}

if (!curr) // If the position is out of range


return head;

if (curr -> prev) // Update the previous node's next pointer


curr -> prev -> next = curr -> next;
Deletion at specific position in Doubly Linked List
if (curr -> next) // Update the next node's prev pointer
curr -> next -> prev = curr -> prev;

// If the node to be deleted is the head node


if (head == curr)
head = curr -> next;

// Deallocate memory for the deleted node


delete curr;
return head;
}
Traversal in Doubly Linked List
a. Forward Traversal:
● Initialize a pointer to the head of the linked list.
● While the pointer is not null:
○ Visit the data at the current node.
○ Move the pointer to the next node.

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) {

// Start traversal from the head of the list


Node* curr = head;

// Continue until current node is not null


// (end of list)
while (curr != nullptr) {

// Output data of the current node


cout << curr->data << " ";

// Move to the next node


curr = curr->next;
}

// Print newline after traversal


cout << endl;
}
Backward Traversal in Doubly Linked List
// Function to traverse the doubly linked list in backward direction
void backwardTraversal(Node* tail) {

// Start traversal from the tail of the list


Node* curr = tail;

// Continue until current node is not null


// (end of list)
while (curr != nullptr) {

// Output data of the current node


cout << curr->data << " ";

// Move to the previous node


curr = curr->prev;
}

// Print newline after traversal


cout << endl;
}
Advantages of Doubly Linked List
● Efficient traversal in both directions: Doubly linked lists allow for efficient traversal of
the list in both directions, making it suitable for applications where frequent insertions
and deletions are required.
● Easy insertion and deletion of nodes: The presence of pointers to both the previous
and next nodes makes it easy to insert or delete nodes from the list, without having to
traverse the entire list.
● Can be used to implement a stack or queue: Doubly linked lists can be used to
implement both stacks and queues, which are common data structures used in
programming.
Disadvantages of Doubly Linked List
● More complex than singly linked lists: Doubly linked lists are more complex than singly
linked lists, as they require additional pointers for each node.
● More memory overhead: Doubly linked lists require more memory overhead than singly
linked lists, as each node stores two pointers instead of one.


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-

1. Circular Singly Linked List


2. Circular Doubly Linked List

.
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

You might also like