Queue empty after display !!!

Discussion in 'C++' started by Kay, Sep 2, 2004.

  1. Kay

    Kay Guest

    how to display a queue context without removing it's item ? It is
    possible to do that ? I have run below function. However, after running
    it, the queue is empty. Did I do sth wrong ?

    while( Queue != NULL ){

    tmp = q->front;

    cout << tmp->item << tmp->cuisine << tmp->mode << endl;

    q->front = tmp->next;
    }
    Kay, Sep 2, 2004
    #1
    1. Advertising

  2. Kay

    Tim Love Guest

    Re: Queue empty after display !!!

    Kay <> writes:

    >how to display a queue context without removing it's item ? It is
    >possible to do that ? I have run below function. However, after running
    >it, the queue is empty. Did I do sth wrong ?


    > while( Queue != NULL ){
    >
    > tmp = q->front;
    >
    > cout << tmp->item << tmp->cuisine << tmp->mode << endl;
    >
    > q->front = tmp->next;
    > }

    What is Queue? Should you be resetting q->front? Why not do tmp=tmp->next?
    Tim Love, Sep 2, 2004
    #2
    1. Advertising

  3. Kay wrote:
    >
    > how to display a queue context without removing it's item ? It is
    > possible to do that ? I have run below function. However, after running
    > it, the queue is empty. Did I do sth wrong ?
    >
    > while( Queue != NULL ){
    >
    > tmp = q->front;
    >
    > cout << tmp->item << tmp->cuisine << tmp->mode << endl;
    >
    > q->front = tmp->next;
    > }


    Read again the reply I sent when I introduced strcmp to you.
    It shows a code exmple an how to do this.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Sep 2, 2004
    #3
  4. Kay wrote:
    > how to display a queue context without removing it's item ? It is
    > possible to do that ? I have run below function. However, after running
    > it, the queue is empty. Did I do sth wrong ?
    >
    > while( Queue != NULL ){
    >
    > tmp = q->front;
    >
    > cout << tmp->item << tmp->cuisine << tmp->mode << endl;
    >
    > q->front = tmp->next;
    > }
    >


    Read the reply I posted to you. I told you
    that you were removing elements from the queue.

    Remember that you're Queue is based on a linked list.
    Remember that the final link in a linked list is NULL.
    Remember that to go from one link to another, you
    follow the "next" field.

    Something like:
    tmp = q->front;
    while (tmp != NULL)
    {
    // Visit or process the node:

    cout << tmp->item << '\n';
    cout << tmp->cusine << '\n';
    cout << tmp->mode << '\n';
    cout << endl;

    // Advance to the next node:

    tmp = tmp->next;
    }

    Note that since your node fields are all char *
    that if you display them like your example above
    they will all "run into each other" and you will
    not see the separation. Most people will use
    a separation character or characters, such as
    a newline (like my example), tabs, or surround
    the text with double quotes (").

    Now let us see if you will actually read any of
    these replies and implement their suggestions...

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Sep 2, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul L. Du Bois

    Queue.Queue-like class without the busy-wait

    Paul L. Du Bois, Mar 24, 2005, in forum: Python
    Replies:
    29
    Views:
    1,057
    Antoon Pardon
    Apr 4, 2005
  2. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    680
    Russell Warren
    Jun 27, 2006
  3. Kris
    Replies:
    0
    Views:
    479
  4. Peng Yu
    Replies:
    2
    Views:
    135
  5. Cameron Simpson
    Replies:
    0
    Views:
    120
    Cameron Simpson
    Nov 23, 2012
Loading...

Share This Page