Thread: queue question

  1. #1
    Unregistered
    Guest

    queue question

    hey. i'm having trouble writting a push function for a queue. this is what i've got, in my head, it should work, but i'm not a computer.

    struct queue
    {
    char charecter;
    queue *next;
    };

    void push(queue* &top, char toPush)
    {
    queue *Temp;
    queue *Traverser;
    Temp = new queue;
    Temp->charecter = toPush;
    Temp->next = NULL;
    if(top->next == NULL)
    {
    cout << "pushing to top";
    top->charecter = toPush;
    top->next = new queue; //this line seems to cause the error in visual c++ 6
    }
    else
    {
    for(Traverser = top ; Traverser->next != NULL ; Traverser = Traverser->next)
    {
    }
    Traverser = Temp;
    cout << "Qu char: " << Traverser->charecter << '\n';
    }
    }

    the push function is what's giving me the trouble. the pop function works fine. i'll post it here incase anyone who helps needs it.

    char pop(queue* &top)
    {
    char toReturn = top->charecter;
    top = top->next;
    return toReturn;
    }

    and here is my main

    void main()
    {
    queue Qu;
    Qu = new queue;
    Qu->next = NULL;
    push('a');
    cout << pop(Qu);
    }

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    top->next = new queue; //this line seems to cause the error in visual c++ 6
    If you're pushing to the top of the queue, you'll want to check whether top==NULL, not top->next. Then you want top=Temp, there's no need to allocate further storage.

    Also, when using your Traverser, it will Traverse until Traverse->next != NULL, so Traverse will point to something, so you probably want Traverse->next = Temp and when poping you'll want to delete the top of the queue -

    char pop(queue* &top)
    {
    queue* temp=top;
    char toReturn = top->charecter;
    top = top->next;
    delete temp;
    return toReturn;
    }
    zen

  3. #3
    Unregistered
    Guest
    i got it to work...kinda

    i can push once only. i can pop it off and everything. the trouble comes when i try to push something into the queue if there is something at the top. i rewrote the code a little. here it is:

    void push(queue* &top, char toPush)
    {
    queue *Temp;
    queue *Traverser;
    Temp = new queue;
    Temp->charecter = toPush;
    Temp->next = NULL;
    if(top == NULL)
    {
    cout << "pushing to top";
    top = new queue; //give top a location
    top->charecter = toPush;
    }
    else //the error is somewhere in here
    {
    for(Traverser = top ; Traverser != NULL ; Traverser = Traverser->next)
    {
    }
    Traverser = Temp;
    cout << "Qu char: " << Traverser->charecter << '\n';
    }
    }


    also, i made it so that Qu = NULL in the beginning, not Qu->next = NULL

  4. #4
    Unregistered
    Guest
    i fixed it so nevermind.

    just for the future welfare of anyone who might need to know this stuff, to fix it i just made top->next = NULL when top==NULL during the push

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with FIFO Queue using Singly Linked Lists
    By astou in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2008, 02:36 PM
  2. Simple Queue Question
    By RobJ in forum C++ Programming
    Replies: 5
    Last Post: 05-27-2007, 09:28 AM
  3. Game programming task
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 01-06-2006, 12:10 AM
  4. Is this a true Queue?
    By chad101 in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2005, 06:00 AM
  5. Pathfinding AI? (Not the A* Algorithim)
    By harryP in forum C++ Programming
    Replies: 22
    Last Post: 08-01-2003, 02:32 PM