Professional Documents
Culture Documents
5 Stack
5 Stack
Linear list
Top 3
0
STACK
CASE 2:
Push( stack, MAX, Top, Ele)
{
IF Top=MAX then Top= 4 D Max= 4
Print OVERFLOW” and
Let us have Max= 4, Top=4
Exit C Ele=E
ELSE
Set Top=Top+1 B Step 1. Top=Max
Stack[Top]= Ele 4=4 True
Return Print OVERFLOW
A EXIT
}
Step 2. Return
Let us have Max= 4, Top=2
Max= 4 Ele=C
Step 1. Top=Max
2=4
2=4false
B Top= 2 Go to Else Part Max= 4
a. Top=Top+1
A =2+1 C Top= 3
Top=3
b. Stack[top]=Ele B
=C
Stack[3]=C
Step 2. Return
A
POP- OPERATION
Accessing the content while removing it from the stack, is known as a Pop
Operation.
CASE 1 CASE 2
Let Max=4 TOP=3
Max= 4 Step 1: Top=0 Max= 4
Let Max=4 TOP=0
3=0False Step 1: Top=0
C Top= 3 0=0True
Go to ELSE Part
B Item=Stack[Top] Print UNDERFLOW
= Stack[3] Max= 4 2. Return
Item= C
A Top=Top-1 Top= 0
=3-1=2
Top=2 B Top= 2
2. Return
A
EVALUATION OF EXPRESSION
The way to write arithmetic expression is known as a notation.
An arithmetic expression can be written in three different but
equivalent notations.
These notations are −
• Infix Notation
• Prefix (Polish) Notation
• Postfix (Reverse-Polish) Notation
Infix Notation
•We write expression in infix notation, e.g. a - b + c, where operators are used in-
between operands.
Prefix Notation
•In this notation, operator is prefixed to operands, i.e. operator is written ahead of
operands. For example, +ab.
•Prefix notation is also known as Polish Notation.
Postfix Notation
•This is known as Reversed Polish Notation.
•In this notation style, the operator is postfixed to the operands i.e., the operator is
written after the operands. For example, ab+.
Postfix Evaluation Algorithm
• Step 1 − scan the expression from left to right
• Step 2 − if it is an operand push it to stack
• Step 3 − if it is an operator pull operand from stack and perform
operation
• Step 4 − store the output of step 3, back to stack
• Step 5 − scan the expression until all operands are consumed
• Step 6 − pop the stack and perform operation
• Read all the symbols one by one from left to right in the given
Postfix Expression
• If the reading symbol is operand, then push it on to the Stack.
• If the reading symbol is operator (+ , - , * , / etc.,), then perform
TWO pop operations and store the two popped oparands in two
different variables (operand1 and operand2). Then perform reading
symbol operation using operand1 and operand2 and push result
back on to the Stack.
• Finally! perform a pop operation and display the popped value as
final result.
Step 1 − scan the expression from left to right
Step 2 − if it is an operand push it to stack
Step 3 − if it is an operator pull operand from stack and
perform operation
Step 4 − store the output of step 3, back to stack
Step 5 − scan the expression until all operands are consumed
Step 6 − pop the stack and perform operation
Steps to convert infix expression to postfix
1. Scan the given infix expression from left to right.
2. If the scanned character is an operand, then output it.
3. Else,
•If the precedence of the scanned operator is greater than the precedence of the operator in the
top of the stack(or the stack is empty or if the stack contains a ‘(‘ ), push it.
•Else, Pop all operators from the stack whose precedence is greater than or equal to that of the
scanned operator. Then, push the scanned operator to the top of the stack. (If you encounter
parenthesis while popping then stop there and push the scanned operator in the stack.)
4. If the scanned character is ‘(‘, push it to the stack.
5. If the scanned character is ‘)’, pop the stack and output characters until ‘(‘ is encountered, and
discard both the parenthesis.
6. Repeat steps 2-5 until infix expression is scanned completely.
7. Print the output.
8. Pop and output from the stack.
INFIX EXPRESSION: a+(b-c/d)-e Read char d—operand (output it)
/
1. Scan the given infix expression from left to right. -
Read char a—operand (output it)
2. If the scanned character is an operand, then (
stack->empty + abc d
output it.
3. Else, a Read char )operator pop ( upto ( )
•If the precedence of the scanned operator is Read char +operator push (+)
greater than the precedence of the operator in the + abc d/-
top of the stack(or the stack is empty or if the stack +
contains a ‘(‘ ), push it.
Read char (operator push ( ( ) Read char - operator push (-)
•Else, Pop all operators from the stack whose ( -
precedence is greater than or equal to that of the + + abc d/-
scanned operator. Then, push the scanned
operator to the top of the stack. (If you encounter Read char b—operand (output it)
parenthesis while popping then stop there and ( - and + have equal precedence but + have higher order. So
push the scanned operator in the stack.) + ab Pop + and Push -
4. If the scanned character is ‘(‘, push it to the Read char - operator push (-)
stack. - - abc d/-+
5. If the scanned character is ‘)’, pop the stack and (
and output characters until ‘(‘ is encountered, and + Read char e—operand (output it)
discard both the parenthesis. Read char c—operand (output it)
6. Repeat steps 2-5 until infix expression is scanned - - -
completely. - ( ( abcd/-+ e
( + abc - Expression scanned completely ,pop element from stack
7. Print the output.
-Read char /operator push (/) until it will empty.
8. Pop and output from the stack. +
/
( -
+ ( + abcd/-+ e-
+
INFIX EXPRESSION: a+(b-c/d)-e
+
POSTFIX EXPRESSION: a b c d / - + e -