DS Unit-II IT DPT
DS Unit-II IT DPT
STACK ADT
A stack is a dynamic and ordered collection of elements in which the insertion and deletion
are restricted at only one end.
The end from which the elements are added and deleted is referred as the ‘top’ of the stack.
Stack is also referred as PILES or PUSH down list. The last element placed in the stack will
be at the top of the stack.
The last element added to the stack is the first element to be removed. Hence stack are
referred as LAST IN FIRST OUT.
Operations:
Operations Axioms
Push Push is an operation to add new element at top in the stack.
Pop Pop is an operation to delete an element at top of the stack
Peak To display the element at top in the stack
Display To display the elements from top to 0 position in the stack
Representation of Stack
Stack can be represented using 2 ways:
Static implementation of stack using array
Dynamic implementation of stack using linked list
1. PUSH
Push is an operation used to add new element into the stack.
While implementing the push operation, overflow condition of the stack is to be checked, to
avoid to push an element in the fully occupied stack.
8
7
top = -1 6
5
queue_size = 10
4
3
2
1
0
9
8
7
6
5
4
3
2
top 1 102
0 101
stack
To push the value 103:
9
8
7
6
5
4
3
top 2 103
1 102
0 101
stack
To push the value 104:
9
8
7
6
5
4
top 3 104
2 103
1 102
0 101
stack
top = top -1 => top = 3 – 1 = 2 => top = 2
9
8
7
6
5
4
3
top 2 103
1 102
0 101
stack
9
8
7
6
5
4
3
2
top 1 102
0 101
stack
9
8
7
6
5
4
3
2
1
top 0 101
stack
since stack becomes empty (top = -1) , pop operation is not possible
3.PEAK
Peak operation is used to display the element from the stack pointed by top.
While implementing the peak operation the underflow condition of the stack is to be checked
to peak an element from an empty stack.
9
8
7
6
5
4
top 3 104
2 103
1 102
0 101
stack
4.Display
To display the element from the stack, traverse in the stack from top to 0 position.
During implementation of the display operation, the underflow condition of the stack is to be
checked to avoid retrieving an element from empty stack.
Algorithm Display(stack , top)
{
if top = -1 then // Is stack empty ?
{
write "Stack is in underflow condition. Display operation is not possible.";
return;
}
// Is stack not empty ?
for i = top to step -1 do
{
write stack[top];
}
}
Example: Peak element in Non-empty stack
9
8
7
6
5
4
top 3 104
2 103
1 102
0 101
stack
Queue ADT
Queue is a dynamic and ordered collection of elements in which insertion are made at one
end and deletion are made at another end.
The end at which insertion are made is referred as rear end. The end from which deletion are
made is referred as front end.
In a queue the first element is to be deleted , so that queue is known as “first in first out”.
Instance:
A stack is a dynamic and ordered collection of elements E1, E2,…, En of size n arranged in a
linear sequence where Ei refers to the ith element in the list.
Operations:
Operations Axioms
Enqueue Enqueue is an operation to add new element at rear end in the queue.
Dequeue Pop is an operation to delete an element at front end in the queue
Peak To display the element at front end in the queue
Display To display the elements from front to rear in the queue
Types of queue
1. Linear Queue
2. Circular Queue
3. Double ended queue
One of the simplest ways of representing a queue is by means of single dimensional array. Both
queue & array are the ordered collection of elements but queue & array are different things.
In array, the number of elements in the array is fixed so insertion and deletion is not possible.
In queue, the size of the queue varies continuously when elements are enqueued and dequeued
Since the queue is stored in part of array, an array can be declared large enough to hold maximum
number of elements of the queue.
During the execution of the program, the queue size can be varied within the space reserved for it.
One end of the array (front of the queue) is fixed and the other end of the array (rear of the queue)
is continuously changed within size of array depending upon the elements enqueued or dequeued
in the queue.
Basic operations
1. Enqueue
2. Dequeue
3. Peak
4. Display
The rear pointer is in the last position (queue size-1) then queue is said to be fully occupied.
Initially the front end and the rear end are at the same position (-1). When you insert elements,
rear pointer moves one by one until the last position is reached.
When we delete elements, the front pointer moves one by one until it reaches rear pointer.
Beyond this we can’t insert the data, irrespective of position of front pointer; this is a main
disadvantage of linear queue which is overcome by circular queue.
1) Enqueue Operation
Enqueue is an operation used to add an element at rear end of the queue. While implementing enqueue
operation, overflow condition of the queue must be checked to avoid to enqueue an element in empty
queue
0 1 2 3 4 5 6 7 8 9
queue :
front = 0
rear = 0
queue [rear] = value => queue [0] = 101
0 1 2 3 4 5 6 7 8 9
queue : 101
front rear
To enqueue value 102 (in non-empty queue)
0 1 2 3 4 5 6 7 8 9
queue : 101 102
front rear
0 1 2 3 4 5 6 7 8 9
queue : 101 102 103
front rear
To enqueue value 104 (in non-empty queue)
0 1 2 3 4 5 6 7 8 9
queue : 101 102 103 104
front rear
2) Dequeue
front rear
0 1 2 3 4 5 6 7 8 9
queue : 101 102 103 104
front rear
0 1 2 3 4 5 6 7 8 9
queue : 102 103 104
front rear
front = front + 1 => front = 1 + 1 = 1 => front = 2
0 1 2 3 4 5 6 7 8 9
queue : 103 104
front rear
0 1 2 3 4 5 6 7 8 9
queue : 104
front rear
0 1 2 3 4 5 6 7 8 9
queue :
front = -1 rear = -1
since queue becomes empty(front = -1 and rear = -1), dequeue operation is not possible
3) Peak
Algorithm display(queue,front,rear)
{
if front = -1 and rear = -1 then // Is queue Empty?
{
write "queue is in underflow condition. peak operation is not possible.";
return;
}
// Is queue not Empty?
write queue[front];
}
Example: Pear operation in non-empty queue
0 1 2 3 4 5 6 7 8 9
queue : 101 102 103 104
front rear
4) Display
Display is an operation to traverse queue to retrieve and display the elements in the queue from front to
rear
0 1 2 3 4 5 6 7 8 9
queue : 101 102 103 104
front rear
Basic operations
1. Enqueue
2. Dequeue
3. Peak
4. Display
1) Enqueue Operation
queue_size = 10
queue
To enqueue the value 124
rear = (rear +1) % queue_size => rear = (9+1) % 10 => rear =10 % 10 => rear = 0
2) Dequeue
queue_size = 10
queue
queue_size = 10
queue
front = (front +1) % queue_size => front = (3+1) % 10 => front = 4 % 10 => rear = 4
3) Peak
}
Example: Peak operation in non-empty queue
queue_size = 10
queue
queue_size = 10
queue
Types of deque
Linear deque
Circular deque
Linear Deque
It is similar to the linear queue except the following conditions:
If the front pointer is in the first position (zeroth position), we can’t insert data at front
end.
If the rear is in the last position (queuesize-1) we can’t insert data at rear end.
Circular Deque
Circular deque is similsr to circular queue except the following conditions:
Insertion and deletion are made at front and rear end of the queue.
Irrespective of the position of the front and rear we can insert and delete he data.
Basic operations
1. Enqueue_front
2. Enqueue_rear
3. Dequeue_front
4. Dequeue_rear
1) Enqueue Operation
i) Enqueue - Front
0 1 2 3 4 5 6 7 8 9
queue :
front rear
To enqueue the value 102
0 1 2 3 4 5 6 7 8 9
queue : 102 101
0 1 2 3 4 5 6 7 8 9
queue : 103 102 101
0 1 2 3 4 5 6 7 8 9
queue : 104 103 102 101
front rear
To enqueue the value 105
0 1 2 3 4 5 6 7 8 9
queue : 105 104 103 102 101
front rear
ii) Enqueue - rear
Algorithm Enqueue_rear (queue , rear , front , queue_size , value)
{
if rear = queue_size-1 then // Is queue Full ?
{ write "queue is in overflow condition. Thus enqueue operation is not possble.";
return;
}
if rear=-1 and front=-1 then // Is queue Empty?
{
rear = 0;
front = 0;
}
else // Is queue not Empty?
{
rear=rear+1;
}
queue[rear] = value;
}
Example: Enqueue-rear operation in Empty queue
0 1 2 3 4 5 6 7 8 9
queue :
front = 0
rear = 0
queue [rear] = value => queue [0] = 101
0 1 2 3 4 5 6 7 8 9
queue : 101
front rear
To enqueue value 102 (in non-empty queue)
0 1 2 3 4 5 6 7 8 9
queue : 101 102
front rear
0 1 2 3 4 5 6 7 8 9
queue : 101 102 103
front rear
To enqueue value 104 (in non-empty queue)
0 1 2 3 4 5 6 7 8 9
queue : 101 102 103 104
front rear
2) Dequeue
front rear
0 1 2 3 4 5 6 7 8 9
queue : 101 102 103 104
front rear
0 1 2 3 4 5 6 7 8 9
queue :
102 103 104
front rear
front = front + 1 => front = 1 + 1 = 1 => front = 2
0 1 2 3 4 5 6 7 8 9
queue : 103 104
front rear
0 1 2 3 4 5 6 7 8 9
queue : 104
front rear
0 1 2 3 4 5 6 7 8 9
queue :
front = -1 rear = -1
since queue becomes empty(front = -1 and rear = -1), dequeue operation is not possible
ii). Dequeue_rear
0 1 2 3 4 5 6 7 8 9
queue : 104 103 102 101
front rear
0 1 2 3 4 5 6 7 8 9
queue : 104 103 102 101
front rear
rear = rear -1 => rear = 9 -1 => rear = 8
0 1 2 3 4 5 6 7 8 9
queue : 104 103 102
front rear
rear = rear -1 => rear = 9 -1 => rear = 8
0 1 2 3 4 5 6 7 8 9
queue : 104 103
front rear
rear = rear -1 => rear = 8 -1 => rear = 7
0 1 2 3 4 5 6 7 8 9
queue : 104
front rear
rear = rear -1 => rear = 7 -1 => rear = 6
0 1 2 3 4 5 6 7 8 9
queue : 104
front rear
front = -1 rear = -1
since queue becomes empty(front = -1 and rear = -1) , dequeue operation is not possible
7. Discuss any one of the application of queue in detail (or) Application of queue
Direct applications
o Access to shared resources (e.g., printer)
o Multiprogramming
Indirect applications
o Priority queue
o Auxiliary data structure for algorithms
o Component of other data structures
Priority queue
Priority queue is a data structure having collection of elements which are associated with specific
order. They are of two types:
1. Ascending Priority Queue
2. Descending Priority Queue
Ascending Priority Queue
It is a collection of elements in which the insertion of element can be any order, but only the
smallest element can be removed.
1. Enqueue
}
}
0 1 2 3 4 5 6 7 8 9
queue : 101 104 107 114
front rear
To enqueue the value 103
Find the appropriate position to insert the value 103 by traversing from rear to front
i=3
103 < queue[3]?
102 < 114 true => queue[i+1] =queue[i] => queue[4] = queue[3]
i = i – 1 => i = 3 – 1 => i = 2
0 1 2 3 4 5 6 7 8 9
queue : 101 104 107 114
front rear
i=2
103 < queue[2] ?
102 < 107 true => queue[i+1] =queue[i] => queue[3] = queue[2]
0 1 2 3 4 5 6 7 8 9
queue : 101 104 107 114
front rear
i=1
103 < 104 true => queue[i+1] =queue[i] => queue[2] = queue[1]
i = i – 1 => i = 1 – 1 => i = 0
0 1 2 3 4 5 6 7 8 9
queue : 101 104 107 114
front rear
i=0
103 < 101 false => queue[i+1] =value => queue[1] = 103 => Insertion of the value
0 1 2 3 4 5 6 7 8 9
queue : 101 103 104 107 114
front rear
2) Dequeue
0 1 2 3 4 5 6 7 8 9
queue : 101 102 103 104
front rear
0 1 2 3 4 5 6 7 8 9
queue : 101 102 103 104
front rear
0 1 2 3 4 5 6 7 8 9
queue : 102 103 104
front rear
0 1 2 3 4 5 6 7 8 9
queue : 103 104
front rear
0 1 2 3 4 5 6 7 8 9
queue : 104
front rear
front = -1 rear = -1
since queue becomes empty(front = -1 and rear = -1), dequeue operation is not possible