1,798 views
1 votes
1 votes

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?

Please log in or register to answer this question.

Related questions

2 votes
2 votes
2 answers
1
1 votes
1 votes
1 answer
2
3 votes
3 votes
3 answers
3
Ibtisam Sayyad asked Jan 12, 2018
7,916 views
What are the minimum enqueue and dequeue operations needed to perform pop operation for a stack which is implemented with two queues if there are already 10 elements in t...