0% found this document useful (0 votes)
15 views

Stack Data Structure

The document discusses stacks and their applications. Some key points: - A stack is a linear data structure that follows the LIFO (last in, first out) principle. Elements can only be inserted or removed from one end called the top. - Common stack operations are push, which adds an element to the top, and pop, which removes the top element. - Stacks can be implemented using arrays or linked lists. Array implementation uses indexes to track the top, while linked lists grow and shrink dynamically. - Stacks have many applications like parsing expressions, converting infix to postfix notation, and arithmetic expression evaluation. Postfix notation avoids the need for parentheses.

Uploaded by

Rehana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Stack Data Structure

The document discusses stacks and their applications. Some key points: - A stack is a linear data structure that follows the LIFO (last in, first out) principle. Elements can only be inserted or removed from one end called the top. - Common stack operations are push, which adds an element to the top, and pop, which removes the top element. - Stacks can be implemented using arrays or linked lists. Array implementation uses indexes to track the top, while linked lists grow and shrink dynamically. - Stacks have many applications like parsing expressions, converting infix to postfix notation, and arithmetic expression evaluation. Postfix notation avoids the need for parentheses.

Uploaded by

Rehana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Data Structures

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

Array representation Linked list representation


Array representation of Stack
• Representation of stack using an then array is the
simplest form of representation.
• But an array has certain restrictions
– The stack must contain homogeneous data elements
– Need to specify upper bound of the array i.e
Maximum size of the array
Generally ,stack grows and shrinks over time but it is
limited to the space allocated to the array
implementing that stack.
• While implementing a stack using an array ,a variable
Top is used to hold the index of stack’s topmost element .
• Initially ,the stack is empty Top has value equal to zero
• When the first element is inserted into the stack ,the value
of Top is incremented by one
• Each time a new element is inserted into stack ,the value
of Top will be incremented by one before placing the new
element into the stack
• On the other hand each time a deletion is performed ,
variable Top is decremented by one .
• One another variable Max is also used that represent the
maximum number of elements that can be pushed into the
stack.
Algorithm : Pop Operation –Delete an element from
the stack represented by an array ‘S’ and returns the
element ‘Data ’ which is at the top of the stack
Storage of Two stack into a single array
• When stack is implemented using an array ,there is a
need to be allocate sufficient fixed space for the stack .
• If initially we reserve large amount of space for the stack
, then it will decrease the number of oveflows but there
will be wastage of memory as memory remains
unutilized most of the times.
• On the other hand ,if we allocate small amount of
memory space then it can be increase the number of
overflows.
• For resolving such overflows , more memory space can
be added to the stack as and when required.
• But ,this addition of memory will require the time which
may be more expensive than the space saved.
• This Time space trade-off (while implementing the stack
using an array) can be resolved using various techniques ,
One of which is described below,
• Generally ,algorithms require more than one stack . It
may be the situation that , one stack encounters overflow
condition while other stack is far from being full.
• In such a case ,instead of imposing limit of maximum
size on each stack , we can allocate a common pool of
memory which can be used by all the stacks .
• Consider an algorithm which requires two stacks S1 and S2 which
can exist together in the same array
• In such arrangement, The first stack will grow towards the right
while the second one grows towards the left.

• 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

• Postfix Notation : STACK


AB 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- Pop all chr


between ( and )
* xy- Push *
( xy- Push (
( xy- Push (
z No operation
xy-z 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

Pop all chr


) xy-zv+f/ between ( and )

--
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

You might also like