Stack Applications June 2012
Stack Applications June 2012
Stack Behaviors
The behavior of putting an item on the stack is called push.
The behavior of removing and item from the stack is called pop.
Example
What stack exists after executing the following commands?
o push(3)
o push(6)
o push(8)
o push(1)
o pop()
o pop()
o push(14)
Arithmetic Expressions
Arithmetic expressions have
o Operands (variables or numeric constants).
o Operators
Binary : +, -, *, / ,%
Unary: Priority convention:
o Unary minus (sign for negative numbers) has highest priority
o *, /, %
have medium priority
o +, have lowest priority
Infix, Prefix, Postfix
Consider two case studies that relate to evaluating arithmetic expressions
o Postfix and infix notation
Expressions normally written in infix form
o Binary operations inserted between their operands
A computer normally scans an expression string in the order that it is input; easier to evaluate an
expression in postfix form
Example: arithmetic expression a & b consists of operands a, b and operator &.
o Infix notation is format where operator is specified in between the two operands. a&b
o Prefix notation is format where operator is specified before the two operands. & a b
o Postfix notation is format where operator is specified after the two operands. Postfix
notation is also called RPN or Reverse Polish Notation. a b &
Advantage of postfix form is that there is no need to group subexpressions in parentheses
No need to consider operator precedence
Arithmetic Expressions
Prefix Notation
Infix Notation
Postfix Notation
+A * B C
A+B*C
ABC*+
* +AB C
(A+B) * C
AB+C*
+ AB C
AB+C
ABC+
A+ B C
A (B+C)
ABC+
2
Boolean Expressions
Boolean expressions have
o Operands (variables of boolean type or boolean constants TRUE, FALSE).
o Operators
Binary : &, V, =>,
Unary:
~
Convention for operator priorities:
o ~ has highest priority,
o & has next priority level,
o v has next priority level,
o => has next priority level,
o has lowest priority.
Infix, Prefix, Postfix for Boolean Expressions
Example: Boolean expression a & b consists of operands a, b and operator &.
o Infix notation is format where operator is specified in between the two operands. a&b
o Prefix notation is format where operator is specified before the two operands. & a b
o Postfix notation is format where operator is specified after the two operands. Postfix
notation is also called RPN or Reverse Polish Notation. a b &
Boolean Expressions
Prefix Notation
vA& B C
& v AB C
v &A B C
& Av B C
Infix Notation
AvB& C
(AvB) & C
A& BvC
A & (BvC)
Postfix Notation
ABC& v
ABvC&
AB& Cv
ABCv&
If (Ch is an operand)
Push value that operand Ch represents onto a stack
Else //Ch is an operator named Op
{
//Evaluate and push the result
Operand1 = top of stack
Pop the stack
Operand2 = top of stack
Pop the stack
Result = Operand1 Op Operand2
Push result onto stack
}
//end if
}//end for
Example 1: Evaluate 5 6 * 10 -
Example 2: Evaluate 3 2 - 4 *
Current Token Stack Content After Processing the
Token (Top is at the right side)
3
3
2
32
1
4
*
14
4
Exercise:
Evaluate the following postfix expressions:
1) 1 2 3 + * (Ans 5)
2) A * (B + C) When A = 2, B = 3 and C = 4 (Postfix ABC + *) (Ans 14)
3) ABC++ when A =1, B = 2, C = 3 (Ans 6)
4) A + (B - C)* D When A = 7, B= 3, C = 12, D = -5 (Postfix ABC D*+) (Ans 52)
Evaluating Boolean Expressions in Postfix Notation
Algorithm is same as for arithmetic expressions:
o INPUT: list of tokens that are either TRUE or FALSE or operators &, V, =>, . List
represents postfix notation of an arithmetic expression.
4
Evaluate:
True True & False =>
Current Token Stack Content After
Processing the Token
(Top is at the right side)
True
True
True
True True
&
True
False
=>
True False
False
a) Get the next input Token (constant, variable, arithmetic operator, left and right parenthesis) in the
infix expression.
b) If Token is
i)
a left parenthesis:
Push it onto the stack
ii)
a right parenthesis: Pop and display stack elements until a left parenthesis is
popped, but do not display it.
(It is an error if the stack becomes empty with no left parenthesis
found.)
iii)
an operator:
If the stack is empty or Token has a higher priority than the top
stack, push Token onto the stack. Otherwise, pop and display the
top stack element: then repeat the comparison of Token with the
new top stack item.
A left parenthesis in the stack is assumed to have a lower priority
than that of operators. This step orders the operators by precedence
and in accordance with left-to-right association.
iv)
an operand:
Display it.
c) When the end of the infix expression is reached, pop and display stack items until the stack is
empty
Example 1: Convert x1 + 2.5 * count /3 to Postfix
Infix
Stack(bot>top) Postfix
A*B(CD)+E
*B(C+D)+E
B(C+D)+E
(C+D)+E
(C+D)+E
(C+D)+E
C+D)+E
+D)+E
D)+E
)+E
+E
+E
E
empty
empty
*
*
empty
(
(
(+
(+
empty
+
+
empty
empty
A
A
AB
AB*
AB*
AB*
AB*C
AB*C
AB*CD
AB*CD+
AB*CD+
AB*CD+
AB*CD+E
AB*CD+E+
7
Example 4:
Convert infix expression into postfix (7+1)*(6+ (3-2)) >> POSTFIX
TOKEN
(+
(+
71
Display
71+
71+
*(
71+
*(
71+6
*(+
71+6
*(+(
71+6
*(+(
71+63
*(+(-
71+63
*(+(-
71+63
*(+
71+63-
71+63-+
7 1 + 6 3 - + displayed
o Stack content *
Add that to display
o 7 1 + 6 3 - + * is POSTFIX
Exercise
Convert the following infix expressions to postfix:
a) 7-(2*3+5)*(8-4/2) (Ans 723*5+842/-*- )
b) A- ( B + C * D ) / E
c) A + (B * C / D) * E
d) B + C * D
e) A * (B / C / D) + E
Balancing Parentheses in Infix Notation
Example
a * ( b + c ) (( a + b ) / c))
The number of left parentheses must be equal to the number of right parentheses
Checking Parentheses
Create an empty stack
Until a special symbol appears (indicating the end of expression) do
Read one by one token from the input expression
If token is{
If the stack is empty push token to the stack
8
If the stack is not empty pop the top stack element and
If it is { then read next token
Otherwise type Rule 1 is not satisfied at position
]
int stack::isempty(){
return (tos==0?1:0);
}
9
int stack::isfull(){
return (tos==SIZE?1:0);
}
void stack::push(int i){
if(!isfull()){
a[tos]=i;
tos++;
}
else{
cerr<<"Stack overflow error ! Possible Data Loss !";
}
}
int stack::pop(){
if(!isempty()){
return(a[--tos]);
}
else
{
cerr<<"Stack is empty! What to pop...!";
}
return 0;
}
void main(){
stack s;
int ch=1,num;
while(ch!=0){
cout<<"Stack Operations Main Menu\n1.Push\n2.Pop\n3.IsEmpty\n4.IsFull\n0.Exit\n";
cin>>ch;
switch(ch){
case 0:
exit(1); //Normal Termination of Program
case 1:
cout<<"Enter the number to push"<<endl;;
cin>>num;
s.push(num);
break;
case 2:
cout<<"Number popped from the stack is: "<<s.pop()<<endl;
break;
case 3:
(s.isempty())?(cout<<"Stack is empty."):(cout<<"Stack is not empty.");
break;
case 4:
(s.isfull())?(cout<<"Stack is full."):(cout<<"Stack is not full.");
break;
default:
10
11