Unit 5 Fds 2023

Download as pdf or txt
Download as pdf or txt
You are on page 1of 99

“STACK”


Prepared By
Prof. Anand N. Gharu
(Assistant Professor)
Computer Dept.
CLASS : SE COMPUTER 2019 23 Oct. 2023
SUBJECT : FDS (SEM-I) Note: The material to prepare this presentation has been taken from internet and are generated only
1
.UNIT :V .
for students reference and not for commercial use
Syllabus
• Basic concept
• Stack Abstract Data Type
• Representation of Stacks Using Sequential Organization
• stack operations, Multiple Stacks
• Applications of Stack- Expression Evaluation and Conversion
• Polish notation and expression conversion
• Need for prefix and postfix expressions
• Postfix expression evaluation
• Linked Stack and Operations
• Recursion- concept
• Variants of recursion- direct, indirect, tail and tree
• Backtracking algorithmic strategy
• Use of stack in backtracking 2
CONCEPT OF STACKs
Definition :
“ Stack is data structure in ehich addition and
removal of an element is allowed at the same end is
called as top of stack.”
- Stack is also called as Last In First Out(LIFO)
list.
- It means element which get added at last will be
removed first.
e.g.

3
Example : https://youtu.be/-KQpk-dIA8s
CONCEPT OF STACKs
Example :

4
Components of Stack
• Top is a variable which refers to
last position in stack.

• Element is component which has


data.
• MaxStack is variable that describes
maximum number of elements in a
stack.
Mr. Anand Gharu(MET's IOE, Nashik) 5
Main Operation

STACK

PUSH POP
Add data to Take data from
element in element in
stack. stack

Mr. Anand Gharu(MET's IOE, Nashik) 6


Kinds of Operation

• Stack Operation in array form

• Stack Operation in Linked list


form

Mr. Anand Gharu(MET's IOE, Nashik) 7


STACK AS AN ADT
Stack is an abstract data type which is defined
by the following structure and operation.

Stack operation :
1. createstack( )
2. Push( )
3. Pop( )
4. Peek( )
5. IsEmpty( )
6. IsFull( )
7. Size( ) 8
STACK AS AN ADT
Createstack - it create new empty stack.
push( ) − Pushing (storing) an element on the stack.
pop( ) − Removing an element from the stack.
peek( ) − get the top data element of the stack,
without removing it.
isFull( ) − check if stack is full.
isEmpty( ) − check if stack is empty.
Size() – return the number of item in the stack.
9
Example Stack as ADT : https://youtu.be/0jhUesQToOc
STACK AS AN ADT
1. Initializing stack :

1
0
STACK AS AN ADT
2. IsFull( ) stack :
check stack is full or not?

1
1
STACK AS AN ADT
3. IsEmpty( ) stack :
check stack is empty or not?

1
2
STACK AS AN ADT
4. Push( ) stack : add element in stack

1
3
STACK AS AN ADT
5. pop( ) stack : remove element from stack

1
4
STACK AS AN ADT
6. display( ) stack :displaying stack

1
5
ALGORITHM TO IMPLEMENT
STACK USING ARRAY
Step 1 : start
Step 2 : Display Menu : 1. push 2. pop 3. display
4. exit.
Step 3 : read choice
Step 4 : if choice 1 then call push ( )
if choice 2 then call pop ( )
if choice 3 the call display ( )
if choice 4 then call exit ( )
default : Invalid choice
Step 5 : read choice again
Step 6 : If choice between 1- 3
repeat step 4 else stop 1
6
MULTIPLE STACKs
“When a stack is created using single array, we can
not able to store large amount of data, thus this
problem is rectified using more than one stack in
the same array of sufficient array. This technique is
called as Multiple Stack”

1
7
MULTIPLE STACKs
Example : When an array of STACK[n] is used to
represent two stacks, say Stack A and Stack B. Then
the value of n is such that the combined size of both
the Stack[A] and Stack[B] will never exceed n.
Stack[A] will grow from left to right, whereas
Stack[B] will grow in opposite direction i.e. right
to left.

1
8
APPLICATION OF STACKs
 Convert infix expression to postfix and prefix
expressions
 Evaluate the postfix expression
 Reverse a string
 Check well-formed (nested) parenthesis
 Reverse a string
 Process subprogram function calls
 Parse (analyze the structure) of computer programs
 Simulate recursion
 In computations like decimal to binary conversion
 In Backtracking algorithms (often used in
optimizations and in games) 1
9
REVERSING OF STACKs
Algorithms :
Step 1 : start
Step 2 : accept string
Step 3 : insert string into character by character
using push method
Step 4 : remove character from stack one by one
and print using pop method
Step 5 : stop
2
0
REVERSING OF STACKs

2
1
polish notation – Expression
Evaluation and conversion
Notation is a way of writing arithmatic expression

Concepts : polish is a way of expressing arithmatic


expression that avoids the use of brackets to define
periorities for evaluation of operators.

There are three notation :


1. Infix notation
2. Prefix notation
3. Postfix notation
Mr. Anand Gharu(MET's IOE, Nashik) 22 22
polish notation – Expression
Evaluation and conversion

The example ex…pression in various forms-


infix, prefix and postfix

 The postfix expressions can be evaluated easily hence infix


expression is converted into postfix expression using stack.
Mr. Anand Gharu(MET's IOE, Nashik) 23 22
The following operators are written is in
descending order of their precedence:

 Exponentiation ^, Unary +, Unary –, and not ~


 Multiplication * and division /

 Addition + and subtraction –

 <, £ , =, ¹, ³, >

 AND

 OR

Mr. Anand Gharu(MET's IOE, Nashik) 24 24


The Operators and priorities

Mr. Anand Gharu(MET's IOE, Nashik) 25 25


Algorithm Infix to postfix conversion

Mr. Anand Gharu(MET's IOE, Nashik) 26 26


Infix to postfix conversion
• Manual algorithm for converting
infix to postfix
(a + b) * c
o Write with parentheses to force correct
operator precedence ((a + b) * c)

o Move operator to right inside parentheses


((a b + ) c * )

o Remove parentheses
Mr. Anand Gharu(MET's IOE, Nashik)
ab+c* 27 27
Infix to postfix conversion
infixVect
(a+b-c)*d–(e+f)

postfixVect

Example : https://youtu.be/OVFwgYrMShw
https://youtu.be/vq-nUF0G4fI
Mr. Anand Gharu(MET's IOE, Nashik) https://youtu.be/vXPL6UavUeA 28
Infix to postfix conversion
stackVect

infixVect
a+b-c)*d–(e+f)

postfixVect

Mr. Anand Gharu(MET's IOE, Nashik) 29


Infix to postfix conversion
stackVect

infixVect
+b-c)*d–(e+f)

postfixVect
a

Mr. Anand Gharu(MET's IOE, Nashik) 30


Infix to postfix conversion
stackVect

infixVect
b-c)*d–(e+f)

postfixVect
a

+
(

Mr. Anand Gharu(MET's IOE, Nashik) 31


Infix to postfix conversion
stackVect

infixVect
-c)*d–(e+f)

postfixVect
ab

+
(

Mr. Anand Gharu(MET's IOE, Nashik) 32


Infix to postfix conversion
stackVect

infixVect
c)*d–(e+f)

postfixVect
ab+

-
(

Mr. Anand Gharu(MET's IOE, Nashik) 33


Infix to postfix conversion
stackVect

infixVect
)*d–(e+f)

postfixVect
ab+c

-
(

Mr. Anand Gharu(MET's IOE, Nashik) 34


Infix to postfix conversion
stackVect

infixVect
*d–(e+f)

postfixVect
ab+c-

Mr. Anand Gharu(MET's IOE, Nashik) 35


Infix to postfix conversion
stackVect

infixVect
d–(e+f)

postfixVect
ab+c-

Mr. Anand Gharu(MET's IOE, Nashik) 36


Infix to postfix conversion
stackVect

infixVect
–(e+f)

postfixVect
ab+c-d

Mr. Anand Gharu(MET's IOE, Nashik) 37


Infix to postfix conversion
stackVect

infixVect
(e+f)

postfixVect
ab+c–d*

Mr. Anand Gharu(MET's IOE, Nashik) 38


Infix to postfix conversion
stackVect

infixVect
e+f)

postfixVect
ab+c–d*

(
-

Mr. Anand Gharu(MET's IOE, Nashik) 39


Infix to postfix conversion
stackVect

infixVect
+f)

postfixVect
ab+c–d*e

(
-

Mr. Anand Gharu(MET's IOE, Nashik) 40


Infix to postfix conversion
stackVect

infixVect
f)

postfixVect

+ ab+c–d*e

(
-

Mr. Anand Gharu(MET's IOE, Nashik) 41


Infix to postfix conversion
stackVect

infixVect
)

postfixVect

+ ab+c–d*ef

(
-

Mr. Anand Gharu(MET's IOE, Nashik) 42


Infix to postfix conversion
stackVect

infixVect

postfixVect
ab+c–d*ef+

Mr. Anand Gharu(MET's IOE, Nashik) 43


Infix to postfix conversion
stackVect

infixVect

postfixVect
ab+c–d*ef+-

Mr. Anand Gharu(MET's IOE, Nashik) 44


Infix to postfix conversion
Infix Expression: a + (b*c).

Mr. Anand Gharu(MET's IOE, Nashik) 45


Resultant Postfix Expression: abc*+
Infix to postfix conversion
Infix Expression: A * B + C * D.

Mr. Anand Gharu(MET's IOE, Nashik) 46


Resultant Postfix Expression: abc*+
Infix to postfix conversion

Mr. Anand Gharu(MET's IOE, Nashik) 47


Infix to postfix conversion

Mr. Anand Gharu(MET's IOE, Nashik) 48


Infix to postfix conversion

Mr. Anand Gharu(MET's IOE, Nashik) 49


Resultant Postfix Expression: AB+CD-*E/
Infix to postfix conversion

Mr. Anand Gharu(MET's IOE, Nashik) 50


Resultant Postfix Expression:
Infix to postfix conversion

Mr. Anand Gharu(MET's IOE, Nashik) 51


Resultant Postfix Expression:
Infix to postfix conversion
((a/(b-c+d))*(e - a)*c)

Mr. Anand Gharu(MET's IOE, Nashik) 52


Resultant Postfix Expression: abc-d+/ea-*c*
Infix to postfix conversion
Infix Expression: A+ (B*C-(D/E^F)*G)*H, where ^ is an exponential operator.

Mr. Anand Gharu(MET's IOE, Nashik) 53


Resultant Postfix Expression: ABC*DEF^/G*-H*+
Infix to postfix conversion
Infix Expression: A+(B*(C-D)/E).

Mr. Anand Gharu(MET's IOE, Nashik) 54


Resultant Postfix Expression: ABCD-*E/+
Infix to postfix conversion
Infix Expression: A * ( B + C * D) + E.

Mr. Anand Gharu(MET's IOE, Nashik) 55


Resultant Postfix Expression: ABCD*+*E+
Infix to postfix conversion
Infix Expression: A * ( B + C * D) + E.

Mr. Anand Gharu(MET's IOE, Nashik) 56


Resultant Postfix Expression: ABCD*+*E+
Infix to postfix conversion
Infix Expression: A * ( B + C * D) + E.

Mr. Anand Gharu(MET's IOE, Nashik) 57


Resultant Postfix Expression: ABCD*+*E+
Infix to postfix conversion
Infix Expression: A * ( B + C * D) + E.

Mr. Anand Gharu(MET's IOE, Nashik) 58


Resultant Postfix Expression: ABCD*+*E+
Infix to postfix conversion

Mr. Anand Gharu(MET's IOE, Nashik) 59


Postfix into Infix conversion
Algorithm:

Iterate the given expression from left to right, one character at a time

1. If a character is operand, push it to stack.

2. If a character is an operator,

• pop operand from the stack, say it's s1.

• pop operand from the stack, say it's s2.

• perform (s2 operator s1) and push it to stack.

3. Once the expression iteration is completed, initialize the result string and
pop out from the stack and add it to the result.
Mr. Anand Gharu(MET's IOE, Nashik) 60
4. Return the result.
Postfix into Infix conversion
Example : https://youtu.be/SfhhPJeF_vE

Mr. Anand Gharu(MET's IOE, Nashik) 61


Resultant Postfix Expression: ABCD*+*E+
Postfix into Infix conversion
Infix Expression: A * ( B + C * D) + E.

Mr. Anand Gharu(MET's IOE, Nashik) 62


Resultant Postfix Expression: ABCD*+*E+
Postfix into Infix conversion
Infix Expression: A * ( B + C * D) + E.

Mr. Anand Gharu(MET's IOE, Nashik) 63


Resultant Postfix Expression: ABCD*+*E+
Postfix into prefix conversion
Infix Expression: A * ( B + C * D) + E.

Mr. Anand Gharu(MET's IOE, Nashik) 64


Resultant Postfix Expression: ABCD*+*E+
POSTFIX EXPRESSION EVALUATION
ALGORITHMS :

Mr. Anand Gharu(MET's IOE, Nashik) 65


Resultant Postfix Expression: ABCD*+*E+
POSTFIX EXPRESSION EVALUATION
POSTFIX EVALUATE EXPRESSION : 53+82-* https://youtu.be/TeM6DdvSxvQ

Mr. Anand Gharu(MET's IOE, Nashik) 66


POSTFIX EXPRESSION EVALUATION
* Evaluate the following postfix expression and show stack after every
step in tabular form. Given A=5, B=6, C=2, D=12, E=4
ABC + *DE\-

Mr. Anand Gharu(MET's IOE, Nashik) 67


POSTFIX EXPRESSION EVALUATION
• Evaluate the following postfix expression
A : 6, 2, 3, +, - , 3, 8, 2, +, +, * , 2, ^, 3, +

Mr. Anand Gharu(MET's IOE, Nashik) 68


POSTFIX EXPRESSION EVALUATION
• Evaluate the following postfix expression
532*8+*

Mr. Anand Gharu(MET's IOE, Nashik) 69


POSTFIX EXPRESSION EVALUATION
• Consider the following arithmetic expression written in postfix notation
10, 2, * , 15, 3, / , + , 12, 3, 2, ↑ , + , + evaluate this expression to find its
value

Mr. Anand Gharu(MET's IOE, Nashik) 70


POSTFIX EXPRESSION EVALUATION
• Consider the following arithmetic expression written in postfix notation
10, 2, * , 15, 3, / , + , 12, 3, 2, ↑ , + , + evaluate this expression to find its
value

Mr. Anand Gharu(MET's IOE, Nashik) 71


POSTFIX EXPRESSION EVALUATION

Mr. Anand Gharu(MET's IOE, Nashik) 72


Algorithm infix into prefix expression

Mr. Anand Gharu(MET's IOE, Nashik) 73


Convert infix into prefix expression
convert the given infix to prefix expression and show detail of stack.
(A-B/C)*(D*E-F)

Mr. Anand Gharu(MET's IOE, Nashik) 74


Convert infix into prefix expression
convert infix string ((A+B) * (C-D))/(E+F) into prefix string with stack.
--- first reverse the string (F+E)/((D-C) * (B+A))

Mr. Anand Gharu(MET's IOE, Nashik) 75


Convert infix into prefix expression
convert infix string ((A+B) * (C-D))/(E+F) into prefix string with stack.
--- first reverse the string (F+E)/((D-C) * (B+A))

Mr. Anand Gharu(MET's IOE, Nashik) 76


Convert prefix into postfix expr.
ALGORITHMS :

Mr. Anand Gharu(MET's IOE, Nashik) 77


Convert prefix into postfix expr.
Convert the following prefix expression into postfix expression
*+a-bc/-de+-fgh

Mr. Anand Gharu(MET's IOE, Nashik) 78


Convert prefix into infix expr.

Mr. Anand Gharu(MET's IOE, Nashik) 79


Convert following into postfix expr.

Mr. Anand Gharu(MET's IOE, Nashik) 80


Convert fully parenthesis into prefix expr.

Mr. Anand Gharu(MET's IOE, Nashik) 81


Convert fully parenthesis into postfix expr.

Mr. Anand Gharu(MET's IOE, Nashik) 82


Convert fully parenthesis into postfix expr.

Mr. Anand Gharu(MET's IOE, Nashik) 83


Linked stack operation
In stack elements are placed one above other. In the
same manner in stack as linked list, we place node one
above other.

Advantages of dynamic implementation of stack


:
1. No memory wastage
2. No memory shortage
3. No limitation on number of elements

Mr. Anand Gharu(MET's IOE, Nashik) 84


Stack operation using linked list
#include <iostream>
using namespace std;

struct Node {
int data;
struct Node *next;
};
struct Node* top = NULL;

void push(int val) {


struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
newnode->data = val;
newnode->next = top;
top Mr.
= newnode;
Anand Gharu(MET's IOE, Nashik) 85
Stack operation using linked list
void pop() {
if(top==NULL)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< top->data <<endl;
top = top->next;
}
}

Mr. Anand Gharu(MET's IOE, Nashik) 86


Stack operation using linked list
void display() {
struct Node* ptr;
if(top==NULL)
cout<<"stack is empty";
else {
ptr = top;
cout<<"Stack elements are: ";
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
cout<<endl;
} Mr. Anand Gharu(MET's IOE, Nashik) 87
Stack operation using linked list
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
Mr. Anand Gharu(MET's IOE, Nashik) 88
Stack operation using linked list
case 2: { default: {
pop(); cout<<"Invalid Choice"<<endl;
break; }
} }
case 3: { }while(ch!=4);
display(); return 0;
break; }
}
case 4: {
cout<<"Exit"<<endl;
break;
}

Mr. Anand Gharu(MET's IOE, Nashik) 89


RECURSION IN STACK
“Calling function inside itself is called as
recursion. Such function is called as recursive
function”
How recursion works?

Mr. Anand Gharu(MET's IOE, Nashik) 90


RECURSION IN STACK
Advantages :
1. It helps to reduce size of program
2. Easy to maintain function calling
3. Evaluation of stack can be through recursion

Disadvantages :
1. It takes more time bcz of stack overlapping
2. Stack overflow may ocuur
3. Memory requirement is more
4. Efficiency is less
Mr. Anand Gharu(MET's IOE, Nashik) 91
Backtracking algorithm strategy:
“Backtracking can be defined as a general algorithmic
technique that considers searching every possible combination
in order to solve a computational problem.”
Backtracking is a problem-solving algorithmic technique that
involves finding a solution incrementally by trying different options
and undoing them if they lead to a dead end. It is commonly used in
situations where you need to explore multiple possibilities to solve a
problem, like searching for a path in a maze or solving puzzles like
Sudoku. When a dead end is reached, the algorithm backtracks to
the previous decision point and explores a different path until a
solution is found or all possibilities have been exhausted
Mr. Anand Gharu(MET's IOE, Nashik) 92
Backtracking algorithm strategy:
How does Backtracking works?
As we know backtracking algorithm explores each and every possible path in order to find a valid
solution, this exploration of path can be easily understood via given images:

Mr. Anand Gharu(MET's IOE, Nashik) 93


Backtracking algorithm strategy:
Complexity Analysis of Backtracking

Since backtracking algorithm is purely brute force therefore in terms


of time complexity, it performs very poorly. Generally backtracking
can be seen having below mentioned time complexities:

Exponential (O(K^N))

Factorial (O(N!))

Mr. Anand Gharu(MET's IOE, Nashik) 94


Backtracking algorithm strategy:
Applications of Backtracking :

1. Creating smart bots to play Board Games such as Chess.

2. Solving mazes and puzzles such as N-Queen problem.

3. Network Routing and Congestion Control.

4. Decryption

5. Text Justification

6. Prim’s and Kruskal’s Algorithms

Mr. Anand Gharu(MET's IOE, Nashik) 95


4-QUEEN PROBLEM
The N Queen is the problem of placing N chess
queens on an N×N chessboard so that no two
queens attack each other.

Mr. Anand Gharu(MET's IOE, Nashik) 96


Recursion Vs Backtracking
Recursion Backtracking
Recursion does not always need Backtracking always uses recursion to
backtracking solve problems

Solving problems with multiple


Solving problems by breaking them
choices and exploring options
into smaller, similar subproblems and
systematically, backtracking when
solving them recursively.
needed.

Controlled by function calls and call Managed explicitly with loops and
stack. state.

Applications of Recursion: Tree and


Graph Traversal, Towers of Hanoi, Application of Backtracking: N Queen
Divide and Conquer Algorithms, problem, Prim’s Algorithms, Kruskal’s
Merge Sort, Quick Sort, and Binary Algorithms etc.
Mr. Anand Gharu(MET's IOE, Nashik) 97
Search.
References
1. https://www.geeksforgeeks.org/introduction-to-backtracking-data-structure-
and-algorithm-tutorials/
2. https://www.tutorialspoint.com/cplusplus-program-to-implement-stack-using-
array
3. https://www.helpmestudybro.com/program-to-implement-infix-to-postfix-
conversion/
4. https://www.upgrad.com/blog/recursion-in-data-structure/
5. https://aits-tpt.edu.in/wp-content/uploads/2018/08/DS-unit-2.1.pdf
6. https://www.javatpoint.com/applications-of-stack-in-data-structure
7. https://www.boardinfinity.com/blog/postfix-
expression/#:~:text=%22Stack%2Dbased%20postfix%20evaluation%22,res
ult%20back%20on%20the%20stack.
8. https://www.tutorialspoint.com/cplusplus-program-to-implement-stack-using-
linked-list
9. https://www.javatpoint.com/conversion-of-prefix-to-postfix-expression

9
8
Thanks ……!!!!
Prof. ANAND GHARU
ASSISTANT PROFESSOR
Blog : anandgharu.wordpress.com

Mr. Anand Gharu(MET's IOE, Nashik) 99

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy