You are on page 1of 16

Stack

&
it’s operations

Manish B. Gudadhe ,St. Vincent Pallotti College of Engineering & Technology, Nagpur
• Stack is a linear data structure which follows a particular order in which
the operations are performed.
• The order may be LIFO(Last In First Out) or FILO (First In Last Out).
Mainly the following three basic operations are performed in the stack:
• Push: Adds an item in the stack. If the stack is full, then it is said to be
an Overflow condition.
• Pop: Removes an item from the stack. The items are popped in the
reversed order in which they are pushed. If the stack is empty, then it
is said to be an Underflow condition.
• Peek or Top: Returns top element of stack.
• isEmpty: Returns true if stack is empty, else false.
PUSH POP

TOP
Algorithm for PUSH operation
• Check if the stack is full or not.
• If the stack is full, then print error of overflow and exit the program.
• If the stack is not full, then increment the top and add the element.

push(int x)
{
if(top >= 10)
{
Printf( “Stack Overflow \n”);
}
else
{
top++;
a[top] = x;
Printf( “Element Inserted \n”);
}
}
Algorithm for POP operation
• Check if the stack is empty or not.
• If the stack is empty, then print error of underflow and exit the program.
• If the stack is not empty, then print the element at the top and decrement the top.

pop()
{
if(top < 0)
{
cout << "Stack Underflow \n";
return 0;
}
else
{
int d = a[top--];
return d;
}
}
Position of Top Status of Stack
-1 Stack is Empty
0 Only one element in Stack
N-1 Stack is Full
N Overflow state of Stack
Stack operations animation
Expression Parsing / Evaluation of expression

The way to write arithmetic expression is known as a notation.

These notations are −

1. Infix Notation

2. Prefix (Polish) Notation

3. Postfix (Reverse-Polish) Notation


Sr.No. Infix Notation Prefix Notation Postfix Notation

1 a+b +ab ab+

2 (a + b) ∗ c ∗+abc ab+c∗

3 a ∗ (b + c) ∗a+bc abc+∗

4 a/b+c/d +/ab/cd ab/cd/+

5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗

6 ((a + b) ∗ c) - d -∗+abcd ab+c∗d-


Sr.No. Operator Precedence Associativity
1 Exponentiation ^ Highest Right Associative
2 Multiplication ( ∗ ) & Second Highest Left Associative
Division ( / )
3 Addition ( + ) & Lowest Left Associative
Subtraction ( − )
(A + B) * C + D / (E + F * G) – H

AB+ * C + D / (E + F * G) – H

AB+ * C + D / (E + FG*) – H

AB+ * C + D / EFG*+ – H

AB+ C * + D / EFG*+ – H

AB+ C * + DEFG*+ / – H

AB+ C * DEFG*+ / + – H

AB+ C * DEFG*+ / + H-
(A + B * C – D) / (E * F)

(A + B C* - D) / (E * F)

(A B C* + - D) / (E * F)

A B C* + D - / (E * F)

A B C* + D - / E F *

A B C* + D - E F * /
Algorithm
Step 1 : Scan the Infix Expression from left to right.
Step 2 : If the scanned character is an operand, append it with final Infix to Postfix string.
Step 3 : Else,
Step 3.1 : If the precedence order of the scanned(incoming) operator is greater than the
precedence order of the operator in the stack (or the stack is empty or the stack contains a
‘(‘ or ‘[‘ or ‘{‘), push it on stack.
Step 3.2 : Else, Pop all the operators from the stack which are greater than or equal to in
precedence than that of the scanned operator. After doing that Push the scanned
operator to the stack. (If you encounter parenthesis while popping then stop there and
push the scanned operator in the stack.)
Step 4 : If the scanned character is an ‘(‘ or ‘[‘ or ‘{‘, push it to the stack.
Step 5 : If the scanned character is an ‘)’or ‘]’ or ‘}’, pop the stack and and output it until a ‘(‘
or ‘[‘ or ‘{‘ respectively is encountered, and discard both the parenthesis.
Step 6 : Repeat steps 2-6 until infix expression is scanned.
Step 7 : Print the output
Step 8 : Pop and output from the stack until it is not empty.
A * (B + C) * D

Move Curren Ttoken Stack Output


1 A empty A
2 * * A
3 ( (* A
4 B (* AB
5 + +(* AB
6 C +(* ABC
7 ) * ABC+
8 * * ABC+*
9 D * ABC+*D
10 empty  
Input Stack Output
A+(B*C-(D/E-F)*G)*H Empty -
+(B*C-(D/E-F)*G)*H Empty A
(B*C-(D/E-F)*G)*H + A
B*C-(D/E-F)*G)*H +( A
*C-(D/E-F)*G)*H +( AB
C-(D/E-F)*G)*H +(* AB
-(D/E-F)*G)*H +(* ABC
(D/E-F)*G)*H +(- ABC*
D/E-F)*G)*H +(-( ABC*
/E-F)*G)*H +(-( ABC*D
E-F)*G)*H +(-(/ ABC*D
-F)*G)*H +(-(/ ABC*DE
F)*G)*H +(-(- ABC*DE/
F)*G)*H +(-(- ABC*DE/
)*G)*H +(-(- ABC*DE/F
*G)*H +(- ABC*DE/F-
G)*H +(-* ABC*DE/F-
)*H +(-* ABC*DE/F-G
*H + ABC*DE/F-G*-
H +* ABC*DE/F-G*-
End +* ABC*DE/F-G*-H
End Empty ABC*DE/F-G*-H*+

You might also like