search
Log In
0 votes
522 views
Hi please verify me

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?
in Programming 522 views
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 Answer

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

0 votes
0 answers
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?
asked May 5, 2018 in Programming Na462 752 views
0 votes
1 answer
2
659 views
On other sources, it is given that we need to assign high priorities to newly inserted element in case of stack otherwise low priority to newly inserted element in case of queue. My doubt here is that shouldn't stack be implemented with max-heap priority queue and queue with min-heap priority queue keeping above assumption of assigning priorities to newly inserted element?
asked Jun 17, 2018 in Algorithms pallaviamu 659 views
0 votes
1 answer
3
346 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 linked list ,all operation ,is emptystack(),delete stack() can be performed in constant time.
asked Jan 2, 2019 in Programming Gurdeep Saini 346 views
3 votes
1 answer
4
670 views
In implementation of queue using stack, deletion of second element from front take Ο(1) time, when insertion take Ο(n) time. Which of the following is correct ? True / False
asked Jan 20, 2018 in Algorithms Hemant Parihar 670 views
...