By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

stack and queues

P: 2
how can be create a stack using two queues?
Sep 17 '10 #1

✓ answered by srbakshi

Hi Donbock,

My thinking was as follows:

We have to implement the Push and Pop functionality of a stack using the Enqueue and Dequeue functionality of a queue. Here's what I thought could be done:

1. Push:

Will be same as an enqueue operation on a queue.

2. Pop:

Dequeue n-1 (n being the total number of items in the queue) number of items from the queue and enqueue them in the same queue. Then dequeue once more to get the last item.

Eg: Queue has 4-3-2-1 (Pop should fetch us 1 in this eg)
We dequeue 3 (size of queue - 1) items and enqueue them in the same queue:

3-2-1-4
2-1-4-3
1-4-3-2

Now dequeue once more to get the last item, i.e. 1.

:D

Share this Question
Share on Google+
5 Replies


P: 18
I think you can implement a stack using just one queue. You can use the enqueue/dequeue functions of the queue to implement the push/pop functions of the stack.
Sep 17 '10 #2

Expert 100+
P: 2,400
@srbakshi:

Typically, queue access functions put items in one end of a circular list and pull them out from the other end.

Typically, stack access functions put items in one end of a circular list and pull them out from the same end.

Do the enqueue/dequeue functions of the queue allow you to specify which end the circular list is being used?
Sep 17 '10 #3

P: 18
Hi Donbock,

My thinking was as follows:

We have to implement the Push and Pop functionality of a stack using the Enqueue and Dequeue functionality of a queue. Here's what I thought could be done:

1. Push:

Will be same as an enqueue operation on a queue.

2. Pop:

Dequeue n-1 (n being the total number of items in the queue) number of items from the queue and enqueue them in the same queue. Then dequeue once more to get the last item.

Eg: Queue has 4-3-2-1 (Pop should fetch us 1 in this eg)
We dequeue 3 (size of queue - 1) items and enqueue them in the same queue:

3-2-1-4
2-1-4-3
1-4-3-2

Now dequeue once more to get the last item, i.e. 1.

:D
Sep 17 '10 #4

100+
P: 207
Srbakshi even though this would work it is very inefficient to do so. Your stack pop has a O(n) complexity compared to a O(1) for a regular stack.

If I had to created a stack using two queues, that had common data using the circular list method, I would just have one queue point to the beginning of the list and the second queue point at the end.

When I pushed some data I would call enqueue on the second queue and when I poped a data member I would call dequeue on the first queue. You would have to keep the pointers right for both queues during the enqueue or dequeue operation, but it should be do able.
Sep 17 '10 #5

P: 18
@abhinav
Although my approach works you might want to look at hype261's answer as well since for a stack having a large number of items, my approach would be very inefficient. :)
Sep 18 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.