6 Dsa
6 Dsa
• Introduction
• Operations on Stack
– PUSH Operation
– POP Operation
– PEEP Operation
• Applications of Stack
– Infix to Postfix Expression Conversion
– Evaluation of Postfix Expression
– Infix to Prefix Expression
– Evaluation of Prefix Expression
• Questions & Answer Discussion
2
Introduction to Stacks
Examples:
Stack of books
Stack of CDs
3
What is a Stack?
• A stack is a data structure in which data is added
and removed at only one end called the top.
4
Stack Operations
• PUSH(Adding element on to the top of the stack)
5
PUSH Operation
ALGORITHM: PUSH (STACK, TOP, MAX_SIZE, ITEM)
Input: STACK is the linear array with maximum size
MAX_SIZE.
Output: This algorithm pushes (or inserts) an element
ITEM on to the STACK.
Step 1: If TOP=MAX_SIZE-1, then
Print “Stack Overflow” and exit.
Step 2: Set TOP=TOP+1
Step 3: Set STACK [TOP] =ITEM
Step 4: Exit
6
START
PUSH Operation
Flow Chart
Is
top=MAX-
1
?
Enter an element to be
inserted
Set top=top+1
Print “Stack
Overflow”
STOP
7
36
25
41
23
12
PUSH Operation on stack
INSERT 12
3
INSERT 36
2
INSERT 25
Max_size=4 1
INSERT 23
0 INSERT 41
-1
8
POP Operation
ALGORITHM: POP (STACK, TOP, MAX_SIZE, ITEM)
Input: STACK is the linear array with maximum size MAX_SIZE.
Output: This algorithm deletes the top element of the STACK
and assigns it to the variable ITEM.
Step 1: If TOP=-1, then
Print “Stack Underflow” and exit.
Step 2: Set ITEM=STACK [TOP]
Step 3: Set TOP=TOP-1
Step 4: Exit
9
START
POP Operation Flow
Chart
Is
Top= -1?
Set item=stack
[Top]
Set top=top-1
STOP
10
POP Operation On Stack
36 3 ( POP )
25 2 ( POP )
23 1 ( POP )
41 0 ( POP )
-1 ( POP )
StACK UNDERFLOW 11
PEEP Operation
ALGORITHM: PEEP (STACK, TOP, MAX_SIZE, ITEM)
Input: STACK is the linear array with maximum size MAX_SIZE.
Output: This algorithm returns the top most element of the
STACK.
12
PEEP Operation on Stack
12
36
25
23
41
13
APPLICATIONS OF STACK
15
For example:
To reverse the string ‘REVERSE’ the string is read from left
to right and its characters are pushed . LIKE: onto a stack.
16
CHECKING OF PARENTHESES
17
For example:
VALID INPUTS INVALID INPUTS
{} {(}
({[]}) ([(()])
{[]()} {}[])
[ { ( { } [ ] ( { })}] [{)}(]}]
18
Practice Questions
1. Write a program to reverse a list of given numbers.
19
Infix Notation
Ex: A + B
20
Prefix Notation
operand.
Prefix Notation.
21
Postfix Notation
• In Postfix notation, the operator comes after the Operand.
Notation.
22
Precedence
• There are 3 levels of precedence for binary
operators as given below:
24
Polish Notation
(a + b) ∗ c ∗+abc ab+c∗
a ∗ (b + c) ∗a+bc abc+∗
(a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗
Step 1: Add ‘)’ to the end of the infix expression I and push
‘(‘on to the STACK.
Step 2: Scan I from left to right and repeat Step 3 to Step 6 for
each element of I until the STACK is empty.
Step 3: If an operand is encountered, add it to P.
Step 4: If a left parentheses ‘(‘is encountered, push it on to the
STACK.
27
Contd…
Step 5: If an operator OP is encountered, then
(a)Repeatedly pop from the STACK and add each operator
(on the top of the STACK) to the Postfix expression ‘P’
which has same precedence as or higher precedence than
OP.
(b)Push the operator OP on to the STACK.
[End of if]
Step 6: If a right parentheses ‘)’ is encountered, then
(a) Repeatedly pop from the STACK and add each operator
(on the top of the STACK) to the Postfix expression ‘P’
until a left parentheses ‘(‘ is encountered.
(b) Remove the left parentheses ‘(‘. [Don’t add it to the
postfix expression P.]
[End of if]
[End of Step 2 loop]
Step 7: Exit
28
Infix to Postfix Conversion
Infix Expression
(a+b-c)*d–(e+f)
Postfix Expression
29
Infix to Postfix Conversion
Infix Expression
(a+b-c)*d–(e+f) )
Postfix Expression
30
Infix to Postfix Conversion
Infix Expression
a+b-c)*d–(e+f) )
Postfix Expression
(
(
31
Infix to Postfix Conversion
Infix Expression
+b-c)*d–(e+f))
Postfix Expression
a
(
(
32
Infix to Postfix Conversion
Infix Expression
b-c)*d–(e+f))
Postfix Expression
+ a
(
(
33
Infix to Postfix Conversion
Infix Expression
-c)*d–(e+f))
Postfix Expression
ab
+
(
(
34
Infix to Postfix Conversion
Infix Expression
c)*d–(e+f))
Postfix Expression
ab+
-
(
(
35
Infix to Postfix Conversion
Infix Expression
)*d–(e+f))
Postfix Expression
ab+c
-
(
(
36
Infix to Postfix Conversion
Infix Expression
*d–(e+f))
Postfix Expression
ab+c-
37
Infix to Postfix Conversion
Infix Expression
d–(e+f))
Postfix Expression
ab+c-
*
(
38
Infix to Postfix Conversion
Infix Expression
–(e+f))
Postfix Expression
ab+c–d
*
(
Stack 39
Infix to Postfix Conversion
Infix Expression
(e+f))
Postfix Expression
ab+c–d*
-
(
Stack 40
Infix to Postfix Conversion
Infix Expression
e+f))
Postfix Expression
( ab+c–d*
-
(
Stack 41
Infix to Postfix Conversion
Infix Expression
+f))
Postfix Expression
ab+c–d*e
(
-
(
Stack 42
Infix to Postfix Conversion
Infix Expression
f))
Postfix Expression
+
ab+c–d*e
(
-
(
Stack 43
Infix to Postfix Conversion
Infix Expression
))
Postfix Expression
+
( ab+c–d*ef
-
(
Stack 44
Infix to Postfix Conversion
Infix Expression
)
Postfix Expression
ab+c–d*ef+
-
(
Stack 45
Infix to Postfix Conversion
Infix Expression
Postfix Expression
ab+c–d*ef+-
Stack 46
Infix to Postfix Conversion
Practice Question:
a+b*c-(d/e+f*g*h)
Infix Character Scanned Stack Postfix Expression
a [] a
+ [+] a
b [+] ab
* [+, *] ab
c [+, *] abc
- [-] abc*+
( [-, (] abc*+
d [-, (] abc*+d
/ [-, (, /] abc*+d
e [-, (, /] abc*+de
+ [-, (, +] abc*+de/
f [-, (, +] abc*+de/f
* [-, (, +, *] abc*+de/f
g [-, (, +, *] abc*+de/fg
* [-, (, +, *] a b c * + d e / f g*
h [-, (, +, *] a b c * + d e / f g*h
) [-] a b c * + d e / f g *h* +
End of Expression [] a b c * + d e / f g*h* + -
Infix to Postfix Conversion
Practice Question:
A-B/(C*D^E)
51
Contd…
Step 4: If an operator OP is encountered, then
(a)Pop the two top elements of the STACK,
where A is the top element and B is the next
to top element.
(b)Evaluate B OP A.
(c)Push the result of the evaluation on to the
STACK.
[End of If]
[End of Step 2 loop]
Step 5: Set VALUE equal to the top most element of the STACK.
Step 6: EXIT
52
Evaluation of Postfix Expression
Postfix Expression
5 6 2 + * 12 4 / -
Result
Stack 53
Evaluation of Postfix Expression
Postfix Expression
5 6 2 + * 12 4 / - )
Result
Stack 54
Evaluation of Postfix Expression
Postfix Expression
6 2 + * 12 4 / - )
Result
5
Stack 55
Evaluation of Postfix Expression
Postfix Expression
2 + * 12 4 / - )
Result
6
5
Stack 56
Evaluation of Postfix Expression
Postfix Expression
+ * 12 4 / - )
Result
2
6
5
Stack 57
Evaluation of Postfix Expression
Postfix Expression
* 12 4 / - )
Result
8
5
Stack 58
Evaluation of Postfix Expression
Postfix Expression
12 4 / - )
Result
40
Stack 59
Evaluation of Postfix Expression
Postfix Expression
4 / - )
Result
12
40
Stack 60
Evaluation of Postfix Expression
Postfix Expression
/ - )
Result
4
12
40
Stack 61
Evaluation of Postfix Expression
Postfix Expression
- )
Result
3
40
Stack 62
Evaluation of Postfix Expression
Postfix Expression
)
Result
37
Stack 63
Evaluation of Postfix Expression
Postfix Expression
Result
37
Stack 64
Postfix Expression: "100 200 + 2 / 5 * 7 +"
65
CONVERSION OF INFIX TO PREFIX
EXPRESSION
ALGORITHM: INFIX_TO_PREFIX (I, P)
Input: I is an arithmetic expression written in infix notation.
Output: This algorithm converts the infix expression to its
equivalent prefix expression P.
66
Contd…
Step 5: If an operator OP is encountered, then
(a)Repeatedly pop from the STACK and add each operator
(on the top of the STACK) to the Output Stack(Stack2)
which has higher precedence than OP.
(b)Push the operator OP on to the STACK.
[End of if]
Step 6: If a left parentheses ‘(’ is encountered, then
(a) Repeatedly pop from the STACK and add each operator
(on the top of the STACK) to the Output Stack(Stack2)
until a right parentheses ‘)‘ is encountered.
(b) Remove the right parentheses ‘)‘. [Don’t add it to the
Output Stack(Stack2).]
[End of if]
[End of Step 2 loop]
Step 7: Pop the elements from the Output Stack(Stack2).
Step 8: Exit
67
Infix to Prefix Conversion
Infix Expression
(a+b-c)*d–(e+f)
Output Stack(Stack2)
Stack 68
Infix to Prefix Conversion
Infix Expression
((a+b-c)*d–(e+f)
Output Stack(Stack2)
)
Stack 69
Infix to Prefix Conversion
Infix Expression
((a+b-c)*d–(e+f
Output Stack(Stack2)
)
)
Stack 70
Infix to Prefix Conversion
Infix Expression
((a+b-c)*d–(e+
Output Stack(Stack2)
f
)
)
Stack 71
Infix to Prefix Conversion
Infix Expression
((a+b-c)*d–(e
Output Stack(Stack2)
+ f
)
)
Stack 72
Infix to Prefix Conversion
Infix Expression
( ( a + b - c ) * d –(
Output Stack(Stack2)
fe
+
)
)
Stack 73
Infix to Prefix Conversion
Infix Expression
((a+b-c)*d–
Output Stack(Stack2)
fe+
)
Stack 74
Infix to Prefix Conversion
Infix Expression
((a+b-c)*d
Output Stack(Stack2)
fe+
-
)
Stack 75
Infix to Prefix Conversion
Infix Expression
((a+b-c)*
Output Stack(Stack2)
fe+d
-
)
Stack 76
Infix to Prefix Conversion
Infix Expression
((a+b-c)
Output Stack(Stack2)
fe+d
*
-
)
Stack 77
Infix to Prefix Conversion
Infix Expression
((a+b-c
Output Stack(Stack2)
)
fe+d
*
-
)
Stack 78
Infix to Prefix Conversion
Infix Expression
((a+b-
Output Stack(Stack2)
)
fe+dc
*
-
)
Stack 79
Infix to Prefix Conversion
Infix Expression
((a+b
-
Output Stack(Stack2)
)
fe+dc
*
-
)
Stack 80
Infix to Prefix Conversion
Infix Expression
((a+
-
Output Stack(Stack2)
)
fe+dcb
*
-
)
Stack 81
Infix to Prefix Conversion
Infix Expression
+ ((a
-
Output Stack(Stack2)
)
fe+dcb
*
-
)
Stack 82
Infix to Prefix Conversion
Infix Expression
+ ((
-
Output Stack(Stack2)
)
fe+dcba
*
-
)
Stack 83
Infix to Prefix Conversion
Infix Expression
(
Output Stack(Stack2)
fe+dcba+-
*
-
)
Stack 84
Infix to Prefix Conversion
Infix Expression
Output Stack(Stack2)
fe+dcba+-*-
Stack 85
Infix to Prefix Conversion
Infix Expression
Output Stack(Stack2)
fe+dcba+-*-
Prefix Expression
-*-+abcd+ef
Stack 86
Infix to Prefix Conversion
Practice Question:
a+b*c-(d/e+f*g*h)
Character Scanned Stack Output Stack
) )
h ) h
* )* h
g )* hg
* )* hg*
f )* hg*f
+ )+ hg*f*
e )+ hg*f*e
/ )+ / hg*f*e
d )+ / hg*f*ed
( hg*f*ed/+
- - hg*f*ed/+
c - hg*f*ed/+c
* -* hg*f*ed/+c
b -* hg*f*ed/+cb
+ + hg*f*ed/+cb*-
a + hg*f*ed/+cb*-a
End of expression hg*f*ed/+cb*-a+
88
Answer: + a - * b c + / d e * f * g h
Character Scanned Stack Output Stack
)
h ) h
* )* h
g )* hg
* )** hg
f )** hgf
+ )+ hgf**
e )+ hgf**e
/ )+/ hgf**e
d )+/ hgf**ed
( hgf**ed/+
- - hgf**ed/+
c - hgf**ed/+c
* -* hgf**ed/+c
b -* hgf**ed/+cb
+ -+ hgf**ed/+cb*
a -+ hgf**ed/+cb*a
End of Expression hgf**ed/+cb*a+-
89
Infix to Prefix Conversion
Practice Question:
A-B/(C*D^E)
Answer: - A / B * C ^ D E
Evaluation of Prefix Expression
ALGORITHM: EVALUATE_PREFIX (P)
Input: P is an arithmetic expression in prefix notation.
Output: This algorithm evaluates the prefix expression P and
assigns the result to the variable VALUE.
92
Evaluation of Prefix Expression
Step 4: If an operator OP is encountered, then
(a)Pop the two top elements of the STACK,
where A is the top element and B is the next
to top element.
(b)Evaluate A OP B.
(c)Push the result of the evaluation(b) on to the
STACK.
[End of If]
[End of Step 2 loop]
Step 5: Set VALUE equal to the top most element of the STACK.
Step 6: EXIT
93
Evaluation of Prefix Expression
Prefix Expression
- * 5 + 6 2 / 12 4
Result
Stack 94
Evaluation of Prefix Expression
Prefix Expression
( - * 5 + 6 2 / 12 4
Result
Stack 95
Evaluation of Prefix Expression
Prefix Expression
( - * 5 + 6 2 / 12
Result
Stack 96
Evaluation of Prefix Expression
Prefix Expression
(-*5+62/
Result
12
Stack 97
Evaluation of Prefix Expression
Prefix Expression
(-*5+62
Result
Stack 98
Evaluation of Prefix Expression
Prefix Expression
(-*5+6
Result
Stack 99
Evaluation of Prefix Expression
Prefix Expression
(-*5+
Result
Stack 100
Evaluation of Prefix Expression
Prefix Expression
(-*5
Result
Stack 101
Evaluation of Prefix Expression
Prefix Expression
(-*
Result
Stack 102
Evaluation of Prefix Expression
Prefix Expression
(-
Result
40
Stack 103
Evaluation of Prefix Expression
Prefix Expression
(
Result
37
Stack 104
Evaluation of Prefix Expression
Prefix Expression
Result
37
Stack 105
Prefix Expression: - + 7 * 4 5 + 2 0
106
Prefix Expression: - + 8 / 6 3 2
107
Linked list implementation of stack
109
Implementation of Stack using LL
110
Implementation of Stack using LL
111
Perform Operations
• Linked is having following items
Top
10 12 1
• Push 100
• Push 50
• Pop
• Push 45
• Pop
• Pop
• Display resulting stack
Implementation
#include <stdio.h>
#include <stdlib.h>
113
Implementation
// Push function to add an element to the stack
void push(int value) {
// Create a new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Heap Overflow\n");
return;
}
newNode->data = value; // Set the value
newNode->next = top; // Link the new node to the previous top node
top = newNode; // Make the new node the top node
printf("Pushed %d onto the stack\n", value);
}
114
Implementation
// Pop function to remove an element from the stack
int pop() {
if (top == NULL) {
printf("Stack Underflow\n");
return -1; // Return -1 if the stack is empty
}
int poppedValue = top->data; // Get the data of the top node
struct Node* temp = top; // Temporarily store the top node
top = top->next; // Move the top to the next node
free(temp); // Free the memory of the old top node
printf("Popped %d from the stack\n", poppedValue);
return poppedValue; // Return the popped value
}
115
Implementation
// Peek function to return the top element of the
stack
int peek() {
if (top == NULL) {
printf("Stack is empty\n");
return -1; // Return -1 if the stack is empty
}
return top->data; // Return the data of the top
node
}
116
Implementation
// Main function to demonstrate the stack operations
int main() {
push(10);
push(20);
push(30);
pop();
pop();
return 0;
}
117
Questions
a. Array
b. Lists
c. Stack
d. All of above
c. Stack
118
Questions
a. FIFO lists
b. LIFO list
c. Piles
d. Push-down lists
a. FIFO Lists
119
Questions
a) Finding factorial
b) Tower of Hanoi
c) Infix to Postfix Conversion
d) All of the above
120
Questions
a) Queue
b) Stack
c) Tree
d) Graph
b) Stack
121
Questions
5.In the stack, if user try to remove element from the empty stack then it
is called -----
a)Garbage Collection
b)Overflow Stack
c)Underflow Stack
d)Empty Collection
c) Underflow Stack
122
Questions
6. User push 1 element in the stack having already five elements and
having stack size as 5 then stack becomes-----
a)User flow
b)Underflow
c)Crash
d)Overflow
d)Overflow
123
Questions
b) 1
124
Questions
a)Underflow Occurs
b)Overflow Occurs
c)Stack operation will be performed smoothly
d)None of these
a)Underflow Occurs
125
Questions
9.What will be the postfix expression for the following infix expression
b*c+d/e
a. b*cde/+
b. bc*de/+
c. bcd*e/+
d. bc*de+/
b. bc*de/+
126
Questions
a. Infix Expression
b. Prefix Expression
c. Postfix Expression
c. Postfix Expression
127
Questions
a. Implementation of Recursion
b. Evaluation of postfix expression
c. Allocating resources and scheduling
d. Reversing String
128
Questions
a. list
b. queue
c. hash map
d. stack
d. stack
129
Questions
-+ab/*/cd-efg
130