I recently came across this question
What is the BEST-WORST case time to add and remove elements from a queue that has been implemented wit开发者_JAVA技巧h the help of 2 stacks.
could not come up with great answer... can you all suggest!!
Well... I honestly can't see a WORST-BEST scenario here.. the best scenario is when you only have one element on the list for both the queue and the stack will return the same thing (disregarding of course an empty queue =D).
But this operation, for must cases, will need to perform two operations on each element of the stack for each "push" to the queue... which means you get O(2n).... And this operation is lineal so... the more elements you have the worse it gets and gets...
精彩评论