Stack
Stack
STACK
Specifications/ADT of Stack
What is Stack?
Representation of Stacks
Operations on Stacks
Push
Pop
IsEmpty
IsFull
Usage
Evaluation of Expressions
Infix to Postfix Conversion
Limited by Array (Fixed Size)
Link List (Dynamic and Variable size)
What is Stack?
?
Pop Is full
Empty stack ?
Implementation of Stacks
a a a
(2^3 + 6) * 2 – 9 / 3
= (8 + 6) * 2 – 9 / 3
= 14 * 2 – 9 / 3
= 28 – 3
= 25
Infix, Prefix (Polish) and Postfix
(Reverse Polish) Notations
Infix Prefix Postfix
(Polish Notation) (Reverse Polish
Notation)
A+B +AB AB+
Stack is also used to store operators and operands and then pass
to the postfix expression according to their precedence.
A+B*C+(D*E+F)*G
Stack
Output: A
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
Stack
Output: A
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
Stack
Output: AB
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
Stack
Output: AB
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
*
+
Stack
Output: ABC
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
Output: ABC*+
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
(
+
Stack
Output: ABC*+
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
(
+
Stack
Output: ABC*+D
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
Stack
Output: ABC*+D
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
*
(
+
Stack
Output: ABC*+DE
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
Stack
Output: AB*+DE*
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
+
(
+
Stack
Output: ABC*+DE*F
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
Stack
Output: ABC*+DE*F+
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
Stack
Output: ABC*+DE*F+
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
*
+
Stack
Output: ABC*+DE*F+G
Example (Infix to Postfix
Conversion)
A+B*C+(D*E+F)*G
Stack
Output: ABC*+DE*F+G*+
Convert the following infix
expressions into postfix expressions
A$B*C-D+E/F/(G+H)
(A+B)*(C$(D-E)+F)-G
(A+B)*(C-D)
A-B/(C*D$E)
((A+B)*C-(D-E))$(F+G)
A+B*C+(D*E+F)*G
X+6*(Y+Z)^3
(((A-(B+C))*D)$(E+F)
A+(((B-C)*(D-E)+F)/G)$(H-J)
Algorithm (for Infix to Postfix
Conversion) Simple
Initialize a Stack for operators, output list
Split the input into a list of symbols.
for each symbol (left to right):
if it is operand: add to output
if it is '(': push onto Stack
if it is ')': pop & add till '('
if it has '+-*/':
while peek has precedence ≥ it:
pop & add
push onto Stack
pop and add the rest of the Stack.
Algorithm (for Infix to Postfix
Conversion) Most Efficient
stk = empty stack
symb = Read the first character
while(!end of string)
{
if(symb is an operand)
add symb to the postfix string
else
{
while((!empty stack()) && prced(stacktop(), symb))
{top symb = pop(stk);
add symb to the postfix string}
if(empty stack(), || symb!=‘)’)
push(stk, symb)
else
top symb = pop(stk);//end of else
}
symb = get the next char
}//end of while
While(!empty stack())
{top symb=pop(stk);
Add top symb to the postfix string}
Evaluation of Expression
Computer evaluates an expression given in infix notation by
converting it into postfix notation.
Expression is scanned from left to right until the end of the expression
When end of the expression is reached, the top value from the stack is
picked. It is the computed value of the expression
Example (Evaluation of Expression)
Postfix Expression
ABC*+DE*F+G*+
Stack
Example (Evaluation of Expression)
Postfix Expression
ABC*+DE*F+G*+
Stack
Example (Evaluation of Expression)
Postfix Expression
ABC*+DE*F+G*+
Stack
Example (Evaluation of Expression)
Postfix Expression
ABC*+DE*F+G*+
Stack
Example (Evaluation of Expression)
Postfix Expression
ABC*+DE*F+G*+
Stack
Example (Evaluation of Expression)
Postfix Expression
ABC*+DE*F+G*+
Stack
Example (Evaluation of Expression)
Postfix Expression
ABC*+DE*F+G*+
Stack
Example (Evaluation of Expression)
Postfix Expression
ABC*+DE*F+G*+
Stack
Example (Evaluation of Expression)
Postfix Expression
ABC*+DE*F+G*+
Stack
Example (Evaluation of Expression)
Postfix Expression
ABC*+DE*F+G*+
Stack
Example (Evaluation of Expression)
A+B*C+(D*E+F)*G
= 5 + 6 * 9 + (2 * 4 + 8) * 3
= 5 + 54 + (8 + 8) * 3
= 59 + 16 * 3
= 59 + 48
=107
Algorithm (for evaluating the Postfix)
stk = empty stack
symb = get the first value
while(!end of string)
{
if(symb is an operand)
Push(stk, symb);
else
{
operand 2 = pop(stk());
operand 1= pop(stk());
result = apply symb to operand 1 && operand 2
push(stk, result);
} //end of else
symb = get the next value
}// end of while
return(pop(stk));
Infix to Prefix
For Infix to Prefix conversion
Read the input string from right to left.
OR
Convert Infix to Postfix, then reverse the
output string
Do it Yourself????????
Prefix to Postfix Conversion
if(symb is an operand)
push(stk, symb);
else{
operand1 = pop(stk);
operand2 = pop(stk);
strcat(operand1, operand2, symb);
push(operand);
}
return(pop(stk));