want a c++ code for this full ?
Implement a Queue using two stacks Make it Dequeue efficient
You need to compromise time complexity of enqueue to make its dequeue efficient.
not able to think how to do ??
can you explain please :((
Basic principle of Queue is First-in First-out, while that of Stack is First-in Last-out, so, if you want to make dequeue as efficient from stack as it is from queue then first element should always remain at top, then followed by second and so on.
So, major work is done in enqueue so that order of elements in stack remain as needed above. Now lets suppose you have k elements in the stack with top element as the first element arrived, followed by second and so on. Now, for next element, since it is latest element it should be retrieved after k elements which arrived before according to working principle of queue. So, if we empty this stack into another stack simply by removing top element of parent stack and pushing it in auxiliary stack until parent stack is empty and then push latest element into the parent stack followed by all elements from auxiliary stack the order remains intact and now for k+1 elements in parent stack.