Professional Documents
Culture Documents
Dhakshayani. J,
Guest Faculty,
Pondicherry University.
STACK
DATA
STRUCTURE
ARRAY
QUEUE STAC
K
What is Linear Data Structure
In linear data structure, data is arranged in
linear sequence.
Data items can be traversed in a single run.
In linear data structure elements are accessed or
placed in contiguous(together in sequence)
memory location.
What Is Stack?
It is an ordered group of homogeneous items of
elements.
A stack is called a last-in-first-out (LIFO)
2) Stack underflow
The condition resulting from trying to pop an empty stack.
if(!stack.IsEmpty())
stack.Pop(item);
Algorithm Of Insertion In Stack: (Push)
1. Insertion(a,top,item,max)
2. If top=max then
print ‘STACK OVERFLOW’
exit
else
3. top=top+1
end if
4. a[top]=ite
m
5. Exit
Algorithm Of Deletion In Stack: (Pop)
1. Deletion(a,top,item)
2. If t op =0 then
print ‘STACK UNDERFLOW’
exit
else
3. item=a[top]
end if
4. t o p = t o p - 1
5. Exit
Algorithm Of Display In Stack:
1.Display(top,i,a[i])
2.If t op =0 then
Print ‘STACK
EMPTY’
Exit
Else
3.Fo
r
i=t o
p to
0
APPLICATIONS OF STACKS ARE:
I. Reversing Strings:
• A simple application of stack is reversing strings. To reverse a
string , the characters of string are pushed onto the stack one by
one as the string is read from left to right.
• Once all the characters of string are pushed onto stack, they are
popped one by one. Since the character last pushed in comes
out first, subsequent pop operation results in the reversal of the
string.
For example:
To reverse the string ‘REVERSE’ the string is read
from left to right and its characters are pushed.
LIKE:
II. Checking the validity of an expression
containing nested parenthesis:
• PREFIX notation:
e.g, +AB
• POSTFIX notation:
e.g: AB+
Conversion of INFIX to POSTFIX conversion:
Example: (2+(4-1)*3) step1
2+41-*3 step2
2+41-3* step3
241-3*+ step4
CONVERSION OF INFIX INTO POSTFIX No two
operators of
2+(4-1)*3 into 241-3*+ same priority can
stay together in
the stack
CURREN ACTION STACK STATUS POSTFIX
T PERFORME EXPRESSI
SYMBOL D ON
( PUSH ( (
2 2
+ PUSH + (+ 2
( PUSH ( (+( 2
4 24
- PUSH - (+(- 24
1 241
) (+(-) 241-
* PUSH * (+* 241-
3 241-3
) (+*) 2 4 1- 3 *+
IV. Tower of Hanoi problem
• Complex recursive problem
• The problem description is,
Suppose there are three pillars A,B and C. there are N
discs of decreasing size so that no two discs are of the same
size
Initially, all the discs are stacked on one pillar in their
decreasing order of size. Let this pillar be A. the other two
pillars are empty.
The problem is to move all the discs from one pillar to another using
the third pillar as an auxiliary so that,
1. Only one disc can be moved at a time.
2. A disc may be moved from any pillar to another pillar
3. At no time can a larger disc can be placed on a smaller disc
The solution of this problem can be stated recursively as follow:
Move N discs from pillar A to C via the pillar B means,
• Moving the first (N-1) discs from pillar A to B
• Moving the disc from the pillar A to C
• Moving all (N-1) discs from pillar B to C
The above solution can be described by writing a function move
(N,A,B,C)
Operations
Enqueue (ItemType, newItem)
Dequeue (ItemType, item)
Enqueue (ItemType Dequeue (ItemType
newItem) newItem)
2) Queue underflow
The condition resulting from trying to remove an empty queue.
if(!q.IsEmpty())
q.Dequeue(item);
Algorithm Of Insertion In Queue: (Enqueue)
1. Using array
2. Multi queue implementation
3. Using a double linked list
1. Array representation of Priority Queue
Array hold the item and its priority value.
Insertion can be done at rear while deletion will be performed in
either of the two ways
1. traverse the array from FRONT for the element with high
priority. Delete that element from queue. If that is not the front
most element, shift all its trailing elements after deleted element
one stroke each to fill the vacant.
2. Add element at the rear end. Using stable sort
algorithm, sort the elements of queue so that the highest
priority element is at the FRONT. When the deletion is
required, delete it from FRONT end only.
2. Multi-queue implementation
This implementation assumes N different priority values.
For each Pi there are two pointers Fi and Ri.
Difficulties:
Lead to huge shifting to make room for an item to be inserted.
Large number of pointers are involved when the range of
priority values is large.
3. Linked list Representation of Priority Queue
We assumes with a node structure,
1. Simulation
2. CPU scheduling in multi-programming environment
3. Round robin algorithm
1. simulation
Time quantum=5
In and out in a queue during RR algorithm