0% found this document useful (0 votes)
47 views130 pages

6 Dsa

Uploaded by

teeyanshshukla
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)
47 views130 pages

6 Dsa

Uploaded by

teeyanshshukla
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/ 130

OUTLINES

• 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.

• To add (push) an item to the stack, it must be


placed on the top of the stack.

• To remove (pop) an item from the stack, it must


be removed from the top of the stack too.

• Thus, the last element that is pushed into the


stack, is the first element to be popped out of
the stack.
i.e., A stack is a Last In First Out (LIFO) data
structure

4
Stack Operations
• PUSH(Adding element on to the top of the stack)

• POP(Deleting the top most element of the stack)

• PEEP(Displaying the top most element of the


stack)

• DISPLAY(Visiting all the stack elements)

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”

Set stack [Top] =Item

STOP

7
36
25
41
23
12
PUSH Operation on stack

Top=MAX_SIZE-1 StACK OVERFLOW

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

Print the Print “Stack


popped Underflow”
element

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.

Step 1: If TOP=-1, then


Print “Stack is empty” and exit.
Step 2: Return STACK [TOP]
Step 3: Exit

12
PEEP Operation on Stack

12

36

25

23

41

13
APPLICATIONS OF STACK

1. Conversion of Infix expression to equivalent


Postfix Expression
2. Evaluation of Postfix Expression
3. Conversion of Infix expression to equivalent
Prefix Expression
4. Evaluation of Prefix Expression
5. Implementation of Recursion
6. Reversing a string
14
REVERSING A STRING
I. To reverse a string , the characters of string are
pushed onto the stack one by one as the string
is read from left to right.
• Once all the characters of string are pushed
onto stack, they are popped one by one. Since
the character last pushed in comes out first,
subsequent pop operation results in the reversal
of the string.

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

• Stacks are also used to check whether a given


arithmetic expressions containing nested
parenthesis is properly parenthesized.
• The program for checking the validity of an
expression verifies that for each left parenthesis
braces or bracket ,there is a corresponding
closing symbol and symbols are appropriately
nested.

17
For example:
VALID INPUTS INVALID INPUTS
{} {(}
({[]}) ([(()])
{[]()} {}[])
[ { ( { } [ ] ( { })}] [{)}(]}]
18
Practice Questions
1. Write a program to reverse a list of given numbers.

2. Write a program to check nesting of parentheses using a stack.

19
Infix Notation

 Infix notation is the common arithmetic and logical formula

notation, in which operators are written infix-style between

the operands they act on

Ex: A + B

20
Prefix Notation

• In Prefix notation, the operator comes before the

operand.

• The Infix expression A+B will be written as +AB in its

Prefix Notation.

• Prefix is also called ‘Polish Notation’

21
Postfix Notation
• In Postfix notation, the operator comes after the Operand.

• The Infix expression A+B will be written as AB+ in its Postfix

Notation.

• Postfix is also called ‘Reverse Polish Notation’

22
Precedence
• There are 3 levels of precedence for binary
operators as given below:

• Highest: Exponentiation (^)


• Next highest: Multiplication (*), modulus (%)
and division (/)
• Lowest: Addition (+) and subtraction (-)
Examples of infix to prefix and post fix
Infix PostFix Prefix

A+B AB+ +AB

(A+B) * (C + D) AB+CD+* *+AB+CD

A-B/(C*D^E) ABCDE^*/- -A/B*C^DE

24
Polish Notation

Infix Polish (Prefix) Polish (Postfix)


A+B +AB AB+
C-D -CD CD-
E*F *EF EF*
G/H /GH GH/
(A+B)*C (+AB)*C=*+ABC (AB+)*C=AB+C*
A+(B*C) A+(*BC)=+A*BC A+(BC*)=ABC*+
(A+B)/(C-D) (+AB)/(-CD)=/+AB-CD (AB+)/(CD-)=AB+CD-/
Infix Notation Prefix Notation Postfix Notation

a+b +ab ab+

(a + b) ∗ c ∗+abc ab+c∗

a ∗ (b + c) ∗a+bc abc+∗

a/b+c/d +/ab/cd ab/cd/+

(a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗

((a + b) ∗ c) - d -∗+abcd ab+c∗d-


CONVERSION OF INFIX TO POSTFIX
EXPRESSION
ALGORITHM: INFIX_TO_POSTFIX (I, P)
Input: I is an arithmetic expression written in infix notation.
Output: This algorithm converts the infix expression to its
equivalent postfix expression P.

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:

Given Infix Expression:

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:

Given Infix Expression:

A-B/(C*D^E)

Precedence and Associativity:

^: Highest precedence, right to left.


*, /: Medium precedence, left to right.
+, -: Lowest precedence, left to right.
.
Symbol Stack (Operators) Output (Postfix) Operations
A (empty) A Operand added to output
- - A Operator pushed onto stack
B - AB Operand added to output
Higher precedence than -,
/ -/ AB
pushed
( -/( AB Left parenthesis pushed
C -/( ABC Operand added to output
* -/(* ABC Operator pushed onto stack
D -/(* ABCD Operand added to output
Higher precedence, pushed to
^ -/(*^ ABCD
stack
E -/(*^ ABCDE Operand added to output
) -/ ABCDE^* Popped till ( and added to output
- ABCDE^*/ Popped / and added to output
(empty) ABCDE^*/- Popped - and added to output
50
Evaluation of Postfix Expression
ALGORITHM: EVALUATE_POSTFIX (P)
Input: P is an arithmetic expression in postfix notation.
Output: This algorithm evaluates the postfix expression P and
assigns the result to the variable VALUE.

Step 1: Add right parentheses ‘)’ to the end of P.


Step 2: Scan P from left to right and repeat Step 3 to Step 4 for
each element of P until the ‘)’ is encountered.
Step 3: If an operand is encountered, push it on to the STACK.

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 +"

Steps for Evaluation:

Step Symbol Stack Action


1 100 [100] Push operand 100
2 200 [100, 200] Push operand 200
Pop 100 and 200, perform
3 + [300]
100 + 200, push result 300
4 2 [300, 2] Push operand 2
Pop 300 and 2, perform
5 / [150]
300 / 2, push result 150
6 5 [150, 5] Push operand 5
Pop 150 and 5, perform
7 * [750]
150 * 5, push result 750
8 7 [750, 7] Push operand 7
Pop 750 and 7, perform
9 + [757]
750 + 7, push result 757

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.

Step 1: Add ‘(’ to the beginning of the infix expression I and


push ‘)‘on to the STACK.
Step 2: Scan I from right to left and repeat Step 4 to Step 6 for
each element of I until the STACK is empty.
Step 3: If an operand is encountered, PUSH it on to the Output
Stack(Stack2).
Step 4: If a right parentheses ‘)‘is encountered, push it on to the
STACK.

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:

Given Infix Expression:

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:

Given Infix Expression:

A-B/(C*D^E)

Precedence and Associativity:

^: Highest precedence, right to left.


*, /: Medium precedence, left to right.
+, -: Lowest precedence, left to right.
Symbol Stack (Operators) Output Stack Operations

( ( Left parenthesis pushed

E ( E Operand added to output

^ (^ E Operator pushed onto stack

D (^ ED Operand added to output

) ED^ Popped till ( and added to output

* * ED^ Operator pushed onto stack

C * ED^C Operand added to output

/ / ED^C* Higher precedence, popped * and pushed /

B / ED^C*B Operand added to output

- - ED^C*B/ Pushed '-' after popping '/'

A - ED^C*B/A Operand added to output

(empty) ED^C*B/A- Popped - and added to output

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.

Step 1: Add a left parentheses ‘(’ at the beginning of P.


Step 2: Scan P from right to left and repeat Step 3 to Step 4
for each element of P until the ‘(’ is encountered.
Step 3: If an operand is encountered, push it on to the STACK.

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

Steps for Evaluation:

Step Symbol Stack Action


1 0 [0] Push operand 0

2 2 [0, 2] Push operand 2

Pop 0 and 2, perform 2 + 0, push


3 + [2]
result 2

4 5 [2, 5] Push operand 5

5 4 [2, 5, 4] Push operand 4

Pop 4 and 5, perform 4 * 5, push


6 * [2, 20]
result 20

7 7 [2, 20, 7] Push operand 7

Pop 7 and 20, perform 7 + 20,


8 + [2, 27]
push result 27

Pop 27 and 2, perform 27 - 2, push


9 - [-25]
result 25

106
Prefix Expression: - + 8 / 6 3 2

Steps for Evaluation:

Step Symbol Stack Action


1 2 [2] Push operand 2

2 3 [2, 3] Push operand 3

3 6 [2, 3, 6] Push operand 6

Pop 6 and 3, perform 6 / 3,


4 / [2, 2]
push result 2

5 8 [2, 2, 8] Push operand 8

Pop 8 and 2, perform 8 + 2,


6 + [2, 10]
push result 10

Pop 10 and 2, perform 10 - 2,


7 - [-8]
push result 8

107
Linked list implementation of stack

• In linked list implementation of stack, the nodes are maintained


non-contiguously in the memory.
• Each node contains a pointer to its immediate successor node in
the stack.
• Stack is said to be overflown if the space left in the memory
heap is not enough to create a node.
• Linked list allocates the memory dynamically.
• However, time complexity in both the scenario (array and Linked
List) is same for all the operations i.e. push, pop and peek.
Implementation of Stack using LL

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>

// Define the structure of a node


struct Node {
int data;
struct Node* next; // Pointer to the next node
};

// Define the top of the stack as a global pointer


struct Node* top = NULL;

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);

printf("Top element is %d\n", peek());

pop();
pop();

printf("Top element after popping is %d\n", peek());

return 0;
}

117
Questions

1. The term "push" and "pop" is related to the

a. Array
b. Lists
c. Stack
d. All of above

c. Stack

118
Questions

2. Which of the following name does not relate to stacks?

a. FIFO lists
b. LIFO list
c. Piles
d. Push-down lists

a. FIFO Lists

119
Questions

3.Which of the following can be an application of stack?

a) Finding factorial
b) Tower of Hanoi
c) Infix to Postfix Conversion
d) All of the above

d)All of the above

120
Questions

4.The data structure which is one ended is ..................

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

7.User perform the following operations on stack size 5,Then


Push(1), pop(), push(2), push(3), pop(),
push(4), pop(), pop(), push(5)

At the end of last operation, total number of elements present in the


stack is----
a)3
b)1
c)2
d)4

b) 1

124
Questions

8.For the following operations on stack of size 5 then

Push(1), pop(), push(2), push(3), pop(),


push(4), pop(), pop(), push(5), pop(),
pop(), push(6)

Which of the following statement is correct for stack

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

10.Expressions in which operator is written after the operand is called----


---

a. Infix Expression
b. Prefix Expression
c. Postfix Expression

c. Postfix Expression

127
Questions

11.Stack can not be used to ------

a. Implementation of Recursion
b. Evaluation of postfix expression
c. Allocating resources and scheduling
d. Reversing String

c. Allocating resources and scheduling

128
Questions

12.Well formed parentheses can be implemented using

a. list
b. queue
c. hash map
d. stack

d. stack

129
Questions

13.Find the equivalent prefix expression from the following infix


expression a+b-c/d*(e-f)/g

-+ab/*/cd-efg

130

You might also like