DSA Chapter 4
DSA Chapter 4
DSA Chapter 4
CHAPTER FOUR
LINKED LISTS
3/2/2024 DSA- CH-4: Linked List
CH-4 Contents
2
1. Overview
2. Singly linked lists
3. Doubly linked lists
4. Circular lists
5. Other variant of lists
3/2/2024
CH-4 Contents
3
1. Overview
2. Singly linked lists
3. Doubly linked lists
4. Circular lists
5. Other variant of lists
3/2/2024
Abstract Data type (ADT)
4
3/2/2024
5
3/2/2024
Overview: Linked List
6
3/2/2024
7
3/2/2024
Array and linked lists
8
Array
Arrays are simple and fast but we must specify their size at
construction time.
Drawbacks: If you construct an array with space for n,
tomorrow you may need n+1.
◼ Here comes a need for a more flexible system.
Linked Lists
Flexible space use by dynamically allocating space for each
element as needed.
This implies that one need not know the size of the list in
advance.
◼ Memory is efficiently utilized.
3/2/2024
Self-Referential Structures
9
3/2/2024
Defining the data structure for a
10
linked list
The key part of a linked list is
A structure, which holds the data for each node (the
name, age, height or whatever for the items in the list),
and,
Most importantly, a pointer to the next node.
3/2/2024
11
3/2/2024
CH-4 Contents
12
1. Overview
2. Singly linked lists
3. Doubly linked lists
4. Circular lists
5. Other variant of lists
3/2/2024
Singly linked lists
13
3/2/2024
14
3/2/2024
Operations
15
3/2/2024
16
3/2/2024
17 3/2/2024
18
Insertion
Ina single linked list, the insertion operation can be
performed in three ways. They are as follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
3/2/2024
Inserting At Beginning of the list
19
3/2/2024
20 3/2/2024
Inserting At End of the list
21
3/2/2024
22 3/2/2024
Inserting At Specific location in the
23
list (After a Node)
We can use the following steps to insert a new node after a node in the
single linked list...
Step 1: Create a newNode with given value.
Step 2: Check whether list is Empty (head == NULL)
Step 3: If it is Empty then, set newNode →
next = NULL and head = newNode.
Step 4: If it is Not Empty then, define a node pointer temp and initialize
with head.
Step 5: Keep moving the temp to its next node until it reaches to the node after
which we want to insert the newNode (until temp1 → data is equal to location,
here location is the node value after which we want to insert the newNode).
Step 6: Every time check whether temp is reached to last node or not. If it is
reached to last node then display 'Given node is not found in the list!!!
Insertion not possible!!!' and terminate the function. Otherwise move
the temp to next node.
Step 7: Finally, Set 'newNode → next = temp → next' and 'temp →
next = newNode'
3/2/2024
24 3/2/2024
25
Deletion
Ina single linked list, the deletion operation can be
performed in three ways. They are as follows...
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
3/2/2024
Deleting from Beginning of the list
26
3/2/2024
27 3/2/2024
Deleting from End of the list
28
We can use the following steps to delete a node from end of the
single linked list...
Step 1: Check whether list is Empty (head == NULL)
Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not
possible' and terminate the function.
Step 3: If it is Not Empty then, define two Node pointers 'temp1' and
'temp2' and initialize 'temp1' with head.
Step 4: Check whether list has only one Node (temp1 → next == NULL)
Step 5: If it is TRUE. Then, set head = NULL and delete temp1. And
terminate the function. (Setting Empty list condition)
Step 6: If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its
next node. Repeat the same until it reaches to the last node in the list.
(until temp1 → next == NULL)
Step 7: Finally, Set temp2 → next = NULL and delete temp1.
3/2/2024
29 3/2/2024
Deleting a Specific Node from the list
30
We can use the following steps to delete a specific node from the
single linked list...
Step 1: Check whether list is Empty (head == NULL)
Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not
possible' and terminate the function.
Step 3: If it is Not Empty then, define two Node pointers 'temp1' and
'temp2' and initialize 'temp1' with head.
Step 4: Keep moving the temp1 until it reaches to the exact node to be
deleted or to the last node. And every time set 'temp2 = temp1' before
moving the 'temp1' to its next node.
Step 5: If it is reached to the last node then display 'Given node not
found in the list! Deletion not possible!!!'. And terminate the function.
Step 6: If it is reached to the exact node which we want to delete, then
check whether list is having only one node or not
3/2/2024
31
Step 7: If list has only one node and that is the node to be
deleted, then set head = NULL and delete temp1 (free(temp1)).
Step 8: If list contains multiple nodes, then check whether temp1 is
the first node in the list (temp1 == head).
Step 9: If temp1 is the first node then move the head to the next
node (head = head → next) and delete temp1.
Step 10: If temp1 is not first node then check whether it is last
node in the list (temp1 → next == NULL).
Step 11: If temp1 is last node then set temp2 →
next = NULL and delete temp1 (free(temp1)).
Step 12: If temp1 is not first node and not last node then
set temp2 → next = temp1 → next and
delete temp1 (free(temp1)).
3/2/2024
32 3/2/2024
33 3/2/2024
34
3/2/2024
Exercise
35
3/2/2024
Single Linked List: Exercises
36
head tail
……
0 1 9
3/2/2024
Basic code for linked list
37
3/2/2024
CH-4 Contents
38
1. Overview
2. Singly linked lists
3. Doubly linked lists
4. Circular lists
5. Other variant of lists
3/2/2024
Introduction
39
10/23/2017
WHY DOUBLY LINKED LIST
40
10/23/2017
41
10/23/2017
Operations
42
10/23/2017
43
10/23/2017
44 10/23/2017
45
Insertion
Ina doubly linked list, the insertion operation can be
performed in three ways. They are as follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
10/23/2017
Inserting At Beginning of the list
46
10/23/2017
52 10/23/2017
53
Deletion
Ina doubly linked list, the deletion operation can be
performed in three ways. They are as follows
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
10/23/2017
Deleting from Beginning of the list
54
10/23/2017
55 10/23/2017
Deleting from End of the list
56
10/23/2017
57 10/23/2017
Deleting a Specific Node from the list
58
10/23/2017
59
Step 7: If list has only one node and that is the node to be
deleted, then set head = NULL, tail = NULL and
delete temp1 (free(temp)).
Step 8: If list contains multiple nodes, then check whether temp is
the first node in the list (temp == head).
Step 9: If temp is the first node then move the head to the next
node (head = head → next) , (temp->next)->previous=NULL
and delete temp.
Step 10: If temp1 is not first node then check whether it is last
node in the list (temp == tail).
Step 11: If temp1 is last node (tail= temp→ previous) , (temp-
>previous)->next =NULL and delete temp.
Step 12: If temp1 is not first node and not last node then
set (temp->previous)->next=temp->next, (temp->next)-
>previous=temp->previous and delete temp1 (free(temp1)).
10/23/2017
60 10/23/2017
61
Display
Ina doubly linked list, the display operation can be
performed in two ways. They are as follows
1. Display forward: Displays the complete list in a forward
manner.
2. Deleting backward: Displays the complete list in a
backward manner.
10/23/2017
Displaying forward
62
1. Overview
2. Singly linked lists
3. Doubly linked lists
4. Circular lists
5. Other variant of lists
10/23/2017
Introduction
67
10/23/2017
Singly Linked List as Circular
68
10/23/2017
Doubly Linked List as Circular
69
10/23/2017
70
10/23/2017
Operations
71
10/23/2017
Reading Assignment
72
Deletion
Display
10/23/2017
CH-4 Contents
73
1. Overview
2. Singly linked lists
3. Doubly linked lists
4. Circular lists
5. Other variant of lists
10/23/2017
Linked Lists Benefits & Drawbacks
74
Benefits
Easy to insert and delete in O(1) time
Don’t need to estimate total memory needed
Drawbacks
Hard to search in less than O(n) time (e.g. binary search
doesn’t work)
Hard to jump to the middle
Skip Lists
Fix
these drawbacks
Good data structure for a dictionary ADT
3/2/2024
Variants of Lists
75
Skip List
A skip list is a data structure that allows fast search within
an ordered sequence of elements.
◼ Skip lists are a randomized data structure
◼ Invented around 1990 by Bill Pugh
◼ Generalization of sorted linked lists - so simple to implement
◼ Expected search time is O(log n)
◼ Randomized data structure:
◼ Use random coin flips to build the data structure
Called skip lists because higher level lists let you skip over
many items.
3/2/2024
76
3/2/2024
77
Sparse Table
Sparse Table is a data structure, that allows answering
range queries.
Many times, we need to store information about stuff,
and a table is the natural way to store it
It can answer most range queries in O(log n), but its
true power is answering range minimum queries (or
equivalent range maximum queries).
◼ For those queries it can compute the answer in O(1) time.
3/2/2024
78
Questions?
3/2/2024
79
Thank You
3/2/2024