DSA Chapter 3 - Linked Lists -All Parts
DSA Chapter 3 - Linked Lists -All Parts
Structures
and
Algorithms
C H A P TE R TH R E E :
LIN K E D LIS TS
▪ Skip Lists.
2.Link field: points to the next node(holds the address of the next
node)
▪It forms a chain of "nodes" with pointers representing the links of the
chain and holding the entire thing together.
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 7
Introduction to Linked List
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 8
Introduction to Linked List
Start
Null
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 9
Introduction to Linked List
▪This linked list has four nodes in it, each with a link to the
next node in the series.
▪The last node has a link to the special value NULL, which
any pointer (whatever its type) can point to, to show that it
is the last link in the chain.
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 11
Types of Linked Lists
NULL
1 data Link data Link
Link Link
3 data data
Link Link
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 12
4 Things When Building Linked
List
1. Structure of a Linked List
▪Data Element: This is the actual value or data stored in each node of the linked list. It can
be of any data type (e.g., integer, string, object).
▪Link Field (Pointer): This is a reference (or pointer) to the next node in the list. In a singly
linked list, each node points to the next node. In a doubly linked list, each node points to both
the next and the previous node.
▪The number of nodes in the list is equal to the number of elements in the list.
▪To traverse the list, start at the head and follow the next pointers until you reach a node
4. Basic operations:
1. Insertion:
▪At the Beginning: Update the new node’s next to point to the current head, then update
▪At the End: Traverse to the last node, then update its next to point to the new node.
▪In the Middle: Traverse to the desired position, update the new node’s next to point to the
next node, and update the previous node’s next to point to the new node.
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 14
4 Things When Building Linked
List
4. Basic operations:
2.Deletion:
▪At the Beginning: Update the head to point to the second node.
▪At the End:Traverse to the second-to-last node and set its next to None.
▪In the Middle: Traverse to the node before the one to be deleted, and
update its next to skip the node to be deleted.
3.Search (Find):
▪Traverse the list and compare each node’s data with the target value.
Return the node if found, otherwise return None.
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 15
Types of Linked Lists:
Singly Linked Lists
▪ In C++,linked list can be defined using structures and pointers.
▪ Each structure represents a node having some data and also a pointer that holds
the address of the next node and creates the link between two nodes.
▪ Nodes can be located anywhere in memory, as they are connected via links.
▪ The simplest form of linked list is the singly linked list.
▪ In a singly linked list, each node contains some data and a single link to its
successor in the list.
▪ Note that only one variable p can be used to access any node in the list.
▪ The last node in the list is indicated by having a null pointer (backslash ‘\’ symbol
to denotes the null pointer).
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 16
Types of Linked Lists:
Singly Linked Lists: Syntax
struct nodeType {
▪Suppose that current is a pointer of the same type as the pointer head. Then the
statement
▪current = head; copies the value of head into current.
▪Now consider the following statement:
▪current = current->link; This statement copies the value of current->link, which is
2800, into current.
▪Therefore, after this statement executes, current points to the second node in the list.
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 20
Types of Linked Lists:
Singly Linked Lists: Some Properties
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 21
Types of Linked Lists:
Singly Linked Lists: Some Properties
▪Values of current, head, and some of the nodes of the linked
list
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 22
Types of Linked Lists:
Singly Linked Lists: Traversal
▪The basic operations of a linked list are as follows:
the list,
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 24
Types of Linked Lists:
Singly Linked Lists: Traversal
▪ For example, suppose that head points to a linked list
of numbers.
▪ The following code outputs the data stored in each
node:
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 25
Types of Linked Lists:
Singly Linked Lists: Insertion and Deletion
▪Consider the following definition of a node.
struct nodeType
{
int info;
nodeType *link;
};
newNode->link = p->link;
p->link = newNode;
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 28
Types of Linked Lists:
Singly Linked Lists: Insertion
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 29
Types of Linked Lists:
Singly Linked Lists: Insertion at the Middle
▪Note that the sequence of statements to insert the node, that is,
▪ newNode->link = p->link;
▪ p->link = newNode;
▪is very important because to insert newNode in the list we only use one pointer, p, to
adjust the links of the nodes of the linked list.
▪Suppose that we reverse the sequence of the statements and execute the statements in
the following order:
▪ p->link = newNode;
▪ newNode->link = p->link;
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 30
Types of Linked Lists:
Singly Linked Lists: Insertion at the Beginning
▪In this operation, a new node is created and set as the new head.
▪Create a new node.
▪Set its next pointer to the current head.
▪Update the head to point to the new node.
Node* insertAtBeginning(Node* head, int value) {
Node* newNode = createNode(value);
newNode->next = head;
return newNode; // New head
}
▪Example Usage: head = insertAtBeginning(head, 10);
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 31
Types of Linked Lists:
Singly Linked Lists: Insertion at the End
▪In this operation, inserts a node at the last position.
▪Traverse to the last node (where next is nullptr).
▪Create a new node and set its next pointer to nullptr.
▪Update the next pointer of the last node to point to the new node.
void insertAtEnd(int value) { Node* current = head;
Node* newNode = new Node(); while (current->next != nullptr) {
newNode->data = value; current = current->next;
newNode->next = nullptr; }
if (head == nullptr) { current->next = newNode;
head = newNode; }
} }
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 32
Types of Linked Lists:
Singly Linked Lists: Deletion in the Middle
▪Suppose that the node with info 34 is to be deleted from the list.
▪The following statement removes the node from the list:
p->link = p->link->link;
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 33
Types of Linked Lists:
Singly Linked Lists: Deletion in the Middle
▪From the Figure above, it is clear that the node with info 34 is removed from
the list.
▪However, the memory is still occupied by this node and this memory is
inaccessible; that is, this node is dangling. To deallocate the memory, we
need a pointer to this node.
▪The following statements delete the node from the list and deallocate the
memory occupied by this node:
q = p->link;
p->link = q->link;
delete q;
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 34
Types of Linked Lists:
Singly Linked Lists: Deletion in the Middle
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 35
Types of Linked Lists:
Singly Linked Lists: Deletion at the Beginning
▪Removes the first node and updates the head.
▪Update the head to point to the second node.
▪Free the memory of the deleted node.
Node* deleteAtBeginning(Node* head) {
if (head == nullptr) return nullptr;
Node* temp = head;
head = head->next;
delete temp;
return head;
}
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 36
Types of Linked Lists:
Singly Linked Lists: Deletion at the Last Position
▪Removes the last node.
▪Traverse to the second-to-last node.
▪Update its next pointer to nullptr.
▪Free the memory of the deleted node.
Node* deleteAtEnd(Node* head) { Node* temp = head;
2.Pointer to the next node – Links the current node to the next node.
3.Pointer to the previous node – Links the current node to the previous node.
▪ A doubly linked list is one where there are links from each node in both directions (previous
node and next).
▪ There are two NULL: at the first and last nodes in the list
▪ Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists
backwards
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 42
Types of Linked Lists:
Doubly Linked List (DLL): Declaration
struct NodeType {
ItemType info;
NodeType * next;
NodeType * back;
};
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 43
Types of Linked Lists:
Doubly Linked List (DLL): Insertion
data
data
data
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 44
Types of Linked Lists:
Doubly Linked List (DLL): Deletion
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 45
Types of Linked Lists:
Doubly Linked List (DLL): Insertion: Example
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 46
Types of Linked Lists:
Doubly Linked List (DLL): Insertion: Example
1. newNode->back = location->back;
2. newNode->next = location
3. location->back->next=newNode;
4. location->back = newNode;
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 47
Types of Linked Lists:
Doubly Linked List (DLL): Deletion
head current
oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
delete oldNode;
current = head;
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 48
Types of Linked Lists:
Doubly Linked List (DLL): Operations
/*
* Node Declaration
*/
struct node
int data;
node *next;
node *prev;
};
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 49
Types of Linked Lists:
Doubly Linked List (DLL): Operations
/* Function Declarations or Prototypes */
void displayMenu();
void createList(Node** head, Node** tail);
void insertAtBeginning(Node** head, Node** tail);
void insertAtEnd(Node** head, Node** tail);
void insertAfter(Node** head, Node** tail);
void deleteFromBeginning(Node**head, Node** tail);
void deleteFromEnd(Node** head, Node** tail);
void deleteNode(Node** head, Node** tail);
void search(Node* head);
void displayForward(Node* head);
void displayBackward(Node* tail);
bool isEmpty(Node* head);
int countElements(Node* head);
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 50
Types of Linked Lists:
Doubly Linked List (DLL): Operations
/* The Main Function */ case 5: deleteFromBeginning(&head,
int main() { &tail); break;
case 6: deleteFromEnd(&head, &tail);
Node* head = nullptr;
break;
Node* tail = nullptr; case 7: deleteNode(&head, &tail);
int choice; break;
do { case 8: search(head); break;
displayMenu(); case 9: displayForward(head); break;
cout << "Enter your choice: "; case 10: displayBackward(tail); break;
cin >> choice; case 11: cout << "Number of elements: " <<
countElements(head); break;
switch(choice) { case 0: cout << "Exiting program...\n";
case 1: createList(&head, &tail); break; break;
case 2: insertAtBeginning(&head, &tail); default: cout << "Invalid choice! Try
break; again.\n"; }
case 3: insertAtEnd(&head, &tail); break; } while(choice != 0);
case 4: insertAfter(&head, &tail); break; return 0; }
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 51
Types of Linked Lists:
Doubly Linked List (DLL): Operations
/* The Display Function */
void displayMenu() {
cout << "\nDoubly Linked List Operations:\n";
cout << "1. Create/Reset List\n";
cout << "2. Insert at Beginning\n";
cout << "3. Insert at End\n";
cout << "4. Insert After a Node\n";
cout << "5. Delete from Beginning\n";
cout << "6. Delete from End\n";
cout << "7. Delete a Specific Node\n";
cout << "8. Search for a Node\n";
cout << "9. Display Forward\n";
cout << "10. Display Backward\n";
cout << "11. Count Elements\n";
cout << "0. Exit\n"; }
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 52
Types of Linked Lists:
Doubly Linked List (DLL): Operations
/* The creating the list and check if the list empty */
void createList(Node** head, Node** tail) {
// Delete existing list if any
while(*head != nullptr) {
Node* temp = *head;
*head = (*head)->next;
delete temp; }
*tail = nullptr;
cout << "New empty list created.\n"; }
bool isEmpty(Node* head) {
if(head == nullptr) {
cout << "List is empty!\n";
return true; }
return false;
}
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 53
Types of Linked Lists:
Doubly Linked List (DLL): Operations
/* Insert at the beginning*/
void insertAtBeginning(Node** head, Node** tail) {
int val;
cout << "Enter value to insert at beginning: ";
cin >> val;
Node* newNode = new Node{val, nullptr, *head};
if(*head == nullptr) {
*tail = newNode;
} else {
(*head)->prev = newNode; }
*head = newNode;
cout << val << " inserted at beginning.\n"; }
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 54
Types of Linked Lists:
Doubly Linked List (DLL): Operations
/* Insert at the end*/
void insertAtEnd(Node** head, Node** tail) {
int val;
cout << "Enter value to insert at end: ";
cin >> val;
Node* newNode = new Node{val, *tail, nullptr};
if(*head == nullptr) {
*head = newNode;
} else {
(*tail)->next = newNode; }
*tail = newNode;
cout << val << " inserted at end.\n";}
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 55
Types of Linked Lists:
Doubly Linked List (DLL): Operations
/* Insert at the middle*/
void insertAfter(Node** head, Node** tail) {
if(isEmpty(*head)) return;
int target, val;
cout << "Enter the value after which to insert: ";
cin >> target;
cout << "Enter value to insert: ";
cin >> val;
Node* current = *head;
while(current != nullptr && current->data != target) {
current = current->next; }
if(current == nullptr) {
cout << target << " not found in list.\n";
return; }
Node* newNode = new Node{val, current, current->next};
if(current->next != nullptr) {
current->next->prev = newNode;
} else {
*tail = newNode; } // Update tail if inserting at end
current->next = newNode;
cout << val << " inserted after " << target << ".\n"; }
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 56
Types of Linked Lists:
Doubly Linked List (DLL): Operations
/* Delete from beginning*/
void deleteFromBeginning(Node** head, Node** tail) {
if(isEmpty(*head)) return;
Node* temp = *head;
*head = (*head)->next;
if(*head != nullptr) {
(*head)->prev = nullptr;
} else {
*tail = nullptr; } // List is now empty
cout << temp->data << " deleted from beginning.\n";
delete temp;
}
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 57
Types of Linked Lists:
Doubly Linked List (DLL): Operations
/* Delete from end*/
void deleteFromEnd(Node** head, Node** tail) {
if(isEmpty(*head)) return;
Node* temp = *tail;
*tail = (*tail)->prev;
if(*tail != nullptr) {
(*tail)->next = nullptr;
} else {
*head = nullptr;} // List is now empty
cout << temp->data << " deleted from end.\n";
delete temp; }
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 58
Types of Linked Lists:
Doubly Linked List (DLL): Operations
/* Delete from middle*/
void deleteNode(Node** head, Node** tail) {
if(isEmpty(*head)) return;
int val;
cout << "Enter value to delete: ";
cin >> val;
Node* current = *head;
while(current != nullptr && current->data != val) {
current = current->next; }
if(current == nullptr) {
cout << val << " not found in list.\n";
return; }
if(current == *head) {
deleteFromBeginning(head, tail);
} else if(current == *tail) {
deleteFromEnd(head, tail);
} else {
current->prev->next = current->next;
current->next->prev = current->prev;
cout << current->data << " deleted from list.\n";
delete current; } }
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 59
Types of Linked Lists:
Doubly Linked List (DLL): Operations
/* Search for an element*/
void search(Node* head) {
if(isEmpty(head)) return;
int val;
cout << "Enter value to search: ";
cin >> val;
Node* current = head;
int position = 1;
while(current != nullptr) {
if(current->data == val) {
cout << val << " found at position " << position << ".\n";
return; }
current = current->next;
position++; }
cout << val << " not found in list.\n"; }
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 60
Types of Linked Lists:
Doubly Linked List (DLL): Operations
/* Display all elements from start to end */
void displayForward(Node* head) {
if(isEmpty(head)) return;
/*
* Number of elements in Doubly Link List
*/
int countElements(Node* head) {
int count = 0;
Node* current = head;
while (current != nullptr) {
count++;
current = current->next;
}
return count;
}
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 63
CIRCULAR LINKED LIST
(CLL)
Types of Linked Lists:
Circular Linked List (CLL)
▪A Circular Linked List (CLL) is a variation of a linked list in which the last node points back to the first node,
forming a circle. There are two types of circular linked lists:
1.Singly Circular Linked List – Each node has a next pointer, and the last node points to the first
node.
▪ Traversal is only forward.
2.Doubly Circular Linked List – Each node has both next and prev pointers, forming a two-way
circular connection.
▪ The head->prev points to the tail, and tail->next points to head.
▪ Allows bidirectional traversal.
▪Real-World Examples:
1. Music Playlist (SCLL): Songs loop back to the first track when the last song ends.
2. Multiplayer Game Turns (DCLL): Players take turns in a loop, and the game can move forward or
backward.
3. CPU Scheduling (Round-Robin Algorithm) – Tasks are executed in a cyclic order.
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 65
Types of Linked Lists:
Singly Circular Linked List (SCLL): Operations
1. Create the List
▪ Initialize head = nullptr.
2. Insert & Delete from Beginning
1. Insert:
▪ New node’s next points to head.
▪ Last node’s next updates to new node.
▪ Update head to new node.
2.Delete:
▪ Traverse to last node, update its next to head->next.
▪ Delete head and update head to head->next.
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 66
Types of Linked Lists:
Singly Circular Linked List (SCLL): Operations
2.Delete:
▪ Locate node, update previous node’s next to skip the node.
2. Delete:
▪ Traverse until the element is found (loop stops when back at head).
4/14/2025 Data Structures and Algorithms Chapter Three: Linked Lists By: Kibru G. 69
Types of Linked Lists:
Doubly Circular Linked List (DCLL): Operations
2. Delete: