2. What is a STACK ?
Ordered list of elements of same data type
Linear list
Top
Last-In First-Out (LIFO) principle.
1
2
3
4
0
Insertion Deletion
Top
3. STACK
Stack is an abstract data type with a bounded(predefined)
capacity.
It is a simple data structure that allows adding and removing
elements in a particular order.
The order may be LIFO(Last In First Out) or FILO(First In Last
Out).
The element which is placed (inserted or added) last, is accessed
first.
Every time an element is added, it goes on the top of the stack
and the only element that can be removed is the element that is
at the top of the stack, just like a pile of objects.
4. Basic features of Stack
1.Stack is an ordered list of similar data type.
2.Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out).
3.push() function is used to insert new elements into the Stack and pop() function is used
to remove an element from the stack. Both insertion and removal are allowed at only one
end of Stack called Top.
4.Stack is said to be in Overflow state when it is completely full and is said to be
in Underflow state if it is completely empty.
5. 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.
6. Applications of stack
• Balancing of symbols
• Infix to Postfix /Prefix conversion
• Redo-undo features at many places like editors, photoshop.
• Forward and backward feature in web browsers
• Used in many algorithms like Tower of Hanoi, tree traversals, stock span
problem, histogram problem.
• Other applications can be Backtracking, Knight tour problem, rat in a maze, N
queen problem and sudoku solver
• In Graph Algorithms like Topological Sorting and Strongly Connected Components
7. Push Operation
• Step 1 − Checks if the stack is full.
• Step 2 − If the stack is full, produces an error and exit.
• Step 3 − If the stack is not full, increments top to point next empty space.
• Step 4 − Adds data element to the stack location, where top is pointing.
• Step 5 − Returns success.
The process of putting a new data element onto stack is known as a
Push Operation.
8. Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
B
A
Max= 4
Top= 2
Let us have Max= 4, Top=2 Ele=C
Step 1. Top=Max
2=4
2=4false
Go to Else Part
a. Top=Top+1
=2+1
Top=3
b. Stack[top]=Ele
=C
Stack[3]=C
Step 2. Return
C
B
A
Max= 4
Top= 3
Let us have Max= 4, Top=4 Ele=E
Step 1. Top=Max
4=4 True
Print OVERFLOW
EXIT
Step 2. Return
D
C
B
A
Max= 4
Top= 4
Push( stack, MAX, Top, Ele)
{
IF Top=MAX then
Print OVERFLOW” and
Exit
ELSE
Set Top=Top+1
Stack[Top]= Ele
Return
}
CASE 2:
9. POP- OPERATION
• Step 1 − Checks if the stack is empty.
• Step 2 − If the stack is empty, produces an error and exit.
• Step 3 − If the stack is not empty, accesses the data element at
which top is pointing.
• Step 4 − Decreases the value of top by 1.
• Step 5 − Returns success.
Accessing the content while removing it from the stack, is known as a Pop
Operation.
10. POP Procedure: POP(STACK, TOP, ITEM)
1.If TOP=0 then
Print UNDERFLOW and Exit.
Else
Set Item:=STACK[TOP]
Set TOP:=TOP-1
2. Return
C
B
A
Max= 4
Top= 3
Let Max=4 TOP=3
Step 1: Top=0
3=0False
Go to ELSE Part
Item=Stack[Top]
= Stack[3]
Item= C
Top=Top-1
=3-1=2
Top=2
2. Return
B
A
Max= 4
Top= 2
CASE 1
Max= 4
Top= 0
CASE 2
Let Max=4 TOP=0
Step 1: Top=0
0=0True
Print UNDERFLOW
2. Return
11. 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
12. 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+.
13. 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
14. • 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.
15. 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
16. 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.
17. INFIX EXPRESSION: a+(b-c/d)-e
Read char a—operand (output it)
stack->empty
a
Read char +operator push (+)
Read char (operator push ( ( )
Read char b—operand (output it)
a b
Read char - operator push (-)
Read char c—operand (output it)
-
( a b c
-Read char /operator push (/)
(
+
+
+
(
+
(
+
-
(
+
-
(
+
/
-
(
+
Read char d—operand (output it)
a b c d
Read char )operator pop ( upto ( )
a b c d / -
Read char - operator push (-)
a b c d / -
- and + have equal precedence but + have higher order. So
Pop + and Push -
a b c d / - +
Read char e—operand (output it)
-
( a b c d / - + e
- Expression scanned completely ,pop element from stack
until it will empty.
+
+ a b c d / - + e -
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
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
POSTFIX EXPRESSION: a b c d / - + e -