09.2 Stack
09.2 Stack
09.2 Stack
Objectives
7 7
7 7
6 6
6 6
6 6
6 6
7 7
7 7
6 6
6 6
6 6
6 6
What
A is is
stack a Stack ?
a collection of data items that can be accessed at
only one end, called top.
Items can be inserted and deleted in a stack only at the top.
The last item inserted in a stack is the first one to be
deleted.
Therefore, a stack is called a Last-In-First-Out (LIFO) data
structure.
PUSH:are
There It istwo
thebasic
process
operations
of inserting
that aare
newperformed
element on the
stacks:
top of a stack.
PUSH
POP
Push an
Element 1
Empty Stack
Push an
Push an
Element 32
Element
3
2
1
POP an
Element
3
3 2
2
1
Answer:
LIFO
List down some real life examples that work on the LIFO
principle.
Answer:
Pile of books: Suppose a set of books are placed one over
the other in a pile. When you remove books from the pile, the
topmost book will be removed first. Similarly, when you have
to add a book to the pile, the book will be placed at the top of
the pile.
Pile of plates: The first plate begins the pile. The second plate
is placed on the top of the first plate and the third plate is
placed on the top of the second plate, and so on. In general, if
you want to add a plate to the pile, you can keep it on the top
of the pile. Similarly, if you want to remove a plate, you can
remove the plate from the top of the pile.
Bangles in a hand: When a person wears bangles, the last
bangle worn is the first one to be removed.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
Ver. 1.0 SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR Session 10
Data Structures and Algorithms
Implementing Stacks
{(a + b) × (c + d) + (c × d)]}
Consider an example.
Suppose the expression is:
{(a + b) × (c + d) + (c × d)]}
Scan the expression from
left to right.
The first entry to be
scanned is ‘{’, which is a left
parenthesis.
Push it into the stack.
{
{(a + b) × (c + d) + (c × d)]}
The next entry to be
scanned is ‘(’, which is a left
parenthesis.
Push it into the stack.
The next entry is ‘a’, which
is an operand. Therefore, it
is discarded.
The next entry is ‘+’, which
is an operator. Therefore, it
is discarded. (
The next entry is ‘b’, which {
is an operand. Therefore, it
is discarded.
{(a + b) × (c + d) + (c × d)]}
The next entry to be
scanned is ‘)’, which is a
right parenthesis
POP the topmost entry from
the stack.
Match the two brackets.
( )
Brackets Matched
(
{
Therefore it is discarded
The next entry to be scanned
is ‘d’, which is an operand.
Therefore it is discarded
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
Ver. 1.0 SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR Session 10
Data Structures and Algorithms
Implementing Stacks (Contd.)
{(a + b) × (c + d) + (c × d)]}
The next entry to be
scanned is ‘)’, which is a
right parenthesis.
POP the topmost element
from the stack.
Match the two brackets.
( )
Brackets Matched
(
{
Therefore, it is discarded.
The next entry to be scanned
is ‘d’, which is an operand.
Therefore, it is discarded.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
Ver. 1.0 SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR Session 10
Data Structures and Algorithms
Implementing Stacks (Contd.)
{(a + b) × (c + d) + (c × d)]}
The next entry to be
scanned is ‘)’, which is a
right parenthesis.
POP the topmost element
from the stack.
Match the two brackets.
( )
Brackets Matched
(
{
{(a + b) × (c + d) + (c × d)]}
The next entry to be
scanned is ‘]’, which is a
right parenthesis.
POP the topmost element
from the stack.
Match the two brackets.
{ ]
Answer:
top
Problem Statement:
Write a program that accepts an infix expression, and then
converts it into a postfix expression. You can assume that the
entered expression is a valid infix expression.