1 votes 1 votes I have a doubt regarding " implementing queue using 2 stacks " For an EQueue operation there is a push operation in one of the stack that is OK but when we are doing first DQueue operation why we need to pop each element from from one stack and push all into other stack rather than pop elements from one stack until it has one element and after that pop that element from the same stack . saurabh rai asked Oct 5, 2016 saurabh rai 525 views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
1 votes 1 votes Because if you won't push them onto 2nd stack you will lose elements after first one. So when you perform dequeue next time from where will second element come so that we can dequeue it Shivam Chauhan answered Oct 5, 2016 Shivam Chauhan comment Share Follow See all 7 Comments See all 7 7 Comments reply Show 4 previous comments saurabh rai commented Oct 5, 2016 reply Follow Share it is obvious store in other stack. 0 votes 0 votes mcjoshi commented Oct 5, 2016 reply Follow Share Means pop all first and store then in second one( you will lose elements), while pop one store in second approach gives correct answer. 1 votes 1 votes Lakshman Bhaiya commented Apr 8, 2018 reply Follow Share @ mcjoshi Let suppose Stack1 for Push(x) (Enqueue) and Stack2 for Pop() (Dequeue) operation. If I Push all element in Stack1 and Stack2 is empty then I Pop() one by one element from Stack1 and Push(x) into the Stack2.And then Pop() one by one from Stack2. This is good or not? Please correct me if I'm wrong. 0 votes 0 votes Please log in or register to add a comment.