The Gateway to Computer Science Excellence
0 votes
286 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 by Junior (657 points) | 286 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.........................
by Loyal (6.2k points)
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

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,391 answers
198,591 comments
105,442 users