SlideShare uma empresa Scribd logo
1 de 79
LINKED LISTS
DSA- CH-4: Linked List
CHAPTER FOUR
4/16/2024
1
CH-4 Contents
4/16/2024
2
1. Overview
2. Singly linked lists
3. Doubly linked lists
4. Circular lists
5. Other variant of lists
CH-4 Contents
4/16/2024
3
1. Overview
2. Singly linked lists
3. Doubly linked lists
4. Circular lists
5. Other variant of lists
Abstract Data type (ADT)
4/16/2024
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.
4/16/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
Overview: Linked List
4/16/2024
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.
4/16/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.
Array and linked lists
4/16/2024
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.
Self-Referential Structures
4/16/2024
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.
Defining the data structure for a
linked list
4/16/2024
10
 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:
4/16/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.
CH-4 Contents
4/16/2024
12
1. Overview
2. Singly linked lists
3. Doubly linked lists
4. Circular lists
5. Other variant of lists
Singly linked lists
4/16/2024
13
 Singly Linked Lists are a type of data structure,
type of list.
 In a 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
 It does not store any pointer or reference to the
previous node.
4/16/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.
Operations
4/16/2024
15
 In a single linked list we perform the following
operations
1. Insertion
2. Deletion
3. Display
4/16/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.
4/16/2024
17
4/16/2024
18
 Insertion
 In a 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
Inserting At Beginning of the
list
4/16/2024
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 = newNo
de.
 Step 4: If it is Not Empty then,
set newNode→next = head and head = newNod
e.
4/16/2024
20
Inserting At End of the list
4/16/2024
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.
4/16/2024
22
Inserting At Specific location in
the list (After a Node)
4/16/2024
23
 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'
4/16/2024
24
4/16/2024
25
 Deletion
 In a 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
Deleting from Beginning of the
list
4/16/2024
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.
4/16/2024
27
Deleting from End of the list
4/16/2024
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.
4/16/2024
29
Deleting a Specific Node from
the list
4/16/2024
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
4/16/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)).
4/16/2024
32
4/16/2024
33
4/16/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).
Exercise
4/16/2024
35
 Implement the above algorithm using C++.
Single Linked List: Exercises
Write a C++ program to create a linked list as shown below.
0 1 9

… …
head tail
4/16/2024
36
Basic code for linked list
4/16/2024
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;
CH-4 Contents
4/16/2024
38
1. Overview
2. Singly linked lists
3. Doubly linked lists
4. Circular lists
5. Other variant of lists
Introduction
10/23/2017
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.
WHY DOUBLY LINKED LIST
10/23/2017
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.
Operations
10/23/2017
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
 In a 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
Inserting At Beginning of the
list
10/23/2017
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
Inserting At End of the list
10/23/2017
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
Inserting At Specific location in
the list (After a Node)
10/23/2017
50
 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
 In a 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
Deleting from Beginning of the
list
10/23/2017
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
Deleting from End of the list
10/23/2017
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
Deleting a Specific Node from
the list
10/23/2017
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
 In a 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.
Displaying forward
10/23/2017
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
Displaying Backward
10/23/2017
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
CH-4 Contents
10/23/2017
66
1. Overview
2. Singly linked lists
3. Doubly linked lists
4. Circular lists
5. Other variant of lists
Introduction
10/23/2017
67
 Circular Linked List is a variation of Linked list
in which
 The first 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.
Singly Linked List as Circular
10/23/2017
68
 In singly linked list, the next pointer of the last
node points to the first node.
Doubly Linked List as Circular
10/23/2017
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.
Operations
10/23/2017
71
 Following are the important operations
supported by a circular list.
1. Insertion
2. Deletion
3. Display
Reading Assignment
10/23/2017
72
 Algorithm detail and implementation of
 Insertion
 Deletion
 Display
CH-4 Contents
10/23/2017
73
1. Overview
2. Singly linked lists
3. Doubly linked lists
4. Circular lists
5. Other variant of lists
Linked Lists Benefits &
Drawbacks
4/16/2024
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
Variants of Lists
4/16/2024
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.
4/16/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.
4/16/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.
4/16/2024
78
Questions?
4/16/2024
79
Thank You

Mais conteúdo relacionado

Semelhante a DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss

Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked listAmit Vats
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked listSumathi Kv
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data StructureMuhazzab Chouhadry
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversalkasthurimukila
 
Data Structures_Linked List
Data Structures_Linked ListData Structures_Linked List
Data Structures_Linked ListThenmozhiK5
 
Chapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdfChapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdfAxmedcarb
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptxssuserd2f031
 
data structures lists operation of lists
data structures lists operation of listsdata structures lists operation of lists
data structures lists operation of listsmuskans14
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and searchEstiak Khan
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm KristinaBorooah
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptxmsohail37
 

Semelhante a DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss (20)

single linked list
single linked listsingle linked list
single linked list
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
Data Structures_Linked List
Data Structures_Linked ListData Structures_Linked List
Data Structures_Linked List
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
 
Chapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdfChapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdf
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Cpp lernaufgabe linked_list
Cpp lernaufgabe linked_listCpp lernaufgabe linked_list
Cpp lernaufgabe linked_list
 
data structures lists operation of lists
data structures lists operation of listsdata structures lists operation of lists
data structures lists operation of lists
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and search
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 

Último

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 

Último (20)

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 

DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss

  • 1. LINKED LISTS DSA- CH-4: Linked List CHAPTER FOUR 4/16/2024 1
  • 2. CH-4 Contents 4/16/2024 2 1. Overview 2. Singly linked lists 3. Doubly linked lists 4. Circular lists 5. Other variant of lists
  • 3. CH-4 Contents 4/16/2024 3 1. Overview 2. Singly linked lists 3. Doubly linked lists 4. Circular lists 5. Other variant of lists
  • 4. Abstract Data type (ADT) 4/16/2024 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.
  • 5. 4/16/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
  • 6. Overview: Linked List 4/16/2024 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.
  • 7. 4/16/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.
  • 8. Array and linked lists 4/16/2024 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.
  • 9. Self-Referential Structures 4/16/2024 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.
  • 10. Defining the data structure for a linked list 4/16/2024 10  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:
  • 11. 4/16/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.
  • 12. CH-4 Contents 4/16/2024 12 1. Overview 2. Singly linked lists 3. Doubly linked lists 4. Circular lists 5. Other variant of lists
  • 13. Singly linked lists 4/16/2024 13  Singly Linked Lists are a type of data structure, type of list.  In a 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  It does not store any pointer or reference to the previous node.
  • 14. 4/16/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.
  • 15. Operations 4/16/2024 15  In a single linked list we perform the following operations 1. Insertion 2. Deletion 3. Display
  • 16. 4/16/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.
  • 18. 4/16/2024 18  Insertion  In a 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
  • 19. Inserting At Beginning of the list 4/16/2024 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 = newNo de.  Step 4: If it is Not Empty then, set newNode→next = head and head = newNod e.
  • 21. Inserting At End of the list 4/16/2024 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.
  • 23. Inserting At Specific location in the list (After a Node) 4/16/2024 23  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'
  • 25. 4/16/2024 25  Deletion  In a 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
  • 26. Deleting from Beginning of the list 4/16/2024 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.
  • 28. Deleting from End of the list 4/16/2024 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.
  • 30. Deleting a Specific Node from the list 4/16/2024 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
  • 31. 4/16/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)).
  • 34. 4/16/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).
  • 35. Exercise 4/16/2024 35  Implement the above algorithm using C++.
  • 36. Single Linked List: Exercises Write a C++ program to create a linked list as shown below. 0 1 9  … … head tail 4/16/2024 36
  • 37. Basic code for linked list 4/16/2024 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;
  • 38. CH-4 Contents 4/16/2024 38 1. Overview 2. Singly linked lists 3. Doubly linked lists 4. Circular lists 5. Other variant of lists
  • 39. Introduction 10/23/2017 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.
  • 40. WHY DOUBLY LINKED LIST 10/23/2017 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
  • 41. 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.
  • 42. Operations 10/23/2017 42  In a doubly linked list we perform the following operations 1. Insertion 2. Deletion 3. Display
  • 43. 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.
  • 45. 10/23/2017 45  Insertion  In a 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
  • 46. Inserting At Beginning of the list 10/23/2017 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
  • 48. Inserting At End of the list 10/23/2017 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
  • 50. Inserting At Specific location in the list (After a Node) 10/23/2017 50  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).
  • 51. 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
  • 53. 10/23/2017 53  Deletion  In a 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
  • 54. Deleting from Beginning of the list 10/23/2017 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.
  • 56. Deleting from End of the list 10/23/2017 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.
  • 58. Deleting a Specific Node from the list 10/23/2017 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
  • 59. 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)).
  • 61. 10/23/2017 61  Display  In a 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.
  • 62. Displaying forward 10/23/2017 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.
  • 64. Displaying Backward 10/23/2017 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.
  • 66. CH-4 Contents 10/23/2017 66 1. Overview 2. Singly linked lists 3. Doubly linked lists 4. Circular lists 5. Other variant of lists
  • 67. Introduction 10/23/2017 67  Circular Linked List is a variation of Linked list in which  The first 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.
  • 68. Singly Linked List as Circular 10/23/2017 68  In singly linked list, the next pointer of the last node points to the first node.
  • 69. Doubly Linked List as Circular 10/23/2017 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.
  • 70. 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.
  • 71. Operations 10/23/2017 71  Following are the important operations supported by a circular list. 1. Insertion 2. Deletion 3. Display
  • 72. Reading Assignment 10/23/2017 72  Algorithm detail and implementation of  Insertion  Deletion  Display
  • 73. CH-4 Contents 10/23/2017 73 1. Overview 2. Singly linked lists 3. Doubly linked lists 4. Circular lists 5. Other variant of lists
  • 74. Linked Lists Benefits & Drawbacks 4/16/2024 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
  • 75. Variants of Lists 4/16/2024 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.
  • 76. 4/16/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.
  • 77. 4/16/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.