Join 136,095 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,627 people online right now. Registration is fast and FREE... Join Now!
Alright, a little info. This seems like something would be easy to do, but I can't figure it out. I need to program a code that test create stack, create queue, push stack, pop stack, enque and deque. Theres more too it, but here is where I am having the problem. I have no idea how to create stacks or queues in C++. Our entire class all he taught us were algorithms. Now he wants us to do implent this in C++. The book has 1 coding example, I tried it and it doesn't work. I have no idea what to do since not once did we go over this, or were taught it. When I emailed for help my response was "Look it up on the internet for help". I'm really confused and frustrated at this point since I have no idea what to do.
I did at least attempt somethings though.
CODE
#include <iostream> #include <iomanip>
using namespace std;
int main () { createStack (s1); createStack (s2); createQueue (q1); createQueue (q2);
}
I got that form the algorithm's, hoping it would even be a start. Nothing worked at all, I have a feeling that's not even close to what I need. So I tried the ONLY example in the book on making a queue (Not even an example on making a stack, everything is algorithm's)
All I got from that was a bunch of errors, about undeclared variables. Sadly this is the best I can do. I'm pretty much at the end of my rope, since I can't get help from our professor, and I don't know much about C++ (I don't know why they are making me take this class as a business major, but it's pretty much hurting me). I don't know if anyone can at least help me get started or help me in anyway at all. I'm sure I'm missing a lot so I apologize in advance. I'm using Visual C++ 2005 Express Edition (What came free form Microsoft). Fair warning, I'm an idiot when it comes to this (Once again no idea why I have to take this class but I'm stuck) so try not to make fun of me too much if you can.
is the intent to write your own class/template for a queue, or simply to use and demonstrate the use of the STL queue? because you kind of have a mix of that here. and creating your own queue/stack class or template is substantially different that just using the builtin standard template library containers.
is the intent to write your own class/template for a queue, or simply to use and demonstrate the use of the STL queue? because you kind of have a mix of that here. and creating your own queue/stack class or template is substantially different that just using the builtin standard template library containers.
please clarify and we'll try to help.
-jjh
I guess we just need to use the builtin standard queue (and stack too). What I have to do is make 2 stacks and 2 queues, and input data one queue, and stack, then transfer it over to the other stack and queue with a delete command. Whichever one I need for that, I'm assume we can use the standard ones, but I may be wrong (Like I said I'm an idiot when it comes to this). It probably looks like I'm trying to do both because I'm trying to do things from two different sources.
QUOTE
s1,s2,q1,q2 are all variables in your main function that are being used, but have not been declared.
I added int declarations for s1,s2,q1,q2, however I still get undeclared errors for createStack and createQueue. I added
#include <stack> #include <queue>
But that still causes them to be errors. Am I missing another one?
Thanks for the replys.
Edit: If it helps heres what I added for the 1st code:
error C3861: 'createStack': identifier not found error C3861: 'createStack': identifier not found error C3861: 'createQueue': identifier not found error C3861: 'createQueue': identifier not found
I get the errors if I put the variables declartions in the main function or outside of it.
This post has been edited by Arshan: 11 Oct, 2007 - 01:04 PM
there are no methods createStack() and createQueue() for the respective STL containers.
to declare a queue, stack, or any other STL object (or any template class at all, really), you need to specify the type of data that it will hold by using <> after the container name:
CODE
queue<int> myQueueOfInts;
this is because the standard template libary containers are, well, templates. they can hold pretty much any kind of data you want to throw at them, but each instantiation can only hold one type of data, and you must tell the object what type of data you want it to hold when you declare it.
the following code is a little demonstration of using a queue to store the int data type. it instantiates an empty queue of ints, then uses push() to add 5 random integers to the queue (until the size() of the queue is 5 elements. it then outputs the contents of the queue by displaying the first object in the queue (obtained with a call to the method front()), and then removing that element with a call to pop().
Your definition of the createQueue function takes no arguments, yet in the main function, you use it as it should take one argument of type int. And be careful with the curly braces (code blocks).
<edit> -jjh beat me for a sec
This post has been edited by PennyBoki: 11 Oct, 2007 - 01:28 PM
Alright, I'll give it a shot. Now I can make the things, hopefully I can figure out the rest on my own or come close to it. Thanks for your help everyone.
Alright I'm back with some new questions. I made some progress and I'm doing better, but I got stuck again. For more details on my program. I need to make a menu that lets you insert or delete integers form 2 stacks and 2 queues. When I use insert, it prompts an input to an int to put into the first stack and queue. When I use delete it removes it form the first queue and stack and adds them to the 2nd stack and queue. However when I try to use delete, it removes them from the first fine, but it doesn't add them to the 2nd right. Here is my current code.
The other problem I have is when I try to delete the last int in the first 2 stacks it causes an error. Is there a way to fix this also? Everything else seems to work fine, which makes me happy for now, since I'm alot further than where I first was. Thanks again for your help
first, you should be calling these methods with top() and front() -the parentheses are required, otherwise this should give you a compiler error.
with those fixed, in these statements, you are testing whether s1.top is equal to numa, and q1.front is equal to numn. presumably you are trying to assign to numa the value of s1.top(), and assign to numn the value of q1.front().
with the way it is written, numa and numn are uninitialized (not set to a value), so you are comparing a known quantity (the values in s1.top() and q1.front()) to a garbage value. this will most likely evaluate to false, but the statement has no effect anyway, so it doesn't matter. then the garbage values are pushed into your queue and stack. this is where your weird output is coming from.
and as for the error when deleting, i believe that your problem is that you are calling top() and front() on empty stacks/queues, during your cout << statements. you should check to make sure that the stack or queue actually has an element in it before calling top() or front, since calling them on an empty container will result in an error.
hope that helps,
-jjh
This post has been edited by jjhaag: 11 Oct, 2007 - 04:05 PM
Did what you suggested, fixed the problem and everything works great. Theres one or two things I need to do to tidy it up, but other than that, the main code is done. Thanks a lot for everyones help you all are life savers .
Did what you suggested, fixed the problem and everything works great. Theres one or two things I need to do to tidy it up, but other than that, the main code is done. Thanks a lot for everyones help you all are life savers .
It would be nice of you when you'll have the workng code to share it with us, so that me and others will learn from it. Or better yet, you can submit it as a code snippet and you'll get kudos for it