How to do that ?

K

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;
}

}
 
T

Thomas Matthews

Kay said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top