Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
What is stack?
What is stack?
Stack ADT allows all data operations at one
end only. At any given time, We can only
access the top element of a stack.
This feature makes it LIFO data structure.
LIFO stands for Last-in-first-out. Here, the
element which is placed (inserted or added)
last, is accessed first. In stack terminology,
insertion operation is
calledPUSHoperation and removal
operation is called POP operation.
Basic Operations
Array Representation of
Stacks
XXX
1
YYY
ZZZ
TOP
8
MAXSTK
Push Operation
PUSH(STACK, TOP, MAXSTK, ITEM)
This procedure pushes an ITEM onto a stack
1. If TOP=MAXSTK then print OVERFLOW and
Return
2. Set TOP:=TOP+1
3. Set STACK[TOP]:=ITEM
4. Return
Pop Operation
POP(STACK, TOP, ITEM)
This procedure deletes the top element of
STACK and assigns it to the variable ITEM
1. If TOP=0 then print UNDERFLOW and
Return
2. Set ITEM:=STACK[TOP]
3. Set TOP:=TOP-1
4. Return
XXX
INFO
YYY
LINK
Top of stack
ZZZ
Bottom of
stack
Push Operation
PUSH_LINKSTACK(INFO,LINK,TOP,AVAIL,ITEM)
1. If AVAIL=NULL, then Write OVERFLOW and Exit.
2.Set NEW :=AVAIL and AVAIL:=LINK[AVAIL]
3.Set INFO[NEW]:=ITEM
4.Set LINK[NEW]:=TOP
5.Set TOP=NEW
6.Exit
Pop Operation
POP_LINKSTACK(INFO,LINK,TOP,AVAIL,ITEM)
1.IF TOP=NULL the Write: UNDERFLOW and Exit
2.Set ITEM:=INFO[TOP]
3.Set TEMP:=TOP and TOP=LINK[TOP]
4.Set LINK[TEMP]=AVAIL and AVAIL=TEMP
5.Exit
Infix Notation
To add A, B, we write
A+B
To multiply A, B, we write
A*B
The operators ('+' and '*') go in between the operands
('A' and 'B')
This is "Infix" notation.
Prefix Notation
Instead of saying "A plus B", we could say "add A,B "
and write
+ AB
"Multiply A,B" would be written
*AB
This is Prefix notation.
Postfix Notation
Another alternative is to put the operators after the
operands as in
AB+
and
AB*
This is Postfix notation.
Parentheses
Evaluate 2+3*5.
+ First:
(2+3)*5 = 5*5 = 25
* First:
2+(3*5) = 2+15 = 17
Infix notation requires Parentheses.
Postfix Notation
235*+=
=235*+
= 2 15 + = 17
23+5*=
=23+5*
= 5 5 * = 25
No parentheses needed here either!
In
Postfix evaluation
Algorithm
1.Scan the Postfix string from left to right
2.Initialize an empty stack.
3.Repeat steps 4 and 5 till all the characters are scanned.
4. If the scanned character is an operand, push it onto the stack.
5.If the scanned character is an operator, and if the operator is unary
operator then pop an element from the stack. If the operator is binary
operator then pop two elements from the stack. After popping the
elements, apply the operator to those popped elements. Let the result of
this operation be retVal onto the stack.
6.After all characters are scanned, we will have only one element in the
stack.
7. Return top of the stack as result
Prefix evaluation
Given infix expression: 4 $ 2 * 3 3 + 8 / 4 / ( 1 + 1 )
Steps:
1.Reverse the expression.
)1+1(/4/8+33*2$4
2.The prefix string is filled from right to left. Initialize a stack.
a)Add left parenthesis ) to stack.
b)Add 1 to prefix string
c)Push + on stack
d)Add 1 to prefix string
e)Operator ( is scanned, + is popped from stack. Add + to prefix
string, ) is popped from stack.
f)Push / on to stack
g)Add 4 to prefix string
h)As hierarchy are same push / onto stack
i)Add 8 to prefix string
Prefix evaluation
j)As hierarchy of + is less than /,/ is popped from stack
and added to prefix string
k)Second / is also popped similarly and added to prefix
string.+ is pushed to stack.
l)3 is added to prefix string.
m)As hierarchy of + is same as -, - is pushed onto stack.
n) 3 is added to prefix string.
o)* is scanned, its hierarchy is greater than so it is
pushed to stack
p)2 is added to prefix string.
Prefix evaluation
q)As hierarchy of $ is greater than *, it is pushed to
stack.
r)4 is added to prefix string
s)$, *,-,+ are added to the prfix string respectively.
Final expression obtained is:
+-*$4233//84+11
Pop Operation Algorithm: Transfer n-1elements to the other queue and delete
last from queue for performing pop operation.
If Queue Q1 is not empty then transfer n-1 elements from Q1 to Q2 and then,
Dequeue the last element of Q1 and return it.
If Queue Q2 is not empty then transfer n-1 elements from Q2 to Q1 and then,
Dequeue the last element of Q2 and return it.