Unit 5 Fds 2023
Unit 5 Fds 2023
Unit 5 Fds 2023
”
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.
STACK
PUSH POP
Add data to Take data from
element in element in
stack. stack
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
<, £ , =, ¹, ³, >
AND
OR
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
infixVect
+b-c)*d–(e+f)
postfixVect
a
infixVect
b-c)*d–(e+f)
postfixVect
a
+
(
infixVect
-c)*d–(e+f)
postfixVect
ab
+
(
infixVect
c)*d–(e+f)
postfixVect
ab+
-
(
infixVect
)*d–(e+f)
postfixVect
ab+c
-
(
infixVect
*d–(e+f)
postfixVect
ab+c-
infixVect
d–(e+f)
postfixVect
ab+c-
infixVect
–(e+f)
postfixVect
ab+c-d
infixVect
(e+f)
postfixVect
ab+c–d*
infixVect
e+f)
postfixVect
ab+c–d*
(
-
infixVect
+f)
postfixVect
ab+c–d*e
(
-
infixVect
f)
postfixVect
+ ab+c–d*e
(
-
infixVect
)
postfixVect
+ ab+c–d*ef
(
-
infixVect
postfixVect
ab+c–d*ef+
infixVect
postfixVect
ab+c–d*ef+-
Iterate the given expression from left to right, one character at a time
2. If a character is an operator,
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
struct Node {
int data;
struct Node *next;
};
struct Node* top = NULL;
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:
Exponential (O(K^N))
Factorial (O(N!))
4. Decryption
5. Text Justification
Controlled by function calls and call Managed explicitly with loops and
stack. state.
9
8
Thanks ……!!!!
Prof. ANAND GHARU
ASSISTANT PROFESSOR
Blog : anandgharu.wordpress.com