Hi,
I am reading ahead for a C++ class that i am taking next quarter. I just read stacks and queues and know how to implement both seperately.

I was wandering if there is a program that has been written in which both stacks and queues were implemented in one code.
If there is,
could you post it up.
Thanks

Recommended Answers

All 17 Replies

Include <stack> and <queue>. You can also use a vector or deque as both a stack and a queue at the same time. I'm sure that's not what you meant, but we don't just give away code that smells even a bit like homework.

the stanard libary <stack> and <queue> are just container adapters, eg.

template< typename T, typename CNTR = std::deque<T> >
          struct queue ;

they just adapt an implementation of a sequence container to provide the interface required for a stack/queue.

...You can also use a vector or deque as both a stack and a queue...

a vector cannot be used as the container to be adapted for a std::queue; the container needs to support push_back and a pop_front. using a vector to implement a queue would not be a good idea (performance).

>a vector cannot be used as the container to be adapted for a std::queue;
>the container needs to support push_back and a pop_front.
Try reading for comprehension next time. That was an either/or statement. You can use the stack and queue adapters OR vector and deque because the adapters are notoriously inflexible in practice. Of course, you could play games with me about using stacks to simulate a queue and queues to simulate a stack if you want to try to recover from your mistake, but that would be reaching.

>using a vector to implement a queue would not be a good idea (performance).
You're not very creative, are you? Just because push_front and pop_front aren't available to a vector doesn't mean you can't achieve the correct functionality of a queue in an efficient manner.

Hi, very nice of you to reply. However, i did not understand your reply. it seems like you did not understand my question. I was asking if one could implement the operations of stacks and queues in the same program. thanks.

>a vector cannot be used as the container to be adapted for a std::queue;
>the container needs to support push_back and a pop_front.
Try reading for comprehension next time. That was an either/or statement. You can use the stack and queue adapters OR vector and deque because the adapters are notoriously inflexible in practice. Of course, you could play games with me about using stacks to simulate a queue and queues to simulate a stack if you want to try to recover from your mistake, but that would be reaching.

>using a vector to implement a queue would not be a good idea (performance).
You're not very creative, are you? Just because push_front and pop_front aren't available to a vector doesn't mean you can't achieve the correct functionality of a queue in an efficient manner.

I do my homework myself. I just wanted to see how the operations of stacks and queues are implemented in a code. That's how i learn from other codes. A solid explanation would be nice, but im sure you don't explain either. Thanks for your help anyway.


Include <stack> and <queue>. You can also use a vector or deque as both a stack and a queue at the same time. I'm sure that's not what you meant, but we don't just give away code that smells even a bit like homework.

>it seems like you did not understand my question.
I understood your request (since it wasn't really a question) perfectly. Allow me to quote myself on exactly why I didn't answer to your satisfaction:

Include <stack> and <queue>. You can also use a vector or deque as both a stack and a queue at the same time. I'm sure that's not what you meant, but we don't just give away code that smells even a bit like homework.

I can't be sure that you're not trying to get some homework done by pretending to be reading ahead. People use a lot of tricks like that to get free code, and I've seen most of them. So I'll require that you make an honest attempt on your own before I'll offer even a basic implementation to help you.

If you want ideas on how to proceed with your code, I'm happy to help. If you want suggestions on how to improve your code, I'm happy to help. If you want to do some Q&A about the theory and concepts of what you're working on, I'm happy to help. If you want freebies without investing any effort, I'm happy to tell you to piss off.

Oooooooooooooh!!!!!!!! I'm very :scared:. Don't worry, it is not that important. I'll just ask my teacher next quarter. Thanks for your help. I really appreciate it.
Good Bye:D

>it seems like you did not understand my question.
I understood your request (since it wasn't really a question) perfectly. Allow me to quote myself on exactly why I didn't answer to your satisfaction:

I can't be sure that you're not trying to get some homework done by pretending to be reading ahead. People use a lot of tricks like that to get free code, and I've seen most of them. So I'll require that you make an honest attempt on your own before I'll offer even a basic implementation to help you.

If you want ideas on how to proceed with your code, I'm happy to help. If you want suggestions on how to improve your code, I'm happy to help. If you want to do some Q&A about the theory and concepts of what you're working on, I'm happy to help. If you want freebies without investing any effort, I'm happy to tell you to piss off.

>I'll just ask my teacher next quarter. Thanks for your help. I really appreciate it.
You seem to have missed the point. I'm willing to help you. I can probably be of more help than your teacher as well. But you have to ask me questions that don't boil down to "gimme code". I can guarantee that your teacher will say roughly the same thing. If you want to be a baby and run off, that's fine. I'll just assume that I was correct in thinking you only wanted free homework answers.

I already told you, i do not have a homework. I finish school in a week. I just took an object oriented programming class and learned the basics i.e encapsulation, inheritance, polymorphism and multithreading. I am taking a data structures class next quarter. I am taking the same teacher that i took for OOD and she told me she was going to teach stacks and queues, binary trees, linked lists and all. I was just reading ahead. I don't know why that appears to be strange. I always read ahead. I was just wandering if the operations of stacks and queues could be implemented in the same program as most of the books i am reading, implement stacks in a seperate program, and queues in a different program. That's all. I just wanted to know if thats been done before and i wanted to see a code so i could see. Incase i was asked next quarter in an exam, i would have an idea.
It's not homework. I don't even bother asking for homework help here anymore coz, even when i posts my codes, i never get anyhelp. i either figure it out myself or my teacher helps me. she always does.
so i don't even ask homework questions because even when i show an effort, i never get any help.
This was just an inquiring mind.and I don't expect you to help me with this either. If u can, i'll appreciate it. So now, i would really appreciate it if i could get a yes or no answer. Can the operations of stacks and queues be implemented in the same program? I think they can. i just want to confirm. And like i always do, i'll figure it out on my own. It's really not that important as it's not homework and I maintain that fact.

Thanks for taking the time to read this.

>I already told you, i do not have a homework.
And I already told you, I have no reason to believe that. So far you've been talking a lot, but I haven't seen one bit of code that suggests you're trying to solve the problem on your own.

>I don't even bother asking for homework help here anymore coz, even
>when i posts my codes, i never get anyhelp
I'm sorry to hear that. Maybe you didn't ask a smart question. Maybe nobody was available who could answer your question and it fell down into the second page where nobody looks. But that's not a reason to stop asking.

>Can the operations of stacks and queues be implemented in the same program?
Yes.

>a vector cannot be used as the container to be adapted for a std::queue;
>the container needs to support push_back and a pop_front.
Try reading for comprehension next time. That was an either/or statement. You can use the stack and queue adapters OR vector and deque because the adapters are notoriously inflexible in practice. Of course, you could play games with me about using stacks to simulate a queue and queues to simulate a stack if you want to try to recover from your mistake, but that would be reaching.

i'm not interested in playing games with you.

>using a vector to implement a queue would not be a good idea (performance).
You're not very creative, are you? Just because push_front and pop_front aren't available to a vector doesn't mean you can't achieve the correct functionality of a queue in an efficient manner.

here is what ISO/IEC 14882:1998(E) has to say:

23.2.4 - Template class vector [lib.vector]
-1- A vector is a kind of sequence that supports random access iterators. In addition, it supports (amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time. Storage management is handled automatically, though hints can be given to improve efficiency.
-2- A vector satisfies all of the requirements of a container and of a reversible container (given in two tables in lib.container.requirements) and of a sequence, including most of the optional sequence requirements (lib.sequence.reqmts). The exceptions are the push_front and pop_front member functions, which are not provided.

23.2.1 - Template class deque [lib.deque]
-1- A deque is a kind of sequence that, like a vector (lib.vector), supports random access iterators. In addition, it supports constant time insert and erase operations at the beginning or the end;...
----------------------------------------------------------------
pop_front could easily be programmed on vectors as
v.erase(v.begin()); but would be inefficient.
i repeat; using a vector to implement a queue would not be a good idea (performance).

>here is what ISO/IEC 14882:1998(E) has to say:
I'm well aware of what the standard says.

>i repeat; using a vector to implement a queue would not be a good idea (performance).
And I repeat; you're not very creative, are you? Do you really think the only possible way to simulate queue behavior is by physically removing every item?

Do you really think the only possible way to simulate queue behavior is by physically removing every item?

no i do not. i do think that keeping objects alive after their logical lifetime is over is not efficient. (how inefficient depends on the kind of object).

>i do think that keeping objects alive after their logical lifetime is over is not efficient.
Space efficient or time efficient? If it's the former than I agree but would debate the practicality if the objects are relatively small. If it's the latter then you're just plain wrong wrong unless you can show me an implementation where doing nothing with an existing object is slower than calling the destructor and possibly reclaiming memory to the dynamic pool.

Gee!!! Thanks a lot for the condescending remarks. The fact that i didn't post any code should suggest that i do not have homework.
You've already given the "yes" that i needed. I'll just figure out how they can be implemented together.
One more thing!!! I always ask smart questions. I just never get smart answer.

Have a nice day.

>I already told you, i do not have a homework.
And I already told you, I have no reason to believe that. So far you've been talking a lot, but I haven't seen one bit of code that suggests you're trying to solve the problem on your own.

>I don't even bother asking for homework help here anymore coz, even
>when i posts my codes, i never get anyhelp
I'm sorry to hear that. Maybe you didn't ask a smart question. Maybe nobody was available who could answer your question and it fell down into the second page where nobody looks. But that's not a reason to stop asking.

>Can the operations of stacks and queues be implemented in the same program?
Yes.

commented: You've asked four questions on this forum. Three of them were answered well and one was a poorly asked question. I guess that makes you an arrogant prick. -Narue -2

>Gee!!! Thanks a lot for the condescending remarks.
You're welcome.

>The fact that i didn't post any code should suggest that i do not have homework.
Or it could suggest that you have homework but you're too lazy to do it. Given two equally probable situations, I'll chose the path that hurts your learning process the least.

>I always ask smart questions. I just never get smart answer.
You're not going to get very far with that kind of attitude.

I might not get far, but i'll get farther than you.
Im done replying your juvenile comments.

>Gee!!! Thanks a lot for the condescending remarks.
You're welcome.

>The fact that i didn't post any code should suggest that i do not have homework.
Or it could suggest that you have homework but you're too lazy to do it. Given two equally probable situations, I'll chose the path that hurts your learning process the least.

>I always ask smart questions. I just never get smart answer.
You're not going to get very far with that kind of attitude.

commented: Calling Narue juvenile, eh? You seem to be the one who's juvenile. -2
commented: Narue _is_ juvenile, in case you haven't noticed, joeprogrammer. +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.