2 2 votes Suppose Stack operations are implemented using Queue Operation. Push(x) and Pop() are stack operations whereas enqueue(x) and dequeue are Queue operations. Consider the code: pop() { return Q.dequeue(); } push(x) { n= Q.size(); //Q.size() returns the number of elements in the Queue Q. Q.enqueue(x); for(i=0;i<n;i++) { X STATEMENT } } Find the missing statement X STATEMENT to perform Push() operation correctly (A) Q.enqueue(x) (B) Q.enqueue(Q.deqeue()) (C) Q.dequeue(Q.enqueue(x)) (D) Q.dequeue() Data Structures data-structures stack queue priority-queue + – Parshu gate 1.8k views answer comment Share Follow Print See all 2 Comments 2 2 Comments reply Rishabh Gupta 2 commented Nov 12, 2017 reply Follow flag It should be B. The program is removing elements in front of the newly inserted element and pushing it back to the queue, so they are now in the back of the newly inserted element. 2 2 replyShare VS commented Dec 23, 2017 reply Follow flag for(i=1;i<n;i++) //I think it should be 1 here { X STATEMENT } 0 0 replyShare Please log in or register to add a comment.
0 0 votes First run the push code and enter atleast 2 elements,then perform pop operation Spidey_guy answered Dec 28, 2019 Spidey_guy comment Share Follow 0 reply Please log in or register to add a comment.