Stacks and Queues · Data Structures · GATE CSE
Marks 1
Consider the operator precedence and associativity rules for the integer arithmetic operators given in the table below.
Operator | Precedence | Associativity |
---|---|---|
+ | Highest | Left |
− | High | Right |
* | Medium | Right |
/ | Low | Right |
The value of the expression $3 + 1 + 5 * 2 / 7 + 2 − 4 − 7 − 6 / 2$ as per the above rules is _______
Consider the following sequence of operations on an empty stack.
push(54); push(52); pop(); push(55); push(62); s = pop();
Consider the following sequence of operations on an empty queue.
enqueue(21); enqueue(24); dequeue(); enqueue(28); enqueue(32); q = dequeue();
The value of s + q is ______
(i) First-in-first out types of computations are efficiently supported by STACKS.
(ii) Implementing LISTS on linked lists is more efficient than implementing LISTS on an array for almost all the basic LIST operations.
(iii) Implementing QUEUES on a circular array is more efficient than implementing QUEUES on a linear array with two indices.
(iv) Last-in-first-out type of computations are efficiently supported by QUEUES.
Marks 2
Consider the following expression: $x[i] = (p + r) * -s[i] + \frac{u}{w}$. The following sequence shows the list of triples representing the given expression, with entries missing for triples (1), (3), and (6).
(0) | + | p | r |
(1) | |||
(2) | uminus | (1) | |
(3) | |||
(4) | / | u | w |
(5) | + | (3) | (4) |
(6) | |||
(7) | = | (6) | (5) |
Which one of the following options fills in the missing entries CORRECTLY?
Let S1 and S2 be two stacks. S1 has capacity of 4 elements. S2 has capacity of 2 elements. S1 already has 4 elements: 100, 200, 300, and 400, whereas S2 is empty, as shown below.
Stack S1 |
---|
400 (Top) |
300 |
200 |
100 |
Stack S2 |
---|
Only the following three operations are available:
PushToS2: Pop the top element from S1 and push it on S2.
PushToS1: Pop the top element from S2 and push it on S1.
GenerateOutput: Pop the top element from S1 and output it to the user.
Note that the pop operation is not allowed on an empty stack and the push operation is not allowed on a full stack.
Which of the following output sequences can be generated by using the above operations?
Consider a sequence a of elements $$a_0=1,a_1=5,a_2=7,a_3=8,a_4=9$$, and $$a_5=2$$. The following operations are performed on a stack S and a queue Q, both of which are initially empty.
I: push the elements of a from a$$_0$$ to a$$_5$$ in that order into S.
II: enqueue the elements of a from a$$_0$$ to a$$_5$$ in that order into Q.
III: pop an element from S.
IV: dequeue an element from Q.
V: pop an element from S.
VI: dequeue an element from Q.
VII: dequeue an element from Q and push the same
VIII: Repeat operation VII three times.
IX: pop an element from S.
X: pop an element from S.
The top element of S after executing the above operations is ____________.
Consider the queues Q1 containing four elements and Q2 containing none (shown as the Initial State in the figure). The only operations allowed on these two queues are Enqueue (Q, element) and Dequeue (Q). The minimum number of Enqueue operations on Q1 required to place the elements of Q1 in Q2 in reverse order (shown as the Final State in the figure) without using any additional storage is ______________.
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 ? #include
#define EOF -1
void push (int); /* push the argument on the stack */
int pop (void); /* pop the top of the stack */
void flagError ();
int main () {
int c, m, n, r;
while ((c = getchar ()) != EOF) {
if (isdigit (c) )
push (c);
else if ((c == '+') || (c == '*')) {
m = pop ();
n = pop ();
r = (c == '+') ? n + m : n*m;
push (r);
} else if (c != ' ')
flagError ();
}
printf("% c", pop ());
}
What is the output of the program for the following input ?
5 2 * 3 3 2 + * + int func(int m, int n)
{
if (E) return 1;
else return(func(m -1, n) + func(m - 1, n - 1));
}
In the above function, which of the following is the correct expression for E?void insert(Q, X){
push(S1, X);
}
void delete(Q){
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. Let x and y be the number of push and pop operations performed respectively in the process. Which one of the following is true for all m and n? function fun (x:integer):integer;
Begin
If x > 100 then fun : x – 10
Else fun : fun(fun (x + 11))
End;