By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,497 Members | 2,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,497 IT Pros & Developers. It's quick & easy.

How to do that ?

P: n/a
Kay
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;
}

}

Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.