# Stack and queue

522 views

We 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 enque into queue and then pop last element and do the same each time......try it and verify that I am right or wrong?
2

yes, assuming you know the size of stack/queue at each point. reference: https://www.geeksforgeeks.org/implement-a-stack-using-single-queue/

1 vote
please verify ur self first that when ur are doing n-1 dequeue where r u storing them .....nd now after popping desired element from where u r picking those n-1 elements to enqueing the into queue for further iterations.........................
0
no it is possible.....when i m doing dequeue then i m inserting that element at the tail of queue also.

please do it on pen and paper
0
Lets suppose size of queue in of n elements only , then  i want to perform stack op , now according to u for pop first element u have to dequeue other n-1 elements ,now here is point where u will store all u have only place vacant.

## Related questions

1
752 views
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 ... 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?