Homechevron_rightEngineeringchevron_rightComputer Sciencechevron_rightData Structurechevron_rightA stack can be implemented using queue, but then we need to use atleas...

A stack can be implemented using queue, but then we need to use atleas...

  • Q. A stack can be implemented using queue, but then we need to use atleast:
  • filter_dramaExplanation
    Answer is : B

    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

Discussion

    No one started the discussion yet. Break the ice and start the conversation.
    Please Login to be part of the discussion.

Similar Questions

  • 1.

    The Preorder traversal of a tree given below is:

  • filter_dramaExplanation
    Answer is : A

    The correct solution is 'option 1'.

    Key Points

      Algo Preorder(tree root)

      {

    1. Visit the root node.
    2. Traverse the left subtree ( call Algo Preorder(left-subtree) ).
    3. Traverse the right subtree ( call Algo Preorder(right-subtree) ).

      }

    • Start with a root node 'A' move towards it's left subtree i.e 'B'. And again the node 'B' becomes the root node. Recursively calling by the above function prints the pre-order of the above Tree. 

    Thus, the correct answer is: A B D F E C G I H J K L

    Additional Information

    • To build a copy of the tree, a preorder traverse is used. Preorder traversal is often used to get the prefix on an expression tree.
    •  Here, some tree traversal techniques and flow.
    • In-order     -  F D B E A I G C J H L K
    • Pre-order  -  A B D F E C G I H J K L
    • Post-order - F D E B I G J L K H C A
    • Converse Inorder - K L H J C G I A E B D F
    • Converse Preorder- A C H K L J G I B E D F
    • Converse  Postorder- L K J H I G C E F D B A
    Tree traversal

     

    Method flow

    Inorder preorder postorder

    Converse Inorder

    Converse Preorder Converse  Postorder
    1. Left
    2. Root
    3. Right
    1. Root
    2. Left
    3. Right
    1. Left
    2. Right
    3. Root 
    1. Right
    2. Root
    3. Left
    1. Root
    2. Right
    3. Left
    1. Right
    2. Left
    3. Root
  • 2. A stack can be implemented using queue, but then we need to use atleast:
  • filter_dramaExplanation
    Answer is : B

    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

  • 3.

    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 ______.

  • filter_dramaExplanation
    Answer is : B

    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

  • 4.

    A queue is also called a _____ system.

    I. FIFO

    II. LIFO

    III. FILO

    IV. LILO 

  • filter_dramaExplanation
    Answer is : A
    • Queue is data structure which is First in First Out (FIFO) or Last in Last Out (LILO) which means the element which is inserted first comes out first form the queue or the elements inserted last comes out last from the queue
    • Stack is data structure which is Last in First Out (LIFO) or First in Last Out (FILO) which means the element which is popped last from stack is pushed first in stack, or element which is pushed first in stack is popped last from stack
  • 5. Queue structure is used in _______.
  • filter_dramaExplanation
    Answer is : A
    • Breadth-first search (BFS) and Depth First Search (DFS) is an algorithm for traversing or searching tree or graph data structures.
    • Breadth First Search (BFS) algorithm traverses a graph in a breadthwise manner and uses a queue to remember to get the next vertex to start a search, when a dead end occurs in any iteration.
    • Depth First Search (DFS) uses Stack data structure. DFS uses backtracking technique. Remember backtracking can proceed by Stack.

Data StructureTopics

leaderboardLeaderboard
  • Rahul Kumar

    191 Points

  • VIKRAM JEET

    54 Points

  • GEETHIKA CHOWDARY

    53 Points

  • sunita saini

    52 Points

  • Zain

    49 Points