GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Sep-2004, 03:36
Kay Chan Kay Chan is offline
New Member
 
Join Date: Sep 2004
Posts: 15
Kay Chan is on a distinguished road

Question about linked list and queue


I'm doing an assignment about linked list and queue. I have got a trouble in queue. I have added all the items from a txt file to queue. However, after displaying the context of the queue, it become empty.

1) how to solve this problem ?
2) In the linked list, it contains the restaurant name that is same as the queue. Is it possible to display other information after comparing the linked list with queue ?

CPP / C++ / C Code:
//Testing for display the Queue
void QueueView( Queue * q ) {
	
	QueueNode * tmp;
	char * item, * cuisine, *mode;

	cout << "In QueueView" << endl;
	while( !QueueEmpty(q) ){
	    tmp = q->front;
	    cout << tmp->item << tmp->cuisine << tmp->mode << endl;
                 q->front = tmp->next;
	}
}
Last edited by JdS : 02-Sep-2004 at 09:19. Reason: Please insert your example C/C++ codes between [c] and [/c] tags
  #2  
Old 02-Sep-2004, 05:31
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by Kay Chan
I'm doing an assignment about linked list and queue. I have got a trouble in queue. I have added all the items from a txt file to queue. However, after displaying the context of the queue, it become empty.

From your piece of code, I'm guessing you "damage" your first element. Well, actually, what I mean is that you change it's value, and then, when you try to start at the "front", you actually start at the end.

Quote:
Originally Posted by Kay Chan
q->front = tmp->next;

Instead, why not just use tmp to iterate throw the queue's elements?

CPP / C++ / C Code:
 cout << "In QueueView" << endl;
 tmp = q->front;
while( !QueueEmpty(q) )
{
  cout << tmp->item << tmp->cuisine << tmp->mode << endl;
  tmp = tmp->next;
}

Regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 02-Sep-2004, 05:40
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
I missed one
Quote:
Originally Posted by Kay Chan
2) In the linked list, it contains the restaurant name that is same as the queue. Is it possible to display other information after comparing the linked list with queue ?

What do you mean? Do you have a queue for each node in the linked list? Or do the two structures mirror each other?
Well, if this is the case (number 2 ) and if you have some piece of information in both structures (like the restaurant name), then you can just go through the linked list, and for each element, start a search through the queue, searching for the node which has a restaurant name field "equal" to the one in the list.

However, I'm not sure if this was your question....please explain.

Regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #4  
Old 02-Sep-2004, 06:45
Kay Chan Kay Chan is offline
New Member
 
Join Date: Sep 2004
Posts: 15
Kay Chan is on a distinguished road

linked list and queue


Actually, it has a linked list and each node of this list contains different restaurant name and number. And It has a queue that contains restaurant name, cuisine no and operation mode. Hence, it has a meny displaying the restaurant number and name on the screen. When user enter the number, the restaurant name in the linked list search the queue in order to show the cuisine no and operation mode. I get confused because of searching in the queue by using linked list item. Also I'm a beginner for writing program. That's why ....
  #5  
Old 02-Sep-2004, 07:14
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by Kay Chan
Actually, it has a linked list and each node of this list contains different restaurant name and number. And It has a queue that contains restaurant name, cuisine no and operation mode. Hence, it has a meny displaying the restaurant number and name on the screen. When user enter the number, the restaurant name in the linked list search the queue in order to show the cuisine no and operation mode. I get confused because of searching in the queue by using linked list item. Also I'm a beginner for writing program. That's why ....

Well, then just do as I said.
Something like this:

CPP / C++ / C Code:
string lookFor; // used to store the restaurant name we are looking for
BOOL bFound;
//Take the 1st element in the linked list
while ( ! ListEmpty(l) ) 
{
	
	lookFor = l->elem->restaurant; // Storing r. name for the current list element...

	bFound = FALSE;
	tmp = q->front; // Get the first element in the queue
	while( ( ! QueueEmpty(q) ) && ( ! bFound ) )
	{
		cout << tmp->item << tmp->cuisine << tmp->mode << endl;
		if ( ! strcmp(tmp->restaurant, lookFor) ) 
		{
			bFound = TRUE; // found what we were looking for!
		}
		else
			tmp = tmp->next; // still not found, search further in the queue
	}
	if ( bFound ) // It was found
	{
		//Do what you please with the info in *tmp*
		//it is the coresponding to the current list item
	}

	//Move to the next linked list element
	//........your code goes here.........
}

Hope I got it right...

Regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #6  
Old 02-Sep-2004, 08:59
Kay Chan Kay Chan is offline
New Member
 
Join Date: Sep 2004
Posts: 15
Kay Chan is on a distinguished road
After following your guide, the queue works for display its context. And now, I got another problem for comparing with linked list and queue. (char * input) is the restaurant name from linked list and it is used to compare w/ the queue tmp -> item(stored by char *). I can sure that both input and tmp -> item containing the restaurant name. However, the program is stopped by using the if loop and only displaying "true" no matter the data that are right or not. Did I do sth wrong b4 ?

CPP / C++ / C Code:
// For Menu 2 for displaying the cuisine and operation mode 
void DisplayCuisine( Queue * q, char * input){

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

	cout << "Display Cuisine & Operation Mode!" << endl ;	

	tmp = q->front;
	
	bool Found = false;

	cout << input << endl;

	while( ( tmp != NULL ) && !Found ){
		
		if ( strstr( tmp->item, input ) == 0 ){  //used strcmp
		    Found = true;                             // alse cause error
			cout << "true";                  
			tmp = tmp->next;
		} else {
			cout << "false";
			Found = false;
			tmp = tmp->next;	
		}

		if( Found ) {
		    cout << tmp->item << tmp->cuisine << endl;
		}
Last edited by JdS : 02-Sep-2004 at 09:20. Reason: Please insert your example C/C++ codes between [c] and [/c] tags
  #7  
Old 02-Sep-2004, 09:16
Kay Chan Kay Chan is offline
New Member
 
Join Date: Sep 2004
Posts: 15
Kay Chan is on a distinguished road
yes, I can do it now. THX
can we be a friend ?
  #8  
Old 02-Sep-2004, 09:27
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Found was true because strstr returns NULL if the second string is NOT found in the first one. Strcmp however is 0 if the two strings are alike.
So, if input is not found in the first element, Found is TRUE and the while lopp exits. That is why it is always true.
It should however work with strcmp. Except you should leave this line of code out, there is no need to go further if you found the node:

CPP / C++ / C Code:
if ( strstr( tmp->item, input ) == 0 )
{ //used strcmp
	Found = true; // alse cause error
	cout << "true";
	//tmp = tmp->next;
} 
else 
{
	cout << "false";
	Found = false;
	tmp = tmp->next;
}

Quote:
Originally Posted by Kay Chan
yes, I can do it now. THX
can we be a friend ?

You can just add me as a friend and I'll do likewise. You are welcome.

Regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogA Week in Kuwait by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
store a list of names in a Queue dinuka C++ Forum 1 01-Aug-2004 18:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:30.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.