Queue · Data Structures
14 questions · asked in 13 of 27 years (2000–2026)
MCQ 9 · MSQ 1 · NAT 41-mark 7 · 2-mark 7
Questions 11–14 of 14
Answer: (A)
- Empty: REAR == FRONT
- Full: the next position of REAR would reach FRONT, so (REAR + 1) mod n == FRONT
Answer: full: (REAR+1) mod n == FRONT, empty: REAR == FRONT
i. isEmpty(Q) returns true if the queue is empty, false otherwise.
ii. delete(Q) deletes the element at the front of the queue and returns its value.
iii. insert(Q,i) inserts the integer i at the rear of the queue.
Consider the following function
void f (queue Q) {
int i ;
if (!isEmpty(Q)) {
i = delete(Q);
f(Q);
insert(Q, i);
}
} What operation is performed by the above function f ?
Answer: (B)
Now the calls return, innermost first. The call holding inserts 5 first, then the one holding inserts 4, and so on, and the outermost call inserts 1 last. The queue is now from front to rear, which is the reverse of the original order.
Answer: (B)
void insert(Q,x){
push (S1,x);
}
void delete(Q){
if (stack-empty (S2))then
if (stack-empty (S1))then{
print("Q is empty");
return;
}
else while (! (stack-empty)(S1)){
x=pop(S1);
push(S2,x);
}
x=pop(S2);
} Let n insert and m(n) delete operations be performed in an arbitrary on an empty queue Q, Let x and y be the number of push and pop operations performed respectively in the processes. Which one of the following is true for all m and n ?Answer: (A)
Each insert pushes once onto S1, giving n pushes. When S2 is empty, a delete moves all of S1 into S2: each moved element costs one pop from S1 and one push onto S2. Then the delete pops one element from S2. Let d be the number of elements moved to S2 over the whole run. Then
and (d pops from S1, and m pops from S2, one per delete).
Bounds on d: every deleted element must pass through S2, so . No more than n elements are ever inserted, so . So , which gives
and .
Both ends are reached. If inserts and deletes alternate, each delete moves just one element, so . If all n inserts come first, the first delete moves all n, so .
Option A gives exactly these bounds. The other options use looser limits, for example follows from because , and follows from .
Answer: (A)
Answer: (B)
Two stacks are enough to build a queue. One stack is not enough, because a stack is last-in first-out: to remove the oldest element (at the bottom) you must take out everything above it, and one stack has nowhere to hold those elements while you do it. A second stack gives that place.
Let the queue be and let the two stacks be "stack1" and "stack2". The queue never holds more than elements, so neither stack needs more than places. There are two ways to build :
Method 1 (enQueue is costly)
The newly added element always goes to the bottom of stack1, so deQueue only pops from stack1. Stack2 is used to place the new element at the bottom of stack1.
- While stack1 is not empty, move every element from stack1 to stack2.
- Push onto stack1.
- Move everything back from stack2 to stack1.
- If stack1 is empty, report an error.
- Pop one item from stack1 and return it.
Method 2 (deQueue is costly)
In enQueue, the new element goes on top of stack1. In deQueue, if stack2 is empty, all elements are moved to stack2, which reverses their order, and then the top of stack2 is returned.
- Push onto stack1.
- If both stacks are empty, report an error.
- If stack2 is empty, while stack1 is not empty, move every element from stack1 to stack2.
- Pop the element from stack2 and return it.
Answer: (B)
Keep practising