DS and AA3
DS and AA3
Linked List
Limitations of
Array Linked List
Operations
Variations of Linked
List
Array vs Linked
List
2 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Limitations of www.foxitsoftware.com/shopping
Arrays
Arrays are stored in contiguous memory blocks. And have advantages
and disadvantages due to it.
It is very easy to access any data element from array using index
We need to know size of array before hand.
We cannot resize array. Because They are static in size
We can relocate existing array to new array, but still expensive
🞂 Contiguous block cannot be guaranteed insufficient blocks size
Insertion and deletion is very expensive because it needs shifting of elements
Solution: Linked list
A dynamic data structure in which each data element is linked with next element
through some link. Because each element is connected/linked, it will be easy to insert
and delete an element without shifting.
3 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Dynamic Data Structure www.foxitsoftware.com/shopping
Data structures whose size is not known and they can grow/shrink during use are
created using dynamic memory allocation concept.
data next
We always need location of the first node of list in order to process it for
any purpose
Head or Start node is reference which points to address of the first node of list.
Because all nodes are connected only through pointers, so if we have this head, any node
can
be accessed by traversal.
If head is NULL, it means list is empty
6 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Linked List www.foxitsoftware.com/shopping
Tail
7 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Node www.foxitsoftware.com/shopping
C++
struct Node{
int data;
Node Operations: Node* next;
}
Constructing a new node
Node* node=new Node
8 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Node examples www.foxitsoftware.com/shopping
private: public:
//methods }
}
9 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Linked List Memory Representation www.foxitsoftware.com/shopping
Linked list has nodes and memory has cells, nodes of list are distributed in
memory cells, each node is an object that is dynamically created at run time
and a free memory cell is allocated to that node.
Let say head node of a linked list is located at memory cell 1009. Its data is only
an
integer value.
It points to list’s 2nd node which is located at memory location 1001
2nd node points to 3rd node which is located at 1011.
1009 1001 1011
head 15 1001 78 1011 8 NULL
10 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Linked List Operations www.foxitsoftware.com/shopping
Traversal
Search, print, update etc.
Insertion
Deletion
11 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Search www.foxitsoftware.com/shopping
Algorithm: SEARCH(Head, V)
Input: reference to first node of list and value to be
searched
Output: return node if value is found other wise null
Steps:
startSet
1. ptr=Head
2. While (ptr != NULL)
3. if ptr.data==V
4. return ptr
5. end if
6. ptr=ptr.next // update ptr so it can refer to next
7. End While node
8. return NULL
end
12 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Search www.foxitsoftware.com/shopping
Algorithm: PRINT(Head)
Input: reference to first node of list
Output: print all nodes
Steps:
start
1. Set ptr=Head
2. While (ptr != NULL)
3. print ptr.data
4. ptr=ptr.next // update ptr so it can refer to next node
5. End While
end
13 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion www.foxitsoftware.com/shopping
14 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Start www.foxitsoftware.com/shopping
1099
data 1009
15 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Start www.foxitsoftware.com/shopping
Algorithm: INSERT_START(Head, V)
Input: head and data for new node
Output: list with new node inserted
Steps:
Start
1. newestNode = new Node(V) //create new node
2. If Head==NULL // list is empty
3. Head=newestNode
4. Else //list is not empty
5. newestNode.next=Head
6. Head=newestNode
7. End If
End
16 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Start (2) www.foxitsoftware.com/shopping
17 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at End www.foxitsoftware.com/shopping
18 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at End www.foxitsoftware.com/shopping
If list is not empty then we need to traverse the list to find the last node in order to link the newly crated node in existing list.
Algorithm: INSERT_END(Head, V)
Input: head node and data to be inserted
Output: list with new node inserted
Steps:
Start:
1. newestNode = new Node(V) //create new node
2. If Head==NULL // list is empty
3. Head=newestNode
4. Else //list is not empty. Search last node
5. Set ptr=Head
6. While(ptr.next!= NULL)
7. ptr=ptr.next
8. End While // loop will terminate when last node is found
9. ptr.next=newestNode // update the next pointer of ptr(last node)
10. End If
End
19 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Middle www.foxitsoftware.com/shopping
Now this will become 3rd node and new node will be inserted before this node.
20 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Middle www.foxitsoftware.com/shopping
prev curr
So when new node is inserted, we can easily change next links of new
node and previous node.
1009 1001 1099
head 9 1019 7 1099 11
1019 NULL
13 1001
21 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insert at Middle www.foxitsoftware.com/shopping
22 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insert at Middle (2) www.foxitsoftware.com/shopping
24 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Deletion www.foxitsoftware.com/shopping
25 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Deletion www.foxitsoftware.com/shopping
1001 1099
head 7 1099 11
NULL
Deletion From End
1009 1001 1099
head 9 1001 7 1099 11 NULL
1009 1001
head 9 1001 7 NULL
26 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
DELETE www.foxitsoftware.com/shopping
Algorithm: DELETE_END(Head)
Algorithm: Input: reference to first node
DELETE_START(Head) Output: new list with lat node deleted
Steps:
Input: reference to first Start:
node 1. If Head!=NULL// list is not empty
2. If Head.next==NULL// there is only one node
Output: new list with first 3. Head=NULL
node deleted 4. Else
5. Set Curr=Head, Prev=NULL
Steps:
1. If Head!=NULL// list is not 6. While (Curr.next!=NULL)
Startempty 7. Prev=Curr
8. Curr=Curr.next
2. Head=Head.next 9. End While
10. Prev.next=NULL
3. End If End If
End 11.
12.End
If End
27 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Deletion www.foxitsoftware.com/shopping
Deletion at Location
This case involves searching a specific node to delete
We also need to find current and previous node pointers in order to maintain links.
Current will be our required node. Let say we need to delete 2nd from following list
We need to search 2nd in list, and then need to find its previous node also, so before
deletion we can link previous node to next node of 2nd node
1009 1001 1099
head 9 1001 7 1099 11
NULL
prev curr
28 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Deletion www.foxitsoftware.com/shopping
prev curr
prev curr
1009 1099
head 9 1099 11
NULL
29 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
DELETE www.foxitsoftware.com/shopping
31 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Types of Linked List www.foxitsoftware.com/shopping
Depending upon how links are maintained, there can be variationsof linked
list:
Singly Linked List
We have discussed it already
Doubly
🞂
Linked List
Circular Linked
List
32 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Singly Linked List www.foxitsoftware.com/shopping
Every node contains only one next link which points to next node in
list 1009 1001 1099
head 9 1001 7 1099 11
NULL
33 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Doubly Linked List www.foxitsoftware.com/shopping
Every node contains two links, next which points to next node and previous
which points to previous node in list
1009 1001 1099
head 1099
NULL 1 0 079 1001 1009 10 011 1001 10 0195 NULL
Note that prev and next links hold address of nodes. So, prev link of node located at
1001 points to 1009 and next link points to 1099.
Previous link of first node is NULL
Next link of last node is NULL
Doubly linked list can be traversed
from start to end and from end to
start.
34 25/10/2020
If we have tail node
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Start www.foxitsoftware.com/shopping
Change links
1009 1001
head 1011 1001 NULL
1009 10 011
1007
9
1011
1009
NULL 1009
15
35 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Start www.foxitsoftware.com/shopping
36 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at End www.foxitsoftware.com/shopping
1009 1001
head NULL 1001
1009 1 0 011
10079 1011
1011
NULL
1001 1 009
15
37 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at End www.foxitsoftware.com/shopping
If list is not empty then we need to traverse the list to find the last node in order to link the newly crated node in existing list.
Algorithm: INSERT_END(Head, V)
Input: head node and data to be inserted
Output: list with new node inserted
Steps:
Start:
1. newestNode = new Node(V) //create new node
2. If Head==NULL // list is empty
3. Head=newestNode
4. Else //list is not empty. Search last node
5. Set ptr=Head
6. While(ptr.next!= NULL)
7. ptr=ptr.next
8. End While // loop will terminate when last node is found
9. ptr.next=newestNode // update the next pointer of ptr(last node)
10. newestNode.prev=ptr
11.End
If End
38 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insertion at Middle www.foxitsoftware.com/shopping
In case of doubly linked list, we only need to keep one pointer that points
to current node. Carefully read the statements. One for each link.
1009 1001 1099
head NULL
NULL 1 0 079 1001 1009 1 0 011 1099 11
(Curr.prev).next=newestNode
curr
newestNode.prev=Curr.prev
newestNode.next=Curr
Curr.prev=newestNode
1009 1001 1099
head 11
NULL 1 0 079 1019 1009 1 0 011 1099
NULL
1019
1009 13
1001
40 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Insert at Middle www.foxitsoftware.com/shopping
42 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Deletion at End www.foxitsoftware.com/shopping
43 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
DELETE www.foxitsoftware.com/shopping
Algorithm: DELETE_END(Head)
Algorithm: Input: reference to first node
DELETE_START(Head) Output: new list with node deleted
Steps:
Input: reference to first node Start:
If Head!=NULL// list is not empty
Output: new list with node deleted 1.
2. If Head.next==NULL// there is only one node
Steps: 3. Head=NULL
Else
Start
4.
5. Set Curr=Head
1. If Head!=NULL// list is not 6. While (Curr.next!=NULL)
7. Curr=Curr.next
empty 8. End While
2. Head=Head.next 9. (Curr.prev).next=NULL
10. Curr.prev=NULL
3. Head.prev=NULL 11. End If
End If
4. End If
12.
End End
//if you are maintaining tail reference, then it must be updated
during deletion
44 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Deletion at Location www.foxitsoftware.com/shopping
curr
1009 1099
head NULL 1099 1009 11
NULL
10079
45 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
DELETE www.foxitsoftware.com/shopping
46 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Circular Linked List-Single www.foxitsoftware.com/shopping
Every node contains only one next link which points to next node in
list. 1009 1001 1099
head 9 1001 7 1099 11 1009
47 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Circular Linked List-Double www.foxitsoftware.com/shopping
Doubly linked list, with last node pointing to first node and first node
pointing to last node.
1009 1001 1011
head 1011 1001
1007 1009 1 0 011 1001 100195 1009
9 1011
48 25/10/2020
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Circular Linked List www.foxitsoftware.com/shopping
When
loop
will
termina
te?
49 25/10/2020
INSERTION IN AN EMPTY LIST
return last;
}
INSERTION AT THE BEGINNING OF THE LIST
return last;
}
INSERTION AT THE END OF THE LIST
return last;
}
static Node addAfter(Node last, int data, int item)
{
if (last == null)
return null;
Node temp, p;
p = last.next;
do { To insert a node in between the two nodes, follow these steps:
if (p.data == item) { • Create a node, say T.
temp = new Node(); • Search for the node after which T needs to be inserted, say that
temp.data = data; node is P.
temp.next = p.next; • Make T -> next = P -> next;
p.next = temp; • P -> next = T.
if (p == last)
last = temp;
return last;
}
p = p.next;
} while (p != last.next);
1009 1001
head data 1001 data NULL tail
Memory allocation
Static vs. dynamic
Contiguous vs linked
Space utilization
Array is fixed whereas linked list can grow/shrink
Single node vs single cell
59 01/10/2015
Edited with the trial version
of Foxit Advanced PDF
Editor
To remove this notice, visit:
Practice Problems www.foxitsoftware.com/shopping