0% found this document useful (0 votes)
3 views19 pages

L5 Linked List

The document provides a laboratory guide for CSE 2106, focusing on the deletion of nodes in a doubly linked list. It covers the algorithms for deleting the last node, an intermediate node, and the first node, along with sample code for insertion and deletion processes. Additionally, it includes lab work exercises related to manipulating linked lists, such as merging nodes and deleting occurrences of values.

Uploaded by

janirafsan402
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views19 pages

L5 Linked List

The document provides a laboratory guide for CSE 2106, focusing on the deletion of nodes in a doubly linked list. It covers the algorithms for deleting the last node, an intermediate node, and the first node, along with sample code for insertion and deletion processes. Additionally, it includes lab work exercises related to manipulating linked lists, such as merging nodes and deleting occurrences of values.

Uploaded by

janirafsan402
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

CSE 2106: Data Structures and Algorithms Laboratory

Linked List
Doubly Linked List & Deletion
Dipannita Biswas
Md Mehrab Hossain Opi
Introduction 2
• Today we will learn how to remove a node from a linked list.
• But instead of using singly linked list, we will use doubly linked list
today.

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Doubly Linked List 3
• Remember doubly linked list?
• Instead of keeping a next pointer, we will keep two pointers in a node
now.
• prev and next.

struct node{
int data;
node *prev, *next;
};

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Insertion 4
• To delete nodes, at first we need to insert some data.
• As the lab is about deletion, we are providing the code to insert
elements (at the beginning).

void insert_first(int val, node* &head){


node* new_head = new node;
new_head ->data = val;
new_head->next = head;
head->prev = new_head;
head = new_head;
}

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Deletion 5
• Similar to insertion, we will classify our deletion in three types
• Delete the last node.
• Delete the first node.
• Delete any intermediate node.

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Last node Deletion 6
• Let’s start with last node Deletion.
• At first we need to find the last node.
• We can achieve this by traversing till the end.
• Now the previous of the last node should contain null.
• Also make sure to free the deleted node.

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Last Node Deletion 7

0x2 0x1 0x4 0x2 0x1 0x4 0x5 0x1


null 1 2 3 4 null 5 null
3 2 3 3 0 3 6 0
cur

Free
Initialize
Set
Traverse
next
the memory
cur
of
to ‘prev’
find
withthe
of
head
of last
tail
cur node
to null.

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Algorithm to Delete Last Node 8
Input: Linked List head
Procedure:
1. If head or head.next is null return null.
2. Initialize ‘cur’ to head.
3. While cur.next is not null:
1. Update current to current.next

4. Set current.prev.next to null


5. Deallocate memory

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Delete an Intermediate Node 9
• By an intermediate node, we mean there’s always a previous node and
a next node.
• In this case, we need to deal with three nodes.
• Suppose we want to delete the node ‘cur’.
• We need to update cur.prev and cur.next.
• Also we will need to delete the cur node.

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Delete an Intermediate Node 10

0x2 0x1 0x4


0x1 0x2 0x1 0x4
0x2 0x5 0x1
null 1 2 3 4 5 null
3 2 3
0 3 0 3 6 0

cur

Suppose
Deallocate
Set we
cur.prev.next
cur.next.prev
want
thetomemory
delete
to cur.next
cur.prev
the
of cur
value 3

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Algorithm to Delete an Intermediate Node 11
Input: Linked List head, Value to be Deleted
Procedure:
1. If head is null return null.
2. Initialize ‘cur’ to head.
3. While cur is not null and cur.data is not equal to target:
1. Update current to current.next

4. If cur is null return an error (value not found).


5. Set cur.prev.next to cur.next
6. Set cur.next.prev to cur.prev
7. Deallocate memory

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Delete the First Node 12
• Deleting the first node is fairly easier.
• First change head to head.next.
• Set head.prev to null.
• Deallocate the memory of the previous head.

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Deletion of First Node 13

head = 0x23
0x12

0x2 0x1 0x4 0x2 0x1 0x4 0x5 0x1


null 1 null 2 3 4 null 5 null
3 2 3 3 0 3 6 0
cur

Deallocate
Sethead
Set Set memory
head.prev
cur
= = of cur
to null
head.next
head

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Algorithm to Delete the First Node 14
Input: Linked List head
Procedure:
1. If head is null return null
2. Set cur = head
3. Set head to head.next
4. If head is not null set head.prev to null
5. Deallocate memory for cur

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Lab Work 15
• Given a linked list of integers.
• For every two consecutive 0's, merge all the nodes lying in between
them into a single node whose value is the sum of all the merged
nodes.
Input

0 1 2 0 3 6 4 0

Output

1
0 3 0 0
3

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Lab Work 16
• Instead of deleting the first occurrence, delete all the occurrence of a value.
• Insert the count of that value at the end.
Input
[1,2,3,1,3,2,4]
Delete 1: [2,3,3,2,4,2]
Delete 2: [3,3,4,3]
Delete 3: [4,3]

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Lab Work 17
• Given a doubly linked list of odd length (N)
• Insert node between node.
• Delete the last nodes.
• You can’t create any extra linked list.

Input
[1,2,3,4,5,6,7,8,9]
Output
[1,6,2,7,3,8,4,9,5]

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


Lab Work 18
• Implement the functions using XOR-List.
• Create a function named “delete_occurrence” that will delete all the
nodes with a given value.
Input
[1,2,3,1,3,2,4,1]
Delete 1: [2,3,3,2,4]
Delete 2: [3,3,4]
Delete 3: [4]

CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025


CSE 2106: Data Structures and Algorithms Laboratory 02/10/2025 19

Thank You

You might also like