Linked List, basics , types , operations

48 views 113 slides May 02, 2024
Slide 1
Slide 1 of 113
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104
Slide 105
105
Slide 106
106
Slide 107
107
Slide 108
108
Slide 109
109
Slide 110
110
Slide 111
111
Slide 112
112
Slide 113
113

About This Presentation

Presentation on types of Linked List , various operations on it


Slide Content

Linked List

Introduction A linked list is a data structure in which Successive elements are connected by pointers. Last element points to NULL . It can grow or shrink in size during execution of a program. 2 A B C head

Keeping track of a linked list: Must know the pointer to the first element of the list (called start , head , etc.). Linked lists provide flexibility in allowing the items to be rearranged efficiently. Insert an element. Delete an element.

Array vs Linked list

Creating a node

To be called from main as

Types of Linked list 3 types Singly Linked List(SLL) Doubly Linked List(DLL) Circular Linked List(CLL)

Insertion in a SLL 3cases in this Insertion at the beginning Insertion at the end Insertion after a particular node

Insertion in a SLL Insertion at the beginning Step 1:- Make the new ptr of the node point towards the first node of the list Step2:- Make the head ptr point towards this new node * If the list is empty(means this is gng to be the 1 st node then, simply make head ptr point towards new node )

Insertion in a SLL Insertion at the beginning

Insertion in a SLL Insertion at the end Simply need to make the next ptr of the last node point to the new node instead of NULL

Insertion in a SLL Insertion at the end

Insertion in a SLL Insertion after a particular node Step 1:- Make the next ptr of the node to be inserted point to the next node of the node before which you wanna insert the node Step2:- Make the next ptr of the node after which the node is to be inserted ,point to the node to be inserted

Insertion in a SLL Insertion after a particular node

Deletion in a SLL

Deletion in a SLL 3cases in this Deletion from the beginning Deletion from the end Deletion of an intermediate node

Deletion in a SLL Deleting the first node in SLL Step 1:- Make the start ptr point towards the 2 nd node Step2:- Deleting the 1 st node using DELETE keyword.

Deletion in a SLL Deleting the first node in SLL

Deletion in a SLL Deleting the last node in SLL Step 1:- Make the second last node’s next ptr point to NULL Step2:- Deleting the last node using DELETE keyword.

Deletion in a SLL Deleting the last node in SLL

Deletion in a SLL Deleting a particular node in SLL Step 1:- Make the next ptr of the node previous to the node being deleted point to the successor node of the node to be deleted Step2:- Deleting the particular node using DELETE keyword.

Deletion in a SLL Deleting a particular node in SLL

Searching in a SLL

Searching in a SLL Finding required element in the list 2 ways Linear Much easier in case of linked list Binary Complex as random traversal is not easy

Linear search in a SLL Each node is traversed till the data in the node matches with the required value.

Doubly Linked List

Doubly Linked List Linked data structure that consists of a set of sequentially linked records called nodes Contains 3 fields An integer value Link to the next node Link to the previous node

Structure of a DLL

Traversing in a DLL

Searching in a DLL

Insertion in a DLL

Doubly Linked List--Insertion Insertion at the beginning Node to be added Existing linked list

Doubly Linked List--Insertion Insertion at the beginning 1 st step:- Make it point to head

Doubly Linked List--Insertion Insertion at the beginning 2 nd step:- make prev point to temp

Doubly Linked List—Insertion at beginning

Insertion at the beginning in a DLL

Insertion at the end in a DLL 1 st step:- Traverse the list Update next part of last node Update prev part of temp node How to do this?

Insertion at the end in a DLL 1 st step:- take a pointer tp which initially points to 1 st node and later last node Code for tp to traverse each node

Insertion at the end in a DLL tp is now pointing the last node using the code--- traversal done

Insertion at the end in a DLL Step 2:- Attach a new node to end node of the list Step:- make this point to prev of temp node code

Insertion at the end in a DLL

Insertion at the end in a DLL

Insertion at the end in a DLL

Insertion at the end in a DLL

Insertion after a node in a DLL

Insertion after a node in a DLL 4000 newP There must be a pointer pointing to this new node We need a ptr pointing to this 2 nd node

Insertion after a node in a DLL temp will point to the 2 nd node as per this code

Insertion after a node in a DLL Now we have pointers pointing position node and after node of the desired location to insert

Insertion after a node in a DLL Make this ptr point to prev of new node code

Insertion after a node in a DLL Now prev part of this node needs to point next of new node code

Insertion after a node in a DLL Prev of this node points to next of temp and next of this node points to prev of temp2 code

Insertion after a node in a DLL

Insertion after a node in a DLL

Deletion in a DLL

Deletion from beginning in a DLL

Deletion from beginning in a DLL Step1:-Let a temp node point the head node

Deletion from beginning in a DLL Step2:- let head point to next node now code

Deletion from beginning in a DLL Step:-Remove the temp node

Deletion from beginning in a DLL Step:- Assign NULL to temp and prev of head node

Deletion from beginning in a DLL Method 2

Deletion from beginning in a DLL

Deletion from particular position in a DLL

Deletion from a particular position in a DLL Initially temp is pointing to head and moving towards the position to be deleted

Deletion from a particular position in a DLL Step:- take 1 more ptr to point to node previous to temp

Deletion from a particular position in a DLL Step:- next of temp2 should now point to next of temp

Deletion from a particular position in a DLL Step:- update this so that it points to next of temp2

Deletion from a particular position in a DLL Step:- free memory of temp pointer

Deletion from a particular position in a DLL

Deletion from a particular position in a DLL

Delete last node from a DLL

Delete last node from a DLL Traverse the list till last using temp pointer

Delete last node from a DLL Step:- storing this node in a temp2 While(temp-> NEXT !=NULL) { temp = temp->NEXT; } temp2 = temp-> prev ; temp2->NEXT = NULL; Step:-Now this should point to NULL

Delete last node from a DLL While(temp-> NEXT !=NULL) { temp = temp->NEXT; } temp2 = temp-> prev ; temp2->NEXT = NULL; free(temp); temp=NULL;

Delete last node from a DLL

Delete last node from a DLL

Circular Linked List

Circular Linked List Variation of LL in which last element points to 1 st element Both SLL and DLL can be converted to CLL.

Singly Linked List as Circular In SLL , the next pointer of last node points to 1 st node

Doubly Linked List as Circular In DLL , the nest pointer of last node points to 1 st node and previous pointer of 1 st node points to last node making the circular in both direction

Advantages of CLL 1.Any node can be a starting point.We can traverse the whole list by starting from any point 2.

Disadvantages of CLL 1. inserting at start of list would require doing a search for last node ----expensive 2. Finding end of the list and loop control is harder ( no NULL’s to mark the beginning and end )

Circular Linked List--Operations Insertion Deletion Display

Circular Linked List--Insertion Insertion At the beginning At the end At a particular position

Circular Linked List—Insertion at beginning Tail pointer is the pointer to the last node of the linked list Why this tail pointer is needed? Next slide

Circular Linked List—Insertion at beginning Consider a CLL of 3 nodes head A ptr is must for this node it’s NEXT can point to new node No need to traverse the whole list to find last node

Circular Linked List—Insertion at beginning Step:- Update this ptr Let it point to NEXT ptr of tail

Circular Linked List—Insertion at beginning Step:- Update the NEXT of tail ptr

Circular Linked List– Insertion at end Step:- update this ptr and let it point to the first node So NEXT ptr of new node takes the current address which tail ptr’s NEXT has (to point the first node)

Circular Linked List– Insertion at end Step:- this ptr needs to point to the new node Tail ptr needs to point to new node now Code

Circular Linked List– Insertion at end Final result

Circular Linked List– Insertion between nodes Initial State

Circular Linked List– Insertion between nodes Step:- a ptr is needed to point this node as we need to reach here

Circular Linked List– Insertion between nodes Step:- this ptr needs to be updated

Circular Linked List– Insertion between nodes Step:- NEXT of this node will point to new node now code

Circular Linked List– Insertion between nodes If pos =3(similar to inserting at the end,with slight changes)

Circular Linked List--Deletion Deletion At the beginning At the end At a particular position

CLL – Deletion of first node

CLL – Deletion of first node To delete, we are making a temp ptr point to first node

CLL – Deletion of first node code

CLL – Deletion of first node Step:- let’s free this node now

CLL – Deletion of last node

CLL – Deletion of last node For this, we take a temp ptr and make it point to 1 st node initially. This code is to make temp reach the 2 nd node so that we can update the NEXT ptr of this node point to 1000

CLL – Deletion of last node The loop has made the temp ptr reach here

CLL – Deletion of last node

CLL – Deletion of intermediate node

CLL – Deletion of intermediate node

CLL – Deletion of intermediate node Temp reaches here due to this loop in the code We need something to point to this position So, taking another variable

CLL – Deletion of intermediate node Code for setting position of temp2

CLL – Deletion of intermediate node

CLL – Deletion of intermediate node free(temp2)

CLL – Deletion of intermediate node

GTU questions Explain creation,insertion and deletion of a doubly linked list with example.[07] Write and explain algorithm for deletion in a singly linked list [07] Write and explain algorithm for insertion in doubly linked list[07] Write an algorithm to insert a node in a circular linked list at the first position[07] Write user defined ‘C’ function to insert node at a specific location in singly linked list[03] Write user defined ‘C’ function to delete node from end in circular linked list[04] Design an algorithm to merge two linked list[07]
Tags