Dsu Ex 16
Dsu Ex 16
Experiment No: 16
Title of Write a 'C' Program to perform PUSH and POP Operations
Experiment on
Stack using an Array.
ALGORITM:
push(stack[MAX],element)
Step 1: Start
Step 2: If top = MAX-1 goto Step 3 else goto Step 4
Step 3: Display message ”Stack Full” and exit
Step 4: top = top + 1
Step 5: stack[top] = element
Step 6: Stop
pop(stack[MAX],element)
Step 1: Start
Step 2: If top = -1 goto Step 3 else goto Step 4
Step 3: Display message ”Stack Empty” and exit
Step 4: Return stack[top] and set top = top - 1
Step 5: Stop
display
Step 1: Check if the stack is empty (top == -1).
Step 2: If empty, print "Empty".
Step 3: If not empty, print all elements from stack[top] down to stack[0]
1 | Page
2 | Page
3 | Page
OUTPUT:
4 | Page
Practical Related Questions
1. Write a C program to perform following operations on Empty stack:
a. PUSH (10), PUSH (20), POP, PUSH (10), PUSH (20), POP, POP, POP,
PUSH (20),
POP.
5 | Page
6 | Page
2. Write a C program to reverse a String using Stack.
7 | Page
8 | Page
3. Write a C program to check whether the given string is palindrome
or not using stack.
9 | Page
10 | P a g e
11 | P a g e
Exercise:-
2. Consider the following stack and observe how many operations are required to delete
smallest element from given stack.
2
4
1
7
5
9
Ans :
Approach:
1. We will pop each element from the stack until we find the smallest element (1).
2. Once the smallest element is found, it is removed.
3. Optionally, if you need to preserve the stack's order (excluding the smallest element),
we could use a temporary stack to store the popped elements and then push them back
after removing the smallest element.
Steps:
1. Pop 2 (store it in a temporary stack, if preserving order)
2. Pop 4 (store it in a temporary stack, if preserving order)
3. Pop 1 (this is the smallest element, so we discard it)
4. Push 4 back (only if you're preserving the original stack)
5. Push 2 back (only if you're preserving the original stack)
Operation Count:
Pop operations to reach the smallest element: 3 pop operations.
(Optional) Push operations to restore the stack: 2 push operations (to restore the
stack).
Total Operations:
Without restoring stack: 3 operations (only pops).
With restoring stack: 3 pops + 2 pushes = 5 operations.
Thus, a minimum of 3 operations is required to delete the smallest element, and 5 operations
if you want to restore the stack (without the smallest element).
12 | P a g e
Grade and Process Product Dated Sign
Dated Related (35) Related (15)
Signature of
Teacher
13 | P a g e