How to do that ?

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

  1. Kay

    Kay Guest

    There is a queue containing three different items - name, number,
    character. Then, it has a linked list containing the name that is as
    same as the queue. If I want to display the the number and character by
    comparing the name with the queue and the list, is it possible to show
    the number and character from the queue ? If yes, how to do that ?

    The funtion has two arguement - *q and *input form the linked list. This
    function can't do what I have mention as above even I have a if loop.
    How to do what I have mentioned.

    void DisplayCuisineByRestaurant( Queue * q, char * input){

    QueueNode * tmp;
    char * item, * cuisine, *mode;

    //checking the queue until empty
    while(!QueueEmpty(q)){

    tmp = q->front;

    item = tmp->item;
    cuisine = tmp->cuisine;
    mode = tmp->mode;

    ----> if( strcmp( input, cuisine ) == 0 ){
    ----> cout << item << mode << endl;
    ----> }

    q->front = tmp->next;
    }

    }
    Kay, Sep 1, 2004
    #1
    1. Advertising

  2. Kay wrote:
    > There is a queue containing three different items - name, number,
    > character. Then, it has a linked list containing the name that is as
    > same as the queue. If I want to display the the number and character by
    > comparing the name with the queue and the list, is it possible to show
    > the number and character from the queue ? If yes, how to do that ?
    >
    > The funtion has two arguement - *q and *input form the linked list. This
    > function can't do what I have mention as above even I have a if loop.
    > How to do what I have mentioned.
    >
    > void DisplayCuisineByRestaurant( Queue * q, char * input){
    >
    > QueueNode * tmp;
    > char * item, * cuisine, *mode;
    >
    > //checking the queue until empty
    > while(!QueueEmpty(q)){

    Looks like you want to test for the end of the
    queue, not until it is empty. You don't want
    to remove items from a queue when you are
    iterating through it.

    >
    > tmp = q->front;
    >
    > item = tmp->item;
    > cuisine = tmp->cuisine;
    > mode = tmp->mode;

    According to this function, there is no reason
    to copy the data from the node.


    >

    cout << "Visiting node:\n"
    << " item: " << tmp->item << '\n'
    << " cusine: " << tmp->cuisine << '\n'
    << " mode: " << tmp->mode << '\n'
    << endl;


    > ----> if( strcmp( input, cuisine ) == 0 ){
    > ----> cout << item << mode << endl;

    cout << "Cusine equal to \"" << input << "\"" << endl;
    > ----> }

    else
    {
    cout << "Not equal" << endl;
    }
    >
    > q->front = tmp->next;

    Here you would want to advance to the next item,
    not remove it.


    > }
    >
    > }
    >

    I suggest that you change your design so that this function
    iterates, or visits, each item in the queue and does NOT
    remove the items. Displaying the contents of a container
    is generally a passive action: the displaying does not alter
    the contents of the container. Change the design to alter
    the contents after displaying them.

    Also, try using a debugger.


    --
    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 1, 2004
    #2
    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.

Share This Page