Ads Assignment No. 3
Ads Assignment No. 3
Q.9.) Draw the stack status for the flowing given operations:
i. Push(a)
ii. Push(b)
iii. Push(c)
iv. Push(d)
v. Pop()
vi. Pop()
vii Pop()
Ans :- Here is the stack status after each operation in sequence:
Stack Operations and Status:
1. Push(a)
o Stack
2. Push(b)
• Stack
3. Push(c)
• Stack
4. Push(d)
• Stack
5. Pop()
• Removes d (top element).
• Stack
6. Pop()
• Removes c (top element).
• Stack
7. Pop()
• Removes b (top element).
• Stack
Q.10.) Write the algorithm to insert and delete an element in a simple queue.
Ans :- Here’s an algorithm for inserting and deleting elements in a simple queue
(FIFO - First-In, First-Out). A simple queue allows insertion at the rear and deletion
from the front.
Queue Insertion (Enqueue) Algorithm
1. Check for Queue Overflow:
o If the rear pointer is at the maximum capacity (MAX - 1), the queue is
full. Print an error message: "Queue Overflow".
2. Insert the Element:
o If the queue is empty (when front is -1), set front to 0.
o Increment the rear pointer by 1.
o Insert the element at the position indicated by rear.
3. End.
Queue Deletion (Dequeue) Algorithm
1. Check for Queue Underflow:
o If front is -1 (or front > rear), the queue is empty. Print an error
message: "Queue Underflow".
2. Delete the Element:
o Retrieve the element at the position indicated by front.
o If front is equal to rear (i.e., there’s only one element left), set both
front and rear to -1 to reset the queue.
o Otherwise, increment the front pointer by 1 to move to the next
element in the queue.
3. End.
Example Code for Reference (in Python)
Explanation of the Code:
• enqueue(element): Checks for overflow, then adds an element to the rear.
• dequeue(): Checks for underflow, then removes an element from the front.