0% found this document useful (0 votes)
166 views10 pages

Presentation On Stack Data Structure and Its Applications: Name Department Roll No

small ppt about dsa

Uploaded by

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

Presentation On Stack Data Structure and Its Applications: Name Department Roll No

small ppt about dsa

Uploaded by

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

PRESENTATION ON STACK DATA STRUCTURE AND ITS APPLICATIONS

NAME - SAYANJIT MUKHERJEE

DEPARTMENT – COMPUTER SCIENCE

ROLL NO. – 10800122175

GUIDE – GEEKS FOR GEEKS


INDEX :
 STACKS INTRODUCTION

 APPLICATIONS OF STACKS IN LIFE

 RECURSION IN STACKS

 ARITHMETIC OPERATIONS USING STACKS

 INFIX TO POSTFIX CONVERSION


1: What is Stack data structure?

ANS : Stack is a linear data structure that follows a particular


order in which the operations are performed. The order may
be LIFO(Last In First Out) or FILO(First In Last Out). LIFO
implies that the element that is inserted last, comes out first
and FILO implies that the element that is inserted first, comes
out last.
There are many real-life examples of a stack. Consider an
example of plates stacked over one another in the canteen. The
plate which is at the top is the first one to be removed, i.e. the
plate which has been placed at the bottommost position
remains in the stack for the longest period of time. So, it can
be simply seen to follow LIFO(Last In First Out)/FILO(First
In Last Out) order.
2: Different application of stack?
ANS: Application of the Stack:

 A Stack can be used for evaluating expressions consisting of


operands and operators.

 Stacks can be used for Backtracking, i.e., to check


parenthesis matching in an expression.

 It can also be used to convert one form of expression to


another form.
 Evaluation of Arithmetic Expressions
 It can be used for systematic Memory Managemet.
 Backtracking

 Delimiter Checking

 Reverse a Data

 Processing Function Calls


3.Explain recursion as an application of stack with
Example?
 "Recursion" is technique of solving any problem by calling same function again and again until some breaking (base)
condition where recursion stops and it starts calculating the solution from there on. For eg. calculating factorial of a
given number
 Thus in recursion last function called needs to be completed first.
 Now Stack is a LIFO data structure i.e. ( Last In First Out) and hence it is used to implement recursion.
 The High level Programming languages, such as Pascal , C etc. that provides support for recursion use stack for book
keeping. Recursion is extremely useful and extensively used because many problems are elegantly specified or solved
in a recursive way.
 In each recursive call, there is need to save the
 current values of parameters,
 local variables and
 the return address (the address where the control has to return from the call).
 Also, as a function calls to another function, first its arguments, then the return address and finally space for local
variables is pushed onto the stack.
 Recursion is extremely useful and extensively used because many problems are elegantly specified or solved in a
recursive way.
 The example of recursion as an application of stack is keeping books inside the drawer and the removing each book
recursively.
Factorial Function
To illustrate recursive functions, consider calculating the factorial of an integer.
The product of the positive integers from 1 to n is called n factorial denoted by
n!
. To find n!, multiply the number by the factorial of the number that is one less than
the number.
That is,
n! = n * (n - 1)!
Assume we need to find the value of 5!.
Then,
5! = 5 * 4!
, where
4! = 4 * 3!
Therefore,
5! = 5 * 4 * 3!
Expanding further, we get
5! = 5 * 4 * 3 * 2 * 1!
We can now write a recursive function that computes the factorial of a number. Here
the base case is when
n=1
, because the result will be 1 as
1! = 1
. The recursive case of the factorial function will call itself, but with a smaller value of
n, as
factorial(n) = n factorial (n–1).
 Execution of Arithmetic expressions using stack and its example
 Traverse the expression:

 1.1 If the character is an operand, push it into the stack.

 1.2 If the character is an operator, pop the 2 top most elements from the stack and perform the
operation. Push the result back to the stack.

 Once the expression is fully traversed, the element in the stack is the result.

Time complexity: O(N)

We traverse the entire string exactly once. Also, we perform a maximum of 2n push/pop operations, which
means that an element goes into the stack and comes out of the stack(2n operations for n elements). This
results in a time complexity of O(n).

Space complexity: O(N)

We use an auxiliary stack which can contain a maximum of N/2 elements. Hence, the space complexity of the
algorithm is O(N).
Step by Step Example
 Given expression is: 5 + 3 * 7.

 Step 1 is to change this infix expression to postfix: 5 3 7 *


+

 Step 2: Stack S = [ ], traverse the string:


5 : Operand, push into the stack, S = [5], top = 5
3 : Operand, push into the stack, S = [5, 3], top = 3
7 : Operand, push into the stack, S = [5, 3, 7], top = 7
* : Operator, pop top two elements, op1 = 7, op2 = 3.
Stack after pop operations S = [5], top = 5. Now, we
push the result of op1 * op2, i.e 7 * 3 = 21 into the stack.
S = [5, 21], top = 21
+ : Operator, pop top two elements, op1 = 21, op2 = 5.
Stack after pop operations S = []. Push the result of op1
+ op2 into the stack, i.e 21 + 5 = 26, S = [26]

 The string has been completely traversed, the stack


contains only 1 element which is the result of the
expression = 26.
CONVERSION OF INFIX EXPRESSION INTO POSTFIX EXPRESSION
Below are the steps to implement the above idea:

 Scan the infix expression from left to right.

 If the scanned character is an operand, put it in the postfix expression.

Otherwise, do the following


 If the precedence and associativity of the scanned operator are greater than the precedence
and associativity of the operator in the stack [or the stack is empty or the stack contains a
‘(‘ ], then push it in the stack. [‘^‘ operator is right associative and other operators like
‘+‘,’–‘,’*‘ and ‘/‘ are left-associative].

 Check especially for a condition when the operator at the top of the stack and the scanned
operator both are ‘^‘. In this condition, the precedence of the scanned operator is higher due
to its right associativity. So it will be pushed into the operator stack.

 In all the other cases when the top of the operator stack is the same as the scanned operator,
then pop the operator from the stack because of left associativity due to which the scanned
operator has less precedence.

 Else, Pop all the operators from the stack which are greater than or equal to in precedence
than that of the scanned operator.
 After doing that Push the scanned operator to the stack. (If you encounter parenthesis while
popping then stop there and push the scanned operator in the stack.)
 If the scanned character is a ‘(‘, push it to the stack.
 If the scanned character is a ‘)’, pop the stack and output it until a ‘(‘ is encountered, and
discard both the parenthesis.
 Repeat steps 2-5 until the infix expression is scanned.
 Once the scanning is over, Pop the stack and add the operators in the postfix expression
until it is not empty.
 Finally, print the postfix expression.
Infix to postfix with illustration

Thank you

You might also like