This document discusses stacks and their applications. It defines a stack as a Last In First Out (LIFO) data structure where newly added items are placed on top. The core stack operations of PUSH, POP, and PEEK are described. An array implementation of stacks is presented and animations demonstrate push and pop operations. Applications of stacks like checking for balanced braces, converting infix to postfix notation, and postfix calculators are explained with examples. Pseudocode provides an algorithm for infix to postfix conversion using a stack.
ļ¬ What is a Stack
ļ¬ Stack Operations
ļ¬ LIFO Stack
ļ¬ Animation Depicting Push Operation
ļ¬ Animation Depicting Pop Operation
ļ¬ Array Implementation of Stack
ļ¬ Applications of stack
Checking for balanced braces
Converting Infix Expression to Postfix
Postfix calculator
3.
What is aStack?
ļA stack is a Last In, First Out (LIFO) data
structure,objects inserted last are the first to
come out of the stack
ļAnything added to the stack goes on the ātopā
of the stack
ļAnything removed from the stack is taken from
the ātopā of the stack
ļThings are removed in the reverse order from
that in which they were inserted
4.
Stack Operations
ļ¬ PUSH
Adds the object to the top of the stack
ļ¬ POP
Removes the object at the top of the stack
and returns it
ļ¬ PEEK
Returns the top object of the stack but does
not remove it from the stack
ļ¬ ISEMPTY
Returns True if Stack is empty
array Implementation ofStack
A[1] A[2] ā¦. ā¦. ā¦. A[n]
Push X w y x
Stack pointer Stack pointer After many Stack pointer
J=2 J=3 Push Ops J=n
Before Push After Push Stack Full
Pop w y
After many Pop ops
Stack Pointer Stack Pointer Stack Pointer
J=0 J=2 J=3
Stack Empty After Pop Before Pop
9.
Checking for BalancedBraces
ļ¬ ([]({()}[()])) is balanced; ([]({()}[())]) is not
ļ¬ Simple counting is not enough to check
balance
ļ¬ You can do it with a stack: going left to
right,
ļ¬ If you see a (, [, or {, push it on the
stack
ļ¬ If you see a ), ], or }, pop the stack
and check whether you got the
corresponding (, [, or {
ļ¬ When you reach the end, check that the
stack is empty
11.
Converting Infix Expressionsto Equivalent
Postfix Expressions
ļ¬ Infix Expression a+b
ļ¬ Postfix Expression ab+
ļ¬ An infix expression can be evaluated by first
being converted into an equivalent postfix
expression
ļ¬ Facts about converting from infix to postfix
ļ¬ Operands always stay in the same order with
respect to one another
ļ¬ All parentheses are removed
12.
Algorithm : ConvertingInfix Expression to
Postfix using stack
Suppose X is an arithmetic expression written in infix notation.
This algorithm finds the equivalent postfix expression Y.
1. Push "(" onto STACK, and add ")" to the end of X.
2. Scan X from left to right and REPEAT Steps 3 to 6 for each element of X UNTIL the
STACK is empty :
3. If an operand is encountered, add it to Y.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator is encountered, then :
(a) Repeatedly pop from STACK and add to Y each operator (on the top of STACK) which has
the same precedence as or higher precedence than operator.
(b) Add operator to STACK.
/* End of If structure * /
6. If a right parenthesis is encountered, then :
(a) Repeatedly pop from STACK and add to Y each operator (on the top of STACK)
until a left parenthesis is encountered.
(b) Remove the left parenthesis. [Do not add the left parenthesis to Y].
/* End of If structure * /
/* End of Step 2 loop * /
7. END.
13.
A trace ofthe algorithm that converts the infix expression
a - (b + c * d)/e to postfix form
14.
A postfix calculator
ļ¬ When an operand is entered, the calculator
ļ¬ Pushes it onto a stack
ļ¬ When an operator is entered, the calculator
ļ¬ Applies it to the top (one or two, depending
on unary or binary operator) operand(s) of
the stack
ļ¬ Pops the operands from the stack
ļ¬ Pushes the result of the operation on the
stack
15.
The action ofa postfix calculator when evaluating the expression 2 * (3 + 4)
Postfix Notation--- 2 3 4 + *
16.
Acknowledgement
ļ¬ Data Structures BY Tanenbaum
ļ¬ Internet
ļ¬ Computer Science C++ A textbook for class XII -By
Sumita Arora
prepared by-
Seema kaushik,
Computer science department,
DAV public school, sector-14, gurgaon