Stack Data Structure
Stack Data Structure
Unit III
Chapter 1: Stack
• Stack is one of the most commonly used linear data
structures of variable size .
• As in the linear data structure arrays ,The insertion and
deletion of an element can take place at any position of
the array ,
• But in the case of Stack, the insertion and deletion of an
element can occur at only one end known as Top .
• In Stack , Insertion operation is known as push.
• And Deletion operation is known as pop.
• Stack is also called Last In First Out (LIFO) list.
• It means that the last item added to stack will be first
item to be removed from the stack.
Stack of Books
Operations of Stack
• Push :
– Push operation refers to the insertion of a new element
will be inserted on the top of the stack
– We can perform Push only when the stack is not full
– when the stack is full and an attempt is made to insert a
new element into the stack then this condition is known a
Stack Overflow
• Pop :
– Pop operation refers to the removal of an element from te
top of the stack .
– We can perform Pop operation only when stack is not
empty
– When stack is empty and an attempt is made to remove an
element from stack then this condition is known as Stack
Underflow
Application of Stack
• Parsing :When a statement written in a
programming language is input, it is parsed by a
compiler to check whether or not it is syntactically
correct and to extract components if it is correct.
• Expression Conversion (Infix to Postfix or
Prefix)
Memory Representation of Stack
Memory representation of
Stack
• In this setup ,it will be possible for one stack to consume more
than half the space allocated to both the stacks.
• The overflow will occur only when the total size of two stacks
exceeds the space allocated to both the stacks at a particular time
• This approach will decrease the number of overflows without
increasing the total amount of space reserved for the two stacks
Linked List Representation of Stack
• Stack can also be implemented using the linked list .By
Implementing the stack ,we can eliminate the drawbacks which
come across while implementing the stack using an array.
• That is,while implementing the stack using the linked list ,there
is no need to know in advance about the maximum size of the
stack.
• Linked List representation of the stack allows it to grow without
any prior fixed limit.
Application of Stack
• Evaluation of Arithmetic Expressions
• Important application of stack is the compilation of
arithmetic expressions in the programming languages .
• The compiler must be able to translate the expressions
which is written in the usual notations known as Infix
Notation to a form which is known as Reverse polish
notation (Postfix Notation)
• Compilers accomplish this task of notation conversion
i.e Infix to postfix with the help of the stack.
Infix Notation
• The most common notation that is used while
expressing arithmetic expression is Infix notation.
• In this notation ,operator is placed between its operand
.
• For example ,to multiply m an n we write m*n.
• While solving the infix notation the main consideration
is the precedence of the operators and associativity.
Operator Priority
Priority No Operators
1 Brackets (){}
2 Exponents ^ $ ↑
3 Multiplication * and division /
4 Addition + ,Subtraction -
1>2>3>4
Postfix Notation
• The postfix notation is also known as reverse polish
notation
• In this notation ,operators is placed after its operands.
• Examples
Infix Postfix
A+B AB+
A*B+C AB*C+
A+B*C ABC*+
Examples
Iin =(a-b)/c
=[ab-]/c
Ipost =ab-c/
Iin = (x-y)*((z+v)/f)
=[xy-]*([zv+]/f)
=[xy-]*[zv+f/]
Ipost =xy-zv+f/*
Infix to Postfix conversion with the
Stack
Infix to Postfix Using STACK
• Infix Notation : A+B*C
*
+
Consider Iin = (x-y)*((z+v)/f)
Characters Stack Postfix Expression Operation
( Push (
x x No operation
Print to o/p
-
x Push -
y xy No operation
Print to o/p
+ xy-z Push +
V xy-zv No operation
Print to o/p
) Pop all chr
xy-zv+ between ( and )
/ xy-zv+ Push /
f No operation
xy-zv+f print to o/p
--
xy-zv+f/* Pop all chr
from stack*
Ipost =xy-zv+f/*
Prefix Notation
• This Notation is also popular with the name polish
Notation ,which is named after polish Mathematician
Jan Lukasiewicz .
• This polish mathematician developed a system in
which mathematical expression can be specified
without parenthesis by placing the operator before or
after its operand.
• In prefix notation ,operators is placed before its
operands .
• For example to multiply m and n we write *mn
Iin =(a-b)/c
=[-ab]/c
Ipost =/-abc
Iin = (x-y)*((z+v)/f)
=[-xy]*([+zv]/f)
=[-xy]*[/+zvf]
Ipost =*-xy/+zvf
Conversion from Infix to prefix
notation
• Step 1: Reverse the infix expression i.e A+B*C
will become C*B+A. Note while reversing each
‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘.
• Step 2: Obtain the postfix expression of the
modified expression i.e CB*A+.
• Step 3: Reverse the postfix expression. Hence
in our example prefix is +A*BC
Evaluation of Postfix Notation