A stack can be implemented using two queues. Let stack to be implemented be ‘x’ and queues used to implement be ‘a’ and ‘b’.
Method 1 (By push operation)
This method makes sure that the newly entered element is always at the front of ‘a’, so that pop operation just dequeues from ‘a’. ‘b’ is used to put every new element at front of ‘b’.
Method 2 (By making pop operation costly)
In a push operation, the new element is always enqueued to a. In pop() operation, if b is empty then all the elements except the last, are moved to b. Finally, the last element is dequeued from a and returned.
Therefore Option 2 is correct
The Preorder traversal of a tree given below is:
The correct solution is 'option 1'.
Key Points
Algo Preorder(tree root)
{
}
Thus, the correct answer is: A B D F E C G I H J K L
Additional Information
Tree traversal | ||||||
Method flow |
Inorder | preorder | postorder |
Converse Inorder |
Converse Preorder | Converse Postorder |
|
|
|
|
|
|
A stack can be implemented using two queues. Let stack to be implemented be ‘x’ and queues used to implement be ‘a’ and ‘b’.
Method 1 (By push operation)
This method makes sure that the newly entered element is always at the front of ‘a’, so that pop operation just dequeues from ‘a’. ‘b’ is used to put every new element at front of ‘b’.
Method 2 (By making pop operation costly)
In a push operation, the new element is always enqueued to a. In pop() operation, if b is empty then all the elements except the last, are moved to b. Finally, the last element is dequeued from a and returned.
Therefore Option 2 is correct
Consider the following recursive function.
Int function (int x, int y) {
If (y <= 0) return x;
return function (y, x%y);
}
The above recursive function computes ______.
Consider x = 10 and y = 25
CASE1:
If (y <= 0) return x; // 25<=0 (false)
return function (y, x%y); return function(25, 10)
CASE 2:
x = 25, y= 10
If (y <= 0) return x; // 10<=0 (False)
return function (y, x%y); // return function (10, 5)
CASE 3:
x =10, y =5
If (y <= 0) return x; // 5<=0 (false)
return function (y, x%y); //return function(5, 0)
CASE 4:
x = 5, y =0
If (y <= 0) return x; // condition true, return 5
return function (y, x%y);
Output = 5
Which is greatest common divisor of 10 and 25.
So, given program computes GCD of x and y
A queue is also called a _____ system.
I. FIFO
II. LIFO
III. FILO
IV. LILO
Total MCQS : 128
gradeTotal MCQS : 37
gradeTotal MCQS : 133
gradeTotal MCQS : 166
gradeTotal MCQS : 165
gradeTotal MCQS : 61
gradeTotal MCQS : 133
gradeTotal MCQS : 120
gradeTotal MCQS : 7
gradeTotal MCQS : 36
gradeTotal MCQS : 7
gradeTotal MCQS : 175
gradeTotal MCQS : 2533
gradeTotal MCQS : 9
gradeTotal MCQS : 11
grade191 Points
54 Points
53 Points
52 Points
49 Points