Professional Documents
Culture Documents
Vector Linked Lists
Vector Linked Lists
Overview
• Vectors
– Definition, Operations, Implementation
0 1 2 3 4 5 6 11 16
arr
r
n = 11
size = 17
Insertion
• In operation insertAt(r, o), we need to make
room for the new element by shifting forward the
n r elements arr[r], …, arr[n 1]
• In the worst case (r 0), this takes O(n) time
Delete at r arr o
0 1 2 r n
– element
– link to next node
• At the most one predecessor element
and one successor
• Also called as Singly Linked List
Head / Start Tail / End
Null
A B C D
Linked List Operations
• Insert and Deletion
– At Beginning (Start/Head)
– At End (Tail)
– At given position
– After given position/element/node
– Before given position/element/node
• Traversal/Search
Visualize Array as List
• Simple implementation by rearranging array
elements
– insert - move all subsequent elements right
– delete - move all subsequent elements left
• Too Expensive
Insert 48 Delete 5
50 5 32 54 50 5 48 32 54
50 48 32 54
Ways of implementation
• Using an Array
– Array of nodes maintained
– Each node holds index of the next node
• Using References
– Each node is dynamically allocated as
required
Lists Using Array
End Head
Linked List using References
• Struct Node
– Representation of single node
– Holds element and reference to next node
• LinkedList
– Holds the “start/head” node
– Operations(insert, delete, search)
Insert at Start/Head
Start/Head S2 S3
X
Start/Head S2 S3 S4
L X
Insert in the middle
• Insert new element ‘S3’ After element ‘S2’
Start/Head S1 S2 S4
S3
2 1
Start/Head S1 S2 S4
P
Deletion at Start & End
• Start/Head
– Set head = head.next
• End/Tail
– Traverse till last node maintaining previous
node in a variable (prev)
– Set prev.next = null
Delete at middle
• Delete node which is in the middle of List
– Traverse till the node(curr) which you are going to
delete, maintaining previous node (prev)
– Set prev.next = curr.next
• To Delete ‘S3’
prev curr
Start/Head S2 S3 S4
Stack with a Singly Linked List
• We can implement a stack with a singly linked list
• The top element is stored at the first node of the list
• The space used is O (n) and each operation of the
Stack ADT takes O(1) time
nodes
top
elements
Queue with a Singly Linked List
• We can implement a queue with a singly linked list
– The front element is stored at the first node
– The rear element is stored at the last node
• The space used is O(n) and each operation of the
Queue ADT takes O(1) time
rear
nodes
front
elements
Variants of Linked List
• Doubly Linked List
• Circular Linked List
• Circular Doubly Linked List
Doubly Linked List
• Linked List which can traverse Node
forward and backward prev next
• Each node stores
– element
– link to the previous node
element
– link to the next node
head tail
elements
Insertion
• We visualize operation insertAfter(p, X)
p
A B C
p
A B C
X
p
A B X C
Deletion
• We visualize deleteAt(p)
p
A B C D E
A B C p E
A B C E
Circular Linked List
• First node is made the successor of the
last node i.e. last node’s next reference is
to the first node in the list.
• Usage - round robin scheduling of
processes on CPU.
Head / Start
A B C D
Circular Doubly Linked List
head
elements
Summary
• Do you know the difference between
Array, Vector and Linked List?