473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

stack and queues

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

5 2161
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
donbock
2,426 Expert 2GB
@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
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
hype261
207 100+
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
@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

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

Similar topics

7
by: Anand Pillai | last post by:
The standard Python Queue module, allows to generate queues that have no size limit, by passing the size argument as <= 0. q = Queue(0) In a multithreaded application, these queues could be...
0
by: Aki Niimura | last post by:
Hello everyone. I'm trying to control a program from a Python program using IPC. Although using a socket is a common choice for such applications, I would like to use SysV message queues...
4
by: dogbite | last post by:
I need help with an understanding and completing a project in C++ which involves takingm a string of characters Example /*-A+BCDE as an input and outputting a postfix expression ABC+-D*E/. I have...
4
by: Ice | last post by:
Hi there, I'm not sure if this is the right group for this- If it isn't, could anyone point me in the right direction? For our data structures exam, we are usually asked to implement the...
5
by: mike79 | last post by:
Hi all, I have attempted to implement a queue in C. So far all is well, but I have a question to ask you experts in regards to my implementation. Say I have a queue of integers. My queue...
7
by: unified | last post by:
Ok, I'm working on a program that is supposed to compare each letter of a string that is put into a stack and a queue. It is supposed to tell whether or not a word is a palindrome or not. (a...
3
by: Locia | last post by:
How can I read stack element from top at the bottom? IEnumerator have only MoveNext() method and I suppone that begin from the bottom.
4
by: deanfamily | last post by:
I have a rather pecurliar C++ assignment. I need to create a program (using a stack or queue) to verify if the grouping symbols in an arithmetic expression match. For example: {25 + (3 - 6) * 8}...
7
by: DevNull | last post by:
Hello everyone, I decided to pick c++ back up after not really having used it much in 10 years. Just as a point of refference, there was no standard C++ last time I used regularly. Anyways...
5
by: ravi | last post by:
Can any body tell me How to implement a stack using two queues Thax in advance
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.