of 28
8/8/2019 Vector Linked Lists
1/28
Vectors and Linked Lists
8/8/2019 Vector Linked Lists
2/28
Overview
Vectors
Definition, Operations, Implementation
Linked List (LL) Definition, Operations, Implementation
Stacks and Queues using LL Variants of LL
8/8/2019 Vector Linked Lists
3/28
Vector
Collection of data/objects
Array without size constraints - Growable Array
Elements can be accessed randomly using index Operations
Insertion (at a particular index) Deletion (at a particular index) Traversal
Visiting each element atleast once
8/8/2019 Vector Linked Lists
4/28
Growable Array
What to do if array(A) is full or need to insertitem atposition > array size? Create a new array(X) with larger size
Copy the contents of old array (A) to the newarray(X)
Set o ld array(A) = new array(X) How large should the new array be?
incremental strategy:- increase the size by aconstantc
doubling strategy:- double the size
8/8/2019 Vector Linked Lists
5/28
Vector using Array
Use an array arrof size size
A variable nkeeps track of the size of the vector(number of elements stored)
Operation elementAt(r) is implemented in O(1) timeby returning arr[r]
arr
0 1 2
r
3 4 5 6 11 16
n = 11
size = 17
8/8/2019 Vector Linked Lists
6/28
8/8/2019 Vector Linked Lists
7/28
Deletion
In operation deleteAt(r), we need to fill the holeleft by the removed element by shifting backwardthe n r 1 elements arr[r 1], , arr[n 1]
In the worst case (r!
0), this takesO
(n
) time
arr
0 1 2 no
rDelete at r
0 1 2 nr
arrShift elements
left
0 1 2 nr
arrDecrement n
8/8/2019 Vector Linked Lists
8/28
Vector Implementation
struct Vector {int n; // Total elements so farint size; // Current space availableint *arr; // Array holding elements
};
void insertAt(struct Vector *v, int index, int item)int deleteAt(struct Vector *v, int index)void append(struct Vector *v, int item)int elementAt(struct Vector *v, int index)
int numElements(struct Vector *v)
8/8/2019 Vector Linked Lists
9/28
Linked List
Sequence of nodes
Each node stores
element
link to next node
At the most one predecessorand one successor
Also called as Singly Linked List
Node
next
element
A B C D
Null
Head / Start Tail / End
8/8/2019 Vector Linked Lists
10/28
Linked ListOperations
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
8/8/2019 Vector Linked Lists
11/28
Visualize Array as List
Simple implementation by rearranging arrayelements insert - move all subsequent elements right delete - move all subsequent elements left
Too Expensive
3250 5 54
Insert 48
3250 5 5448
Delete 5
3250 48 54
8/8/2019 Vector Linked Lists
12/28
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 asrequired
8/8/2019 Vector Linked Lists
13/28
Lists Using Array
Using an array of Nodes Each node holds index to the next node in the list.
108 134 205 76
4 -1 1 2
item
next
HeadEnd
8/8/2019 Vector Linked Lists
14/28
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)
8/8/2019 Vector Linked Lists
15/28
Insert at Start/Head
S2 S3 Start/Head
S1 S3 Start/Head S2
3. Set X as Head (head = X)
S1 1. Create a new node X
X
S2 S3
Start/Head
S1
2. Set Xs next to Head (X.next = head)X
8/8/2019 Vector Linked Lists
16/28
Insert at End/Tail
S2 S3 Start/Head
2. Traverse tilllast node (L)S4 1. Create a new node X
X
3. Set Ls next to X (L.next = X)S2 S4 Start/Head S3
L X
8/8/2019 Vector Linked Lists
17/28
Insert in the middle
Insert new element S3 After element S2
S1 S4 Start/Head S2
12
1. Create a new node X2. Traverse till S2 (Let this node be P)3. Set X.next = P.next4. Set P.next = X
Start/Head S1 S4 S2
S3
X
P
8/8/2019 Vector Linked Lists
18/28
Deletion at Start & End
Start/Head
Set head = head.next
End/Tail Traverse tilllast node maintaining previous
node in a variable (prev) Setprev.next = null
8/8/2019 Vector Linked Lists
19/28
Delete at middle
Delete node which is in the middle of List
Traverse till the node(curr) which you are going todelete, maintaining previous node (prev)
Setprev.next = curr.next
To Delete S3
S2 S4 Start/Head S3
currprev
8/8/2019 Vector Linked Lists
20/28
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
top
nodes
elements
8/8/2019 Vector Linked Lists
21/28
Queue with a Singly Linked List
We can implement a queue with a singly linked list
The front e lement 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 theQueue ADT takes O(1) time
front
rear
nodes
elements
8/8/2019 Vector Linked Lists
22/28
Variants of Linked List
Doubly Linked List
Circular Linked List
Circular Doubly Linked List
8/8/2019 Vector Linked Lists
23/28
Doubly Linked List Linked List which can traverse
forward and backward
Each node stores
element link to the previous node
link to the next node
prev next
element
Node
tailhead
elements
8/8/2019 Vector Linked Lists
24/28
Insertion We visualize operation insertAfter(p, X)
A B X C
A B C
p
A B C
p
Xp
8/8/2019 Vector Linked Lists
25/28
E
E
E
Deletion We visualize deleteAt(p)
A B C D
p
A B C
D
p
A B C
8/8/2019 Vector Linked Lists
26/28
Circular Linked List
First node is made the successor of thelast node i.e. last nodes next reference isto the first node in the list.
Usage - round robin scheduling ofprocesses on CPU.
A B C D
Head / Start
8/8/2019 Vector Linked Lists
27/28
Circular Doubly Linked List
elements
head
8/8/2019 Vector Linked Lists
28/28
Summary
Do you know the difference betweenArray, Vector and Linked List?
Are you able to visualize different typesof Linked List?
Do you understand the operations onSingly Linked List?