Arrays , Link Lists, Stacks and Queues
Deliverables
Arrays
Link Lists
Stacks
Queues
6/5/2012 7:14 PM Copyright@ gdeepak.com 2
Arrays
Arrays are stored in contiguous memory locations and contain similar
data. Position of any array element can be accessed from its index
position with help of array’s starting address.
An element can be accessed, inserted or removed by specifying its
position (number of elements preceding it)
Vector is abstraction of array data structure, where we can access by
rank.
6/5/2012 7:14 PM Copyright@ gdeepak.com 3
Array operations
• Deletion: In operation removeAtPosition(r), we need to
fill the hole left by the removed element by shifting
backward n-r-1 elements A[r + 1], …, A[n - 1]. In the
worst case (r = 0), this takes O(n) time. In the average
case also it is O(n).
• A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
3 10 0 -135 2 21 41 6 87 11
• Is it necessary to shift the array elements after deletion?
6/5/2012 7:14 PM Copyright@ gdeepak.com 4
Deletion of -34
5
23
-34
1
81
9 After deletion of -34
123
45
2
-31
6/5/2012 7:14 PM Copyright@ gdeepak.com 5
Insertion
Insertion in the array again requires shifting all the
elements one index ahead to make room for insertion.
Need to understand shifting different in deletion and
insertion, otherwise data may be lost.
In Insertion shifting is to start from the last element of
the array and in deletion shifting is to start from the next
array index from where the element is to be deleted.
6/5/2012 7:14 PM Copyright@ gdeepak.com 6
Inserting 100 after -34
5
23
-34
1
81 100
9
123
45
2
6/5/2012 7:14 PM Copyright@ gdeepak.com 7
Array complexity
Accessing an array: O(1) time
Copying an array: O(n) time
Merging two arrays: O(m+n) time
Splitting an array: O(n) time
Intersection of two arrays: O(n2)
Union of two arrays: O(n2)
6/5/2012 7:14 PM Copyright@ gdeepak.com 8
Array incrementing by C or doubling
Let n be the number of elements in the array. The space
used is O(n) and each operation runs in time O(1)
The maximum size of the array must be defined apriori
and cannot be changed.
Incremental strategy: increase the size by a constant C
Doubling strategy: double the size
6/5/2012 7:14 PM Copyright@ gdeepak.com 9
Array incrementing by C or Doubling
If increment by C every time then
Creation copying insertion (iteration wise)
ci c(i-1) c
Total = ci+ci-c+c=2ci
If there are n elements in end then total iterations will be
n/c , So it is 2c(1+2+3+……..n/c) = n n
2c( )( 1) 2
c c n
O(n 2 )
2 c
6/5/2012 7:14 PM Copyright@ gdeepak.com 10
Array incrementing by C or Doubling
If we double the array every time then the creation,
copying, insertion cost will be as follows
Creation copying insertion (iteration wise)
2i 2i-1 2i-1
Total cost in every iteration is 2i+( 2i-1+2i-1) =2i+2i=2i+1
if there are n elements to be inserted then there will be lgn
iterations , total becomes (21 +22+23+…+ 2lgn+1)=4n-1=O(n)
6/5/2012 7:14 PM Copyright@ gdeepak.com 11
Link List
Singly linked list is a data structure
consisting of a sequence of nodes
Each node stores element and link to the
next node
There may be a header link and trailer
link
6/5/2012 7:14 PM Copyright@ gdeepak.com 12
Link list operations
Access (): Accessing an element from a list
Worst case: O (n)
Average Case: 1+2+3+…n = n(n-1)/2 divided by n=n-1/2 = O(n)
Delete (): O(1) complexity, but it is preceded by accessing that
position, effectively taking O(n) time. Delete first takes O(1) time
6/5/2012 7:14 PM Copyright@ gdeepak.com 13
Link list complexity
Insert : O(1) but inserting before or after an element will
require accessing element effectively taking O(n) time.
Inserting at first place is O(1).
Merging : O(1) If the trailer node information is not
available then it will be equal to the length of the smaller
list to reach at the end of the list for merging with the
second list
Size: O(n) time
Intersection: O(n2)
Union: O(n2)
6/5/2012 7:14 PM Copyright@ gdeepak.com 14
Doubly link list
A doubly linked list provides a natural implementation of the
List
Nodes implement Position and stores element, link to the
previous node and link to the next node
Special trailer and header nodes. Deletion an Insertion
becomes easy as there is no need to remember the address of
the previous node.
6/5/2012 7:14 PM Copyright@ gdeepak.com 15
Insertion
6/5/2012 7:14 PM Copyright@ gdeepak.com 16
Deletion
6/5/2012 7:14 PM Copyright@ gdeepak.com 17
Circular link list
There will be no difference in the time
complexity of various operations
Circular Link List and circular doubly link list
Input output buffers use circular queue for
buffering.
6/5/2012 7:14 PM Copyright@ gdeepak.com 18
Comparison link list vs. array
It is easier to delete in the link list
it is easier to insert in the link list
It is easier to access in the array
We have to predefine the array so there can be wastage of
memory
6/5/2012 7:14 PM Copyright@ gdeepak.com 19
Comparison link list vs. array
Array Size has to be defined and is limited to the defined size while link
list can grow to limit of memory
Continuous allocation is required for array, while this is not with link
list
Arrays compute address of data elements by arithmetic operations
while in linked list addresses are in structure itself.
Arrays can be accessed backward and forward but we have to use
special link lists like doubly linked list for backward access
6/5/2012 7:14 PM Copyright@ gdeepak.com 20
Comparison link list vs. array
Arrays definition is part of the language construct but link
list we have to create
Merging two arrays is very difficult while merging link lists
is easy
There will be more cache misses in the Link list then in the
arrays
With large records, moving pointers is easier and faster
than moving the items themselves.
6/5/2012 7:14 PM Copyright@ gdeepak.com 21
Comparison link list vs. array
There can be an array of pointers associated with a link list,
in that case address part of the link list is not required.
In link lists two different processors may work on two
different parts of link list.
Due to address part there is wastage of memory in link list,
it is not much if we look at practical size of record of name,
age etc.
6/5/2012 7:14 PM Copyright@ gdeepak.com 22
Stack
Last in First out,
in and out only Page-visited
Undo sequence
from one end, history in a Web
in a text editor
other end is browser
closed
Chain of method
calls in the Java In other data
Recursion
Virtual Machine, Structures
Function Calls
6/5/2012 7:14 PM Copyright@ gdeepak.com 23
Simulating Pop and Push
6
98
37
25
12
5
6/5/2012 7:14 PM Copyright@ gdeepak.com 24
Stack Example
6/5/2012 7:14 PM Copyright@ gdeepak.com 25
Stack
Creation of an stack: O(n)
push(object): inserts an element O(1)
pop(): removes and returns the last inserted element. O(1)
top(): returns last inserted element without removing O(1)
size(): returns number of elements stored. O(1)
isEmpty(): indicates whether no elements are stored. O(1)
isFull(): Indicates whether array limit is over. O(1)
6/5/2012 7:14 PM Copyright@ gdeepak.com 26
Link List Vs. Array Implementation
Pop
Array: return element at “top” and decrement “top”
Linked list: return and remove at front of linked list
Push
Array: increment “top” and insert element. Must check for overflow!
Linked list: insert element at front of linked list
Array: more memory efficient
Linked list: don’t have to worry about “overflow”
6/5/2012 7:14 PM Copyright@ gdeepak.com 27
Axioms
pop(push(S, v))=S
Top(push(S, v))=v
Top(pop(push(push(S,12),15))) = top(push(S,12))
6/5/2012 7:14 PM Copyright@ gdeepak.com 28
Queues
Insertions and deletions follow the first-in first-out
scheme
Insertions are at the rear of the queue and removals are
at the front of the queue
Two variables keep track of the front and rear. Careful
implementation is necessary to avoid overlapping.
Operations complexity may vary depending upon the
implementation, whether front or rear is fixed or not
6/5/2012 7:14 PM Copyright@ gdeepak.com 29
Queue Example
6/5/2012 7:14 PM Copyright@ gdeepak.com 30
Simulating Enqueue and Dequeue
5 45 71
Perform Operations
Enqueue 99
Dequeue
Dequeue
99
6/5/2012 7:14 PM Copyright@ gdeepak.com 31
Queues
enque(Object o): deque(): removes front(): returns
inserts an and returns front element at front
element o at end element of without
of queue. O(1) queue. O(1) removing it. O(1)
isEmpty():return
size(): returns
s Boolean
number of
indicating queue
elements stored.
is empty or not
O(1)
O(1)
6/5/2012 7:14 PM Copyright@ gdeepak.com 32
Queue Applications
Waiting lists, bureaucracy
Access to shared resources (e.g. printer)
Multiprogramming
Tunnel
In other Data Structures
6/5/2012 7:14 PM Copyright@ gdeepak.com 33
Axioms
Front(Enqueue(Enqueue(Q, w),v))=front(Enqueue(Q, w))
Deque(Enque(Enque(Q, w),v))=Enque(Deque(Enque(Q, w)),v)
In an Enqueue operation, when the array is full, instead of
throwing an exception, we can replace the array with a larger
one, Similar to what we did for an array based stack.
6/5/2012 7:14 PM Copyright@ gdeepak.com 34
Circular queue
Circular Queue is
essentially implemented
as a linear array in a
wrapped around fashion
where even rear pointer
r f
of the queue can come
before the front pointer
6/5/2012 7:14 PM Copyright@ gdeepak.com 35
Questions, Comments and Suggestions
6/5/2012 7:14 PM Copyright@ gdeepak.com 36
Question 1
What is result of Last operation in given sequence :
Push(&);Push($);Push(@);Top();Pop();Push(#);
Pop();Pop();Push(%);Pop();
6/5/2012 7:14 PM Copyright@ gdeepak.com 37
Question 2
What will be the result of last result of given priority queue
if number 1 is the highest priority
Enqueue(3), Enqueue(6), Enqueue(2),dequeue, dequeue,
Enqueue(5),Enqueue(1),dequeue, dequeue
6/5/2012 7:14 PM Copyright@ gdeepak.com 38
Question 3
In a Singly circular linked list organization, insertion of a
record involves modification of
A. One pointer
B. Two pointers
C. Three pointers
D. Four pointer
E. No Pointer
6/5/2012 7:14 PM Copyright@ gdeepak.com 39