Welcome to Dream.In.Code
Getting C++ Help is Easy!

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!




Stack and Queue help

2 Pages V  1 2 >  
Reply to this topicStart new topic

Stack and Queue help

Arshan
11 Oct, 2007 - 12:30 PM
Post #1

New D.I.C Head
*

Joined: 11 Oct, 2007
Posts: 9


My Contributions
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)

CODE

#include <iostream>
#include <iomanip>
#include <queue>

using namespace std;


QUEUE*createQueue (void)
{
        QUEUE* queue;

}
queue = (QUEUE*) Malloc (sizeof (QUEUE));
if (queue)
{
        queue->front = NULL;
        queue->rear = NULL;
        queue->count = 0;
}
return queue;


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.

Thanks for your help, sorry for the long post.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Stack And Queue Help
11 Oct, 2007 - 12:41 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
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
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Stack And Queue Help
11 Oct, 2007 - 12:41 PM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
s1,s2,q1,q2 are all variables in your main function that are being used, but have not been declared.
User is online!Profile CardPM
+Quote Post

Arshan
RE: Stack And Queue Help
11 Oct, 2007 - 12:55 PM
Post #4

New D.I.C Head
*

Joined: 11 Oct, 2007
Posts: 9


My Contributions
QUOTE(jjhaag @ 11 Oct, 2007 - 01:41 PM) *

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:

CODE

#include <iostream>
#include <iomanip>
#include <stack>
#include <queue>





using namespace std;

int s1;
int s2;
int q1;
int q2;

int main ()
{



createStack (s1);
createStack (s2);
createQueue (q1);
createQueue (q2);

}


The errors I get are:

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
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Stack And Queue Help
11 Oct, 2007 - 01:25 PM
Post #5

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
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().
CODE

#include <iostream>
#include <queue>

using namespace std;

int main() {

    queue<int> line;
    while (line.size() < 5) {
        line.push(rand());
    }
    while ( !line.empty() ) {
        cout << line.front() << endl;
        line.pop();
    }

    return 0;
}


hope that helps get you started,

-jjh

This post has been edited by jjhaag: 11 Oct, 2007 - 01:27 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Stack And Queue Help
11 Oct, 2007 - 01:28 PM
Post #6

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
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
User is offlineProfile CardPM
+Quote Post

Arshan
RE: Stack And Queue Help
11 Oct, 2007 - 01:39 PM
Post #7

New D.I.C Head
*

Joined: 11 Oct, 2007
Posts: 9


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Arshan
RE: Stack And Queue Help
11 Oct, 2007 - 02:47 PM
Post #8

New D.I.C Head
*

Joined: 11 Oct, 2007
Posts: 9


My Contributions
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.

CODE
#include <iostream>
#include <iomanip>
#include <stack>
#include <queue>





#include <iostream>
#include <queue>

using namespace std;

int main()


{
int menu;
stack<int> s1;
stack<int> s2;
queue<int> q1;
queue<int> q2;
int inn;
int numa;
int numn;
char yes;


while (menu !=3)
{
cout << "Please choose a function from the menu" << endl;
cout << "1. Insert" << endl;
cout << "2. Delete" << endl;
cout << "3. End" << endl;

cin >> menu;
cout << endl;

if(menu == 1)
{
     cout << "Please insert an interger";
     cin >> inn;
     s1.push(inn);
     q1.push(inn);
     cout << "Stack 1 top: " << s1.top() << endl;
     cout << "Queue 1 front: " << q1.front() << endl;

}
if (menu == 2)
{
     s1.top == numa;
     q1.front == numn;
     s1.pop();
     q2.push(numa);
     q1.pop();
     s2.push(numn);
     cout << "Stack 1 top: " << s1.top()<< endl;
     cout << "Queue 1 front: " << q1.front() << endl;
     cout << "Stack 2 top: " << s2.top() << endl;
     cout << "Queue 2 front: " << q2.front() << endl;
}
}

    
return 0;



}


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
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Stack And Queue Help
11 Oct, 2007 - 03:57 PM
Post #9

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
the problem lies in these statements:
CODE
s1.top == numa;
q1.front == numn;


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
User is offlineProfile CardPM
+Quote Post

Arshan
RE: Stack And Queue Help
11 Oct, 2007 - 05:04 PM
Post #10

New D.I.C Head
*

Joined: 11 Oct, 2007
Posts: 9


My Contributions
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 smile.gif .
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Stack And Queue Help
11 Oct, 2007 - 05:10 PM
Post #11

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(Arshan @ 11 Oct, 2007 - 06:04 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 smile.gif .

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 smile.gif
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Stack And Queue Help
11 Oct, 2007 - 05:20 PM
Post #12

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
kudos and street cred...can't forget the street cred smile.gif

-jjh
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/1/08 08:34PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month