3.2 Linked Lists Types
3.2 Linked Lists Types
Algorithm (IT-209)
Instructor: Abdullah Javed
([email protected])
Lecturer,
Govt. Postgraduate College, Jhelum
Lecture 3.2
Fall 2020
Agenda
• Linked List Variants
Singly
Doubly
Circular
Doubly Circular
2
Variations of Linked Lists
• The linked list that we studied so far is called singly
linked list
• Other types of linked lists exist, namely:
– Circular linked linked list
– Doubly linked list
– Circular doubly linked list
• Disadvantages
Each node maintain forward linkage only
Overhead
Insertion & deletion at end - Reach Predecessor
Within sight
But, out of reach
Expensive operation
Depends upon the length of the list
4
Doubly Linked List
• Each node maintain two pointer
One pointing forward
One pointing backward
5
Doubly Linked List
6
Doubly Linked Lists
pHead
a b c
Advantages:
• Convenient to traverse the list backwards.
• E.g. printing the contents of the list in backward order
Disadvantage:
• Increase in space requirements due to storing two pointers
instead of one
7
Doubly Linked List Operations
• Insert a node At the start of the list
In the middle of the list
At the end of the list
• Traversal
At the start of the list
At the end of the list
• Searching
Scan list contents
8
Insert at Start
9
Insert at Start
10
Insert at Start
11
Insert at End
12
Insert at End
13
Insert at End
14
Delete from Start
15
Delete from Start
16
Delete from End
17
Delete from End
18
Traversal
• Scan list
Starting from the start of the list, moving one node forward
Starting from the end of the list, moving one node backward
19
Searching
20
current
Home Task
• At the middle
Insert an item
Delete an item
newNode
oldNode
21
Circular Linked Lists
• Last node references the first node
• Every node has a successor
• No node in a circular linked list contains NULL
• E.g. a turn-based game may use a circular linked list to switch
between players
• Application
Fair distribution of resources among processes
• Usage
Move current pointer ahead to activate next process
22
Circular Doubly Linked Lists
• Circular doubly linked list
– prev pointer of the dummy head node points to the last node
– next reference of the last node points to the dummy head node
– No special cases for insertions and deletions
24