An implementation of a queue Q, using two stacks S1 and S2, is given below:
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 (\(\le n\)) delete operations be performed in an arbitrary order on an empty queue Q, each delete being made when Q is not empty. Let x and y be the number of push and pop operations performed respectively in the process. Each option below gives bounds on x and y. Which one gives the tightest bounds that are true for all m and n?
- A\(m+n \leq x\leq 2n\) and \(2m \leq y \leq n+m\)
- B\(m+n \leq x\leq 2n\) and \(2m \leq y \leq 2n\)
- C\(2m\leq x\leq 2n\) and \(2m \leq y \leq n+m\)
- D\(2m \leq x\leq 2n\) and \(2m \leq y \leq 2n\)