2,767 views
1 1 vote

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.

Position:
Show:

Related questions

2 2 votes
2 2 answers
2.4k
2.4k views
Gurdeep Saini asked Jan 2, 2019
2,429 views
true/false ?) if stack is implemented as a array,all operation push ,pop ,is emptystack(),delete stack() can be performed in constant time.)if stack is implemented as a l...
1 1 vote
1 1 answer
1.7k
1.7k views
Ravi prakash pandey asked Apr 7, 2018
1,664 views
Hi please verify meWe can implement a stack using only one queue.Like first insert into queue and for popping a element from stack dequeue n-1 element from queue and enqu...
3 3 votes
3 3 answers
8.6k
8.6k views
Ibtisam Sayyad asked Jan 12, 2018
8,562 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...
10 10 votes
2 2 answers
480
480 views
GO Classes asked Jul 27
480 views
Given a stack $S$ with $5$ elements from top to bottom as:$2, 4, 6, 8, 10$and an empty queue $Q$.First, remove the elements one by one from $S$ and insert them into $Q$.T...