In implementation of queue using stack, deletion of second element from front take Ο(n) time, when insertion take Ο(1) time.
Is it a true statement ?
Well it can be true isn't it ?
because suppose elements come we simply push them without taking care of the order in which they are inserted so O(1) time nd for delete pop every one of them and push it into another stack and pop the first element so O(n).
And this statement
In implementation of queue using stack, deletion of second element from front take Ο(1) time, when insertion take Ο(n) time.
Is also correct. Here because we are taking care of order in which elements are inserted into the stack which makes time complexity change.
Approach:
enQueue(q, x)
1) While stack1 is not empty, push everything from satck1 to stack2.
2) Push x to stack1 .
3) Push everything back to stack1.
dnQueue(q)
1) If stack1 is empty then error
2) Pop an item from stack1 and return it
Am i right?