Professional Documents
Culture Documents
Linked List: Data Structure and Algorithms
Linked List: Data Structure and Algorithms
Linked List: Data Structure and Algorithms
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 Arrays www.foxitsoftware.com/shopping
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.
Each
block of data is allocated dyanamically in memory using the new
operator.
4 01/10/2015
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Linked List www.foxitsoftware.com/shopping
data next
5 25/10/2020
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Linked List www.foxitsoftware.com/shopping
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:
start
1. Set 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 node
7. End While
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
In this case, first we need to locate the 2nd node. That is node with data=7.
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 NULL
1019
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
23 25/10/2020
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Variations of Insert 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
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 node Start:
1. If Head!=NULL// list is not empty
Output: new list with first node deleted 2. If Head.next==NULL// there is only one node
Steps: 3. Head=NULL
4. Else
Start 5. Set Curr=Head, Prev=NULL
1. If Head!=NULL// list is not empty 6. While (Curr.next!=NULL)
7. Prev=Curr
2. Head=Head.next 8. Curr=Curr.next
9. End While
3. End If 10. Prev.next=NULL
11. End If
End 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 NULL 7 1001 1009 11 1099 1001 1009
15 NULL
1009 1001
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.
If we have tail node
34 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
Change links
1009 1001
head 1011 7 1001 1009 11 NULL
1009 1001
1011
15
NULL 1009 1009
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 7 1001 1009 11 1011
1009 1001
1011
15
1001 1009 NULL
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
1019
1009 13 1001
39 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 7 1001 1009 11 1099 11 NULL
1009 1001
(Curr.prev).next=newestNode
curr
newestNode.prev=Curr.prev
newestNode.next=Curr
Curr.prev=newestNode
1009 1001 1099
head NULL 7 1019 1009 11 1099 11 NULL
1009 1001
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
4. Else
Start 5. Set Curr=Head
1. If Head!=NULL// list is not empty 6. While (Curr.next!=NULL)
7. Curr=Curr.next
2. Head=Head.next 8. End While
9. (Curr.prev).next=NULL
3. Head.prev=NULL 10. Curr.prev=NULL
End If
4. End If 11.
12. End If
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 7 1099 1009 11 NULL
1009
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 1009
7 1001 1009 1001
11 1011 1001 1009
15 1009
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
49 25/10/2020
Tail Node
Whenever we do insertion/deletion to end of list, we need to search for last
node, it involves loop
Can we avoid this loop?
By maintaining a reference to last node just like we do for first node.
It will save time
How?
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
60 01/10/2015