0% found this document useful (0 votes)
32 views15 pages

Unit 02 - Stack 2021-2022 Sem I

The document discusses stacks, which are linear data structures that only allow insertion and deletion at one end. Stacks follow LIFO order and can be implemented using arrays or linked lists. Common stack operations like push, pop, peek, isFull and isEmpty are described. Applications of stacks include recursion, tree traversal, expression evaluation, and infix to postfix notation conversion.

Uploaded by

bhfr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views15 pages

Unit 02 - Stack 2021-2022 Sem I

The document discusses stacks, which are linear data structures that only allow insertion and deletion at one end. Stacks follow LIFO order and can be implemented using arrays or linked lists. Common stack operations like push, pop, peek, isFull and isEmpty are described. Applications of stacks include recursion, tree traversal, expression evaluation, and infix to postfix notation conversion.

Uploaded by

bhfr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Structures

Prof. Anand Jain, [email protected]


Unit 02 : Stack
Stack
❖ Stack Introduction
▪ A linear list data structure which permits insertion or deletion of an
element to occur only at one end is called as Stack.
▪ Insert operation is referred as Push, Delete operation is referred as
Pop
▪ The most and least accessible elements of stack are referred as TOP
and BOTTOM of stack respectively. Push and Pop are performed at
TOP
▪ Since insertion and deletion are allowed to be performed from one
end only, the elements can be removed in opposite order from that
they were added. Because of this, stack is also called as LIFO (Last
In First Out) list.
Stack
❖ Array Representation of Stack
▪ Itemi denotes the ith item in the stack
▪ i and u denotes index range
▪ TOP is the stack position which is u .. MAX
used to perform push and pop
.. Itemi
operations
.. ..
▪ BOTTOM indicates first element of the
.. ..
stack
i+4 Item5 TOP
▪ MAX indicates maximum capacity of
stack
i+3 Item4
❖ Array representation is easy and i+2 Item3
convenient to implement i+1 Item2
❖ Array allows representation of only fixed i item1 BOTTOM
sized stack
Index Stack Elements
Stack
❖ Linked List Representation of Stack
Stack
❖ Stack Operations
▪ Push and Pop are the primary operations of stack
• Push : Pushing (storing) an element on the stack
• Pop : Removing an element from the stack

▪ We need to check the status of stack to make an efficient use of it


• Peek : Get the top element of stack, without removing it
• isFull : Check if stack is full. It is necessary before pushing an
element
• isEmpty : Check if stack is empty. This is necessary before
performing pop operation
Stack
❖ Stack Operations

Image Source : https://www.programiz.com/dsa/stack


Applications of Stack
❖ Recursion
▪ Recursion is a method of solving problems based on the divide and
conquer mentality. The basic idea is that you take the original
problem and divide it into smaller (more easily solved) instances of
itself, solve those smaller instances (usually by using the same
algorithm again) and then reassemble them into the final solution.
▪ Recursion is especially good for working on things that have many
possible branches and are too complex for an iterative approach.
▪ Recursion is useful for the languages (For Ex. : Clojure) that do not
support loop statements.
▪ Recursion is a useful tool, but it can increase memory usage; and
may crash the program due to stack overflow
Applications of Stack

❖ Tree Traversal
▪ Traversing through Directories
Applications of Stack
❖ Recursion to Find
Factorial of a Number
int find_fact(unsigned int i)
{
if(i <= 1)
{
return 1;
}
return i * find_fact (i - 1);
}
Applications of Stack
❖ Evaluation of Arithmetic Expressions
▪ Operands are variables or constants
▪ Operators indicate the operations to be performed on the operands
▪ Operator Precedence and Operator Associativity
Applications of Stack
❖ Notations for Arithmetic Expressions
▪ Infix Notation : When operator appears in between the operands then
the expression is called as an infix expression. For Ex, A + B

▪ Prefix Notation : When operator appears before the operands then


the expression is called as a prefix expression. For Ex, +AB. Prefix
notation is introduced by Polish mathematician Jan Lukasiewicz and
hence also termed as Polish Notation

▪ Postfix Notation : When operator appears after the operands then the
expression is called as a postfix expression. For Ex, AB+. It is also
termed as Reversed Polish Notation
Applications of Stack
❖ Why Prefix/Postfix Notations
▪ Infix notation is easy to understand/read for humans; but additional load of
operator precedence is associated with it. Prefix/Postfix notation is easier to
parse for a machine, and there is no question of operator precedence.

▪ Time Complexity
• Evaluation of Infix Notation → O(n^2)
• Infix to Postfix → O(n)
• Postfix Evaluation → O(n)
• O(n) + O(n) = O(n)
Applications of Stack
❖ Infix to
Postfix
Conversion
using Stack

You might also like