Data Structure
BY: SAIFULLAH MANSOORI
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 1
Lecture Overview
Linked list.
Advantages and Disadvantages.
Types of Linked Lists.
Singly linked list.
Structure of a Node.
Insertion in singly linked list.
Traversal in singly linked list.
Deletion in singly linked list.
Search in singly linked list.
Reversal in singly linked list.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 2 /45
LinkedList
A linked list is a linear data structure where elements (called nodes) are stored in a non-
contiguous manner in memory.
Each element in the linked list contains data and a reference (or pointer) to the next element in
the list.
Unlike arrays, linked lists do not have a fixed size. This allows for dynamic memory allocation as
elements are added or removed.
The size of the linked list can grow or shrink at runtime, depending on the operations being
performed.
Head → [Data | Next] → [Data | Next] → [Data | NULL]
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 3 /45
LinkedList
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 4 /45
Advantages and Disadvantages
Advantages:
Dynamic Size – Can grow/shrink as needed.
Efficient Insertions/Deletions – Linked lists allow you to insert or remove nodes at any
position
Disadvantages:
Extra Memory – Requires additional space for pointers.
Slower Access – linked lists require linear-time (O(n)) traversal to access an element by its
position.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 5 /45
Types of Linked Lists
Singly Linked List
◦ Each node contains data and a pointer to the next node.
◦ Traversal is only in one direction (forward).
Doubly Linked List
◦ Each node contains data, a pointer to the next node, and a pointer to the previous node.
◦ Allows bidirectional traversal (forward and backward).
Circular Singly Linked List
◦ Like a singly linked list, but the last node points back to the first node, forming a circle.
◦ Traversal can continue indefinitely in one direction.
Circular Doubly Linked List
◦ Like a doubly linked list, but the last node links to the first node and the first node links to the last node.
◦ Supports continuous traversal in both directions.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 6 /45
Singly linked list
A singly linked list is a linked list where each node only points to one node and the tail node
points to NULL.
Each node contains data and a pointer to the next node.
Traversal is possible only in one direction (from head to tail).
Operations: Insertion, deletion, and traversal are easy but require searching for a specific node.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 7 /45
Structure of a Node
Node:
A node is the basic building block of a linked list. Each node contains data and a pointer to the
next node.
It consists of a collection of nodes, where each node stores
Data: The value stored in the node.
Next Reference: A pointer or reference to the next node in the list.
Head:
This a pointer or reference to the first node in the list.
It serves as the starting point for any operations that involve traversing or manipulating the list
Without the head, it would be impossible to access the rest of the nodes in the list.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 8 /45
Pseudocode for Node Structure
CLASS Node
DATA data
POINTER next
CONSTRUCTOR(data):
SET this.data = data
SET this.next = NULL
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 9 /45
Insertion Methods
Insertion Methods in a singly linked list are the operations that allow us to add new nodes to
the list.
There are several common scenarios in which we might want to insert a new node, and these
are typically categorized based on where the new node should be added
at the beginning.
at the end.
at a specific position within the list.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 10 /45
Insertion at the Beginning (Head)
Inserting a new node at the beginning of a singly linked list means adding a new element before
the current head.
Steps for Insertion at the Beginning
Create a new node with the given data.
Point the new node’s next to the current head, so it links to the existing list.
Update the head of the list to the new node
Example:
Before:10 → 20 →30
After inserting 100 at the beginning:100 → 10 → 20 → 30
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 11 /45
Insertion at the Beginning
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 12 /45
Pseudocode Insertion at the Beginning
METHOD INSERT_AT_BEGINNING(value)
CREATE newNode with data = value
SET newNode.next = head
SET head = newNode
IF Tail IS NULL THEN
SET Tail = newNode
END METHOD
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 13 /45
Insertion at the End (Tail)
Inserting at the end means adding a new node after the last node of the list.
Steps for Insertion at the end
Create a new node with the given data.
If the list is empty, set the new node as the head.
Otherwise, traverse the list to find the last node.
Attach the new node to the last node by updating its next pointer.
Example:
Before:10 → 20 → 30
After inserting 50 at the end:10 → 20 → 30 → 50
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 14 /45
Insertion at the End
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 15 /45
Pseudocode Insertion at the End (Tail)
METHOD INSERT_AT_END(value)
CREATE newNode with data = value
SET newNode.next = NULL
IF head IS NULL THEN
SET head = newNode
SET tail = newNode
ELSE
SET tail.next = newNode
SET tail = newNode
END IF
END METHOD
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 16 /45
Insertion at a Specific Position
A new node can be inserted at any given position in the list.
Steps for Insertion at a specific position
Create a new node with the given data.
If inserting at position 1, update the head to point to the new node.
Otherwise, traverse the list to the (position - 1)th node.
Adjust the next pointers so the new node fits in the correct place.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 17 /45
Insertion at a Specific Position
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 18 /45
Pseudocode Insertion at a Specific
Position
METHOD INSERT_AT_POSITION(position, value) SET current = head
IF position < 1 THEN SET counter = 1
PRINT "Invalid position! Position must be >= 1." WHILE counter < position - 1 AND current IS NOT NULL DO
RETURN SET current = current.next
CREATE newNode with data = value INCREMENT counter
IF position == 1 THEN IF current IS NULL THEN
SET newNode.next = head PRINT "Position out of bounds!"
SET head = newNode RETURN
IF tail IS NULL THEN SET newNode.next = current.next
SET tail = newNode SET current.next = newNode
END IF
IF newNode.next IS NULL THEN
RETURN
SET tail = newNode
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 19 /45
Traversal methods
Traversal in the context of a singly linked list refers to the process of visiting each node in the
list, typically to access or display its data.
Since a linked list is a linear data structure where each node points to the next node, traversal is
an essential operation for accessing all the elements in the list.
We can traverse a singly linked list in two way.
Traverse the List (Iterative):
Traverse the List (Recursive):
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 20 /45
Iterative Traversal
This is the most common way to traverse a singly linked list.
It uses a loop to go through each node one by one until the end of the list is reached (when the
next pointer of the node is NULL).
Steps to Iterative Traversal a Singly Linked List:
Start with the head node.
Access the data in the current node.
Move to the next node using the next pointer
Repeat steps 2 and 3 until the next node is NULL
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 21 /45
Pseudocode Iterative Traversal
METHOD TRAVERSE()
SET current = head
WHILE current is NOT NULL:
PRINT "->" + current.data
MOVE current = current.next
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 22 /45
Recursive Traversal
Traversal means visiting each node in a linked list and processing its data.
Using recursion, we can traverse a singly linked list by visiting the current node and making a
recursive call to process the next node until we reach the end (NULL).
Steps to Recursive Traversal a Singly Linked List:
Base Case: If the current node is NULL, return.
Process Current Node: Print or process the current node’s data.
Recursive Call: Call the function with the next node.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 23 /45
Pseudocode Recursive Traversal
METHOD RECURSIVE_TRAVERSE(head):
IF head IS NULL:
RETURN
PRINT "->" + head.data
CALL RECURSIVE_TRAVERSE(head.next)
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 24 /45
Deletion Methods
Deletion Methods in a singly linked list are the operations that allow us to remove nodes from
the list.
Deletion can be performed at various positions, such as
Delete from the beginning
Delete from the end.
Delete from a specific position.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 25 /45
Deletion from the Beginning (Head)
This operation removes the first node from the list and makes the second node the new head.
Steps for Deleting the First Node (Head):
Check if the list is empty (head == NULL). If it is, print a message indicating the list is empty and
return.
If the list is not empty, move the head pointer to the next node (head = head.next), effectively
removing the first node.
Print a message indicating that the first node has been deleted.
Example
Input : head : 3 -> 12 -> 15 -> 18 -> NULL
Output : 12 -> 15 -> 18 -> NULL
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 26 /45
Deletion from the Beginning (Head)
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 27 /45
Pseudocode Deletion from the Beginning
(Head)
METHOD DELETE_FIRST_NODE()
IF head IS NULL THEN
PRINT "List is empty."
RETURN
IF head.next IS NULL THEN
SET head = NULL
SET tail = NULL
PRINT "First node deleted."
RETURN
SET head = head.next
PRINT "First node deleted."
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 28 /45
Deleting the Last Node (Tail)
This operation removes the last node from the list. To do this, we need to traverse the list to find the
second-last node and set its next pointer to NULL.
Steps for Deleting the Last Node (Tail):
Check if the list is empty (head == NULL). If it is, print a message indicating the list is empty and
return.
If there is only one node in the list (head.next == NULL), set head = NULL to remove the only node in
the list.
If there are multiple nodes in the list, traverse the list to find the second-to-last node
(current.next.next == NULL).
Once the second-to-last node is found, set its next pointer to NULL, effectively removing the last node
from the list.
Print a message indicating that the last node has been deleted.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 29 /45
Deleting the Last Node (Tail)
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 30 /45
Pseudocode Deleting the Last Node (Tail)
METHOD DELETE_LAST_NODE() SET current = head
IF head IS NULL THEN WHILE current.next IS NOT NULL AND current.next.next IS NOT NULL DO
PRINT "List is empty." SET current = current.next
RETURN SET current.next = NULL
IF head.next IS NULL THEN SET tail = current
SET head = NULL PRINT "Last node deleted."
SET tail = NULL
PRINT "Last node deleted."
RETURN
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 31 /45
Deletion at a Specific Position
This operation removes a node at a specified position in the list. To perform this, we traverse
the list to find the node just before the one to be deleted, and then adjust the next pointer of
the previous node to skip the node being deleted.
Steps for Deleting a Node at a Specific Position:
traverse the list to find the node just before the position you want to delete (position - 1).
If the position is invalid (greater than the number of nodes or if current.next is NULL), print a
message indicating the position is out of range and return.
Once the node to be deleted is found, adjust the next pointer of the previous node (current.next
= current.next.next) to bypass the node to be deleted.
Print a message indicating that the node at the specified position has been deleted.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 32 /45
Deletion at a Specific Position
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 33 /45
Pseudocode Deletion at a Specific
Position
METHOD DELETE_NODE_AT_POSITION(position) WHILE current IS NOT NULL AND counter < position - 1 DO
IF head IS NULL THEN SET current = current.next
PRINT "List is empty." INCREMENT counter
RETURN END WHILE
IF current IS NULL OR current.next IS NULL THEN
IF position < 1 THEN
PRINT "Position out of range."
PRINT "Invalid position! Position must be >= 1."
RETURN
RETURN
SET temp = current.next
IF position == 1 THEN # Delete the first node
IF temp.next IS NULL THEN
CALL DELETE_FIRST_NODE()
SET tail = current
RETURN
SET current.next = temp.next
SET current = head PRINT "Node at position " + position + " deleted."
SET counter = 1
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 34 /45
Search Methods
Search methods are used to find a specific value or node in a singly linked list.
There are The two most common types of searches are searching by value (value search) or
searching by position (index search).
Search by Value: Traverses the list to find the first node that contains the specified value.
Search by Position: Traverses the list to find the node at a specified position.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 35 /45
Search by Value
Steps for Search by Value:
Start from the head of the list.
Traverse through the list node by node.
Compare the value of each node with the target value.
If the value is found, return the node or the position.
If the end of the list is reached without finding the value, return NULL or a message indicating
that the value is not found.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 36 /45
Pseudocode for Search by Value
METHOD SEARCH_BY_VALUE(value)
SET current = head
WHILE current IS NOT NULL DO
IF current.data == value THEN
PRINT "Value found."
RETURN
SET current = current.next
PRINT "Value not found."
RETURN
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 37 /45
Search by Position
Steps for Search by Position:
Start from the head of the list.
Traverse through the list to find the node at the specified position.
If the position is valid, return the node at that position.
If the position exceeds the length of the list, return NULL or a message indicating the position is
out of bounds.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 38 /45
Pseudocode for Search by Position
METHOD SEARCH_BY_POSITION(position)
IF position < 1 THEN
PRINT "Invalid position! Position must be >= 1."
RETURN
SET current = head
SET counter = 1
WHILE current IS NOT NULL AND counter < position DO
SET current = current.next
INCREMENT counter
IF current IS NULL THEN
PRINT "Position out of bounds."
RETURN
PRINT "Found data: " + current.data
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 39 /45
Update Method
Update methods allow you to modify the value of a node at a specific position in the list.
Steps for update method.
Traverse the list to the specified position.
Once the node is found, update its value with the new value.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 40 /45
Pseudocode for Update Method
METHOD REVERSE_LIST()
SET previous = NULL
SET current = head
SET tail = head
WHILE current IS NOT NULL DO
SET next = current.next
SET current.next = previous
SET previous = current
SET current = next
SET head = previous
PRINT "List reversed."
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 41 /45
Reversal Methods
The reversal method reverses the order of nodes in the linked list. It is done by changing the
next pointers of the nodes.
Steps for reversal methods
Initialize three pointers: previous as NULL, current as head, and next as NULL.
Traverse through the list, and for each node, update its next pointer to the previous node.
After traversing the entire list, set the head to the previous node (which will be the new head).
4/8/2025 COLLECTED BY: MOHAMMAD RAHIM TAHERI 42 /45
Pseudocode for Reversal Methods
METHOD REVERSE_LIST()
SET previous = NULL
SET current = head
SET tail = head
WHILE current IS NOT NULL DO
SET next = current.next
SET current.next = previous
SET previous = current
SET current = next
SET head = previous
PRINT "List reversed."
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 43 /45
Home work
Implement these missing method as your homework.
◦ detectCycle() – Check if the linked list has a loop/cycle.
◦ getSize() – Return the number of nodes in the list.
◦ isEmpty() – Check if the list is empty.
◦ findMiddle() – Find the middle node of the list.
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 44 /45
3/18/2025 COLLECTED BY: SAIFULLAH MANSOORI 45