Download as pdf or txt
Download as pdf or txt
You are on page 1of 79

1

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

 Abstract Data type (ADT) is a type (or class) for


objects whose behavior is defined by a set of
value and a set of operations.
 The definition of ADT only mentions what operations
are to be performed but not how these operations will
be implemented.
 It does not specify how data will be organized in
memory and what algorithms will be used for
implementing the operations.

3/2/2024
5

 It is called “abstract” because it gives an


implementation-independent view.
 The process of providing only the essentials and hiding
the details is known as abstraction.
 Think of ADT as a black box which hides the inner
structure and design of the data type. Now we’ll
define three ADTs namely
 List
ADT,
 Stack ADT,
 Queue ADT

3/2/2024
Overview: Linked List
6

 A linked list is a data structure which is built from


structures and pointers.
 It forms a chain of "nodes" with pointers representing the
links of the chain and holding the entire thing together.
 The elements of a linked list are called the nodes.
◼ A node has two fields i.e. data and next.
◼ The data field contains the data being stored in that specific
node. It cannot just be a single variable. There may be many
variables presenting the data section of a node.
◼ The next field contains the address of the next node. So this is the
place where the link between nodes is established.

3/2/2024
7

 Description of the above diagram


 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 to show
that it is the last link in the chain.
 There is also another special pointer, called Start or Root,
which points to the first link in the chain so that we can keep
track of it.

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

 Structures can hold pointers to instances of themselves.


struct list
{
char name[10];
int count;
struct list *next; // instance of list
};
 However, structures cannot contain instances of
themselves.
 Linked lists are the most basic self-referential
structures.

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.

 Here I have given the structure of a typical node:

3/2/2024
11

 The important part of the structure is the line before


the closing curly brackets.
 This gives a pointer to the next node in the list.
 This is the only case in C++ where you are allowed to
refer to a data type (in this case node) before you
have even finished defining it!
 We have also declared a pointer called start_ptr
that will permanently point to the start of the list.
 To start with, there are no nodes in the list, which is why
start_ptr is set to NULL.

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

 Singly Linked Lists are a type of data structure,


type of list.
 Ina singly linked list each node in the list stores the
contents of the node and a pointer or reference to the
next node in the list.
 A single linked list has a single link to another node
 Itdoes not store any pointer or reference to the
previous node.

3/2/2024
14

 In a single linked list, the address of the first node is


always stored in a reference node known as "front"
(Some times it is also known as "head").
 Always next part (reference part) of the last node
must be NULL.

3/2/2024
Operations
15

 In a single linked list we perform the following


operations
1. Insertion
2. Deletion
3. Display

3/2/2024
16

 Before we implement actual operations, first we


need to setup empty list.
 First perform the following steps before
implementing actual operations.
 Step 1: Include all the header files which are used in
the program.
 Step 2: Define a Node structure with two
members data and next
 Step 3: Define a Node pointer 'head' and set it
to NULL.

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

 We can use the following steps to insert a new node


at beginning of 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,
set newNode→next = head and head = newNode.

3/2/2024
20 3/2/2024
Inserting At End of the list
21

 We can use the following steps to insert a new node at


end of the single linked list...
 Step 1: Create a newNode with given value and newNode
→ next as NULL.
 Step 2: Check whether list is Empty (head == NULL).
 Step 3: If it is Empty then, set 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 last node in the list (until temp → next is
equal to NULL).
 Step 6: Set temp → next = newNode.

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

 We can use the following steps to delete a node from


beginning 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 a Node pointer 'temp' and
initialize with head.
 Step 4: Check whether list is having only one node (temp →
next == NULL)
 Step 5: If it is TRUE then set head = NULL and
delete temp (Setting Empty list conditions)
 Step 6: If it is FALSE then set head = temp → next, and
delete temp.

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

 Displaying a Single Linked List


 We can use the following steps to display the elements of a
single linked list...
◼ Step 1: Check whether list is Empty (head == NULL)
◼ Step 2: If it is Empty then, display 'List is Empty!!!' and terminate
the function.
◼ Step 3: If it is Not Empty then, define a Node pointer 'temp' and
initialize with head.
◼ Step 4: Keep displaying temp → data with an arrow (--->)
until temp reaches to the last node
◼ Step 5: Finally display temp → data with arrow pointing
to NULL (temp → data ---> NULL).

3/2/2024
Exercise
35

 Implement the above algorithm using C++.

3/2/2024
Single Linked List: Exercises
36

Write a C++ program to create a linked list as shown below.

head tail

…… 

0 1 9

3/2/2024
Basic code for linked list
37

 Create a new node


 Structure_Name *new_node= new Structure_Name;
 Student *S1= new Student;
 Getting data from the list
 S1->age; or S1->name;
 Checking whether the list empty or not
 If(head==NULL) else not empty
 Finding the last node
 temp=head;
 while(temp->next!=NULL)
◼ temp=temp->next;

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

 A doubly linked list is one in which all nodes are


linked together by multiple links
 Which help in accessing both the successor (next) and
predecessor (previous) node for any arbitrary node
within the list.
 Every nodes in the doubly linked list has three fields:
1. LeftPointer (previous)
2. RightPointer (next)
3. DATA.

10/23/2017
WHY DOUBLY LINKED LIST
40

 The only way to find the specific node that


precedes p is to start at the beginning of the list.
 The same problem arise when one wishes to delete an
arbitrary node from a singly linked list.
 If we have a problem in which moving in either
direction is often necessary, then it is useful to have
doubly linked lists.
 Each node now has two link data members,
 One linking in the forward direction
 One in the backward direction

10/23/2017
41

 Lpoint(previous) will point to the node in the left side (or


previous node)
 LPoint will hold the address of the previous node.
 Rpoint(next) will point to the node in the right side (or next
node)
 RPoint will hold the address of the next node.
 Data will hold the information of the node.

10/23/2017
Operations
42

 In a doubly linked list we perform the following


operations
1. Insertion
2. Deletion
3. Display

10/23/2017
43

 Before we implement actual operations, first we


need to setup empty list.
 First perform the following steps before
implementing actual operations.
 Step 1: Include all the header files which are used in
the program.
 Step 2: Define a Node structure with two
members data , next and previous
 Step 3: Define a Node pointer 'head' and ‘tail’ set it
to NULL.

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

 We can use the following steps to insert a new node


at beginning of the doubly linked list
 Step 1: Create a newNode with given value
and newNode → previous as NULL
 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then,set
◼ newNode→next = NULL, head =newNode
and tail =newNode.
 Step 4: If it is Not Empty then,
set newNode→next = head ,
head→previous = newNode and head =newNode
10/23/2017
47 10/23/2017
Inserting At End of the list
48

 We can use the following steps to insert a new node


at end of the doubly linked list
 Step 1: Create a newNode with given value
and newNode → next as NULL
 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then,set
◼ newNode→next = NULL, head =newNode
and tail =newNode.
 Step 4: If it is Not Empty then,
set newNode→previous = tail ,
tail→next = newNode and tail =newNode
10/23/2017
49 10/23/2017
Inserting At Specific location in the
50
list (After a Node)
 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, head =newNode
and tail =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).
10/23/2017
51

 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: Check whether temp → data is equal to location,
if it is TRUE go to step 8 otherwise terminate the function
 Step 8: Check whether temp is the last node, if yes
tail=newNode, if no (temp->next)->previous=newNode
 Step 9: Finally, Set 'newNode → next = temp → next' ,,
'temp → next = newNode‘ and newNode-
>previous=temp

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

 We can use the following steps to delete a node from


beginning of the doubly 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 a Node pointer 'temp' and
initialize with head.
 Step 4: Check whether list is having only one node (temp →
next == NULL)
 Step 5: If it is TRUE then set head = NULL , tail = NULL and
delete temp (Setting Empty list conditions)
 Step 6: If it is FALSE then set head = temp → next, (temp->
next)->previous=NULL and delete temp.

10/23/2017
55 10/23/2017
Deleting from End of the list
56

 We can use the following steps to delete a node from end


of the doubly 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 a Node pointer 'temp' and
initialize with tail.
 Step 4: Check whether list is having only one node (temp →
previous == NULL)
 Step 5: If it is TRUE then set head = NULL , tail = NULL and
delete temp (Setting Empty list conditions)
 Step 6: If it is FALSE then set tail = temp → previous, (temp->
previous)->next=NULL and delete temp.

10/23/2017
57 10/23/2017
Deleting a Specific Node from the list
58

 We can use the following steps to delete a specific node


from the doubly 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 Node pointers 'temp'
initialize with head.
 Step 4: Keep moving the temp until it reaches to the exact node
to be deleted or to the last node. And every time check
whether temp is reached to last node or not. If it is reached to
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

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

 We can use the following steps to display forward


the elements of a doubly linked list
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!!' and
terminate the function.
 Step 3: If it is Not Empty then, define a Node
pointer 'temp' and initialize with head.
 Step 4: Keep displaying temp → data with an arrow (-
-->) until temp==NULL, every time move the temp to
next node.
10/23/2017
63 10/23/2017
Displaying Backward
64

 We can use the following steps to display backward


the elements of a doubly linked list
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!!' and
terminate the function.
 Step 3: If it is Not Empty then, define a Node
pointer 'temp' and initialize with tail.
 Step 4: Keep displaying temp → data with an arrow (-
→) until temp==NULL, every time move the temp to
previous node.
10/23/2017
65 10/23/2017
CH-4 Contents
66

1. Overview
2. Singly linked lists
3. Doubly linked lists
4. Circular lists
5. Other variant of lists

10/23/2017
Introduction
67

 Circular Linked List is a variation of Linked list in


which
 Thefirst 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.

10/23/2017
Singly Linked List as Circular
68

 In singly linked list, the next pointer of the last node


points to the first node.

10/23/2017
Doubly Linked List as Circular
69

 In doubly linked list, the next pointer of the last


node points to the first node and the previous
pointer of the first node points to the last node
making the circular in both directions.

10/23/2017
70

 As per the above illustration, following are the


important points to be considered.
 The last link's next points to the first link of the list in
both cases of singly as well as doubly linked list.
 The first link's previous points to the last of the list in
case of doubly linked list.

10/23/2017
Operations
71

 Following are the important operations supported


by a circular list.
1. Insertion
2. Deletion
3. Display

10/23/2017
Reading Assignment
72

 Algorithm detail and implementation of


 Insertion

 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

 Self Organizing list


A Self Organizing list reorders its nodes based on
searches which are done.
◼ Singly and doubly linked lists require sequential search to
locate an element or to see that it is not in the list, we can
improve the efficiency of the search by dynamically
organizing the list in a certain manner.
◼ This organization depends on the configuration of data;
thus, the stream of data requires reorganizing the nodes
already on the list.
◼ There are many different ways to organize the lists, and this
section describes four of them.

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

You might also like