Stack
Stores aset of elements in a particular order
Stack principle: LAST IN FIRST OUT = LIFO
It means: the last element inserted is the first one
to be removed
Example
3.
Last In FirstOut
B
A
D
C
B
A
C
B
A
D
C
B
A
E
D
C
B
A
top
top
top
top
top
A
4.
Stack Operations
Thelast item added is pushed (added) to
the stack.
The last item added can be popped
(removed) from the stack.
The last item added can be topped
(accessed) from the stack.
These operations all take constant time:
O(1).
A typical stack interface:
void push(Thing newThing);
void pop();
Thing top();
5.
Basic Alg forStack Push & Pop
PUSH(STACK,TOP,MAXSTK,ITEM)
1. If TOP=MAXSTK, then
print OVERFLOW, and
Return
2. Set TOP = TOP +1
3. Set STACK[TOP] = ITEM
4. Return
POP(STACK,TOP,MAXSTK,ITEM)
1. If TOP=0, then print
UNDERFLOW, and
Return
2. ITEM = Set STACK[TOP]
3. Set TOP = TOP - 1
4. Return
6.
Stack Applications
StringConversion
Converting Infix to Postfix
Evaluation of Expression
Towers of Hanoi
Balancing symbols
7.
Infix to postfix
Algorithm
1) Examine the next element in the input.
2) If it is an operand, output it.
3) If it is opening parenthesis, push it on stack.
4) If it is an operator, then
i) If stack is empty, push operator on stack.
ii) If the top of the stack is opening parenthesis,
push operator on stack.
8.
iii) Ifit has higher priority than the top of stack,
push operator on stack.
iv) Else pop the operator from the stack and
output it, repeat step 4.
5) If it is a closing parenthesis, pop operators
from the stack and output them until an opening
parenthesis is encountered. pop and discard the
opening parenthesis.
6) If there is more input go to step 1
7) If there is no more input, unstack the
remaining operators to output.
Algorithm for evaluating
expression
Byusing a stack algorithm
1. Initialize an empty stack
2. Repeat the following until the end of the
expression is encountered
a) Get the next token (const, var, operator) in the
expression
b) Operand – push onto stack
Operator – do the following
i. Pop 2 values from stack
ii. Apply operator to the two values
iii. Push resulting value back onto stack
3. When end of expression encountered,
value of expression is the (only) number
left in stack
Algorithm for BalancedSymbol
Checking
Make an empty stack
read symbols until end of file
if the symbol is an opening symbol push it onto the
stack
if it is a closing symbol do the following
if the stack is empty report an error
otherwise pop the stack. If the symbol popped does not
match the closing symbol report an error
At the end of the file if the stack is not empty
report an error
Basic Insert Algfor Queue
QINSERT(QUEUE,N,FRONT,REAR,ITEM)
1. If FRONT = 1 and REAR = 1, or if FRONT = REAR
+ 1, then Write OVERFLOW and Return
2. If FRONT = NULL, then
SET FRONT = 1 and REAR = 1
Else If REAR = N, then:
SET REAR = 1
Else
SET REAR = REAR+1
3. Set QUEUE[REAR] = ITEM
4. Return
32.
Basic Delete Algfor Queue
QDELETE(QUEUE,N,FRONT,REAR,ITEM)
1. If FRONT = NULL, then Write UNDERFLOW and
Return
2. Set ITEM = QUEUE[FRONT]
3. If FRONT = REAR, then
SET FRONT = NULL and REAR = NULL
Else If FRONT = N, then:
SET FRONT = 1
Else
SET FRONT = FRONT+1
3. Return
33.
Working of asimple queue
0 1 2 3 4 5 6 7
Proc
ess:
Elements
:
a b c d e f g h
F
R
-
1
Animated by S.Graceline Jasmine
34.
Working of acircular queue
1 2 3 4 5 6 7 8
Proc
ess:
Elements
:
a b c d e f g h
F
R
0
Animated by M.Jayasudha
i j
F = R = 0
35.
Priority Queue
Apriority queue is a collection of elements such
that each element has been assigned a priority
and such that the order in which elements are
deleted and processed comes from the following
rules:
An element of higher priority are processed before
any element of lower priority.
Two elements with the same priority are
processed according to the order in which they
were added to the queue.
36.
Array representation ofa priority queue
Use separate queue foe each level of priority.
Each such queue will appear in its own circular
array and must have its own pair of pointers,
FRONT and REAR.
If each queue is allocated the same amount of
space, a two-dimensional array QUEUE can be
used instead of linear arrays.
37.
Example for PriorityQueue
Front[K] and REAR[K] contain, respectively, the
front and rear elements of row K of QUEUE.
2
1
0
5
4
Front Rear
2
3
0
1
4
AAA
BBB CCC XXX
FFF DDD EEE
GGG
1 2 3 4 5 6
1
2
3
4
5