Thursday, April 14, 2011

Queue using Stack

Enqueue():
Pop() all elements from StackA to StackB one by one - O(n)
Push() new element in StackA, so it stays at the bottom- O(1).
Pop() all elements from StackB back to StackA - O(n)
So, enqueue is O(n).


Dequeue():
Just Pop().
O(1)

No comments: