Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 28

Vectors and Linked Lists

Overview
• Vectors
– Definition, Operations, Implementation

• Linked List (LL)


– Definition, Operations, Implementation
– Stacks and Queues using LL
– Variants of LL
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
Growable Array
• What to do if array(A) is full or need to insert
item at position > array size?
– Create a new array(X) with larger size
– Copy the contents of old array (A) to the new
array(X)
– Set old array(A) = new array(X)
• How large should the new array be?
– incremental strategy:- increase the size by a
constant c
– doubling strategy:- double the size
Vector using Array
• Use an array ‘arr’ of size ‘size’
• A variable ‘n’ keeps track of the size of the vector
(number of elements stored)
• Operation elementAt(r) is implemented in O(1) time
by returning arr[r]

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

Insert ‘o’ at ‘r’ arr


0 1 2 r n

Shift elements arr


right 0 1 2 r n

Store ‘o’ and arr o


increment ‘n’ 0 1 2 r n
Deletion
• In operation deleteAt (r), we need to fill the hole
left by the removed element by shifting backward
the n  r  1 elements arr[r  1], …, arr[n  1]
• In the worst case (r  0), this takes O(n) time

Delete at r arr o
0 1 2 r n

Shift elements arr


left 0 1 2 r n

Decrement ‘n’ arr


0 1 2 r n
Vector Implementation
struct Vector {
int n; // Total elements so far
int size; // Current space available
int *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)


Linked List
Node
• Sequence of nodes
• Each node stores next

– 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

• Using an array of Nodes


– Each node holds index to the next node in the list.

item 108 134 205 76


next 4 -1 1 2

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

1. Create a new node X S1 

2. Set X’s next to Head (X.next = head)


X Start/Head
S1 S2 S3 

3. Set X as Head (head = X)


Start/Head S1 S2 S3 
Insert at End/Tail
Start/Head S2 S3 
X

1. Create a new node X S4 

2. Traverse till last node (L)


3. Set L’s next to X (L.next = X)

Start/Head S2 S3 S4 

L X
Insert in the middle
• Insert new element ‘S3’ After element ‘S2’
Start/Head S1 S2 S4 

1. Create a new node X


2. Traverse till S2 (Let this node be P)
3. Set X.next = P.next
4. Set P.next = X
X

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?

• Are you able to visualize different types


of Linked List?

• Do you understand the operations on


Singly Linked List?

You might also like