Write a program to implement a queue using two stacks such that the in linear time and the dequeue operation runs in constant time in c++
Answers
Answered by
0
Answer:
Implementation of Queue using two Stacks
Enqueue operation:
Simply push the elements into the first stack.
Dequeue operation:
Pop from the second stack if the second stack is not empty.
If second stack is empty, pop from the first stack and push all the elements into second until the first stack becomes empty.
Now pop an element from the second stack.
Similar questions
Math,
2 months ago
Social Sciences,
2 months ago
Physics,
4 months ago
Hindi,
9 months ago
Math,
9 months ago