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 03-Sep-2004, 09:35
Kay Chan Kay Chan is offline
New Member
 
Join Date: Sep 2004
Posts: 15
Kay Chan is on a distinguished road

Insert problem in linked list with two function code


There are two different function. The first one loads data from a txt
file and adds the name and num to the linked list. It can display the
context of the linked list. I'm designing to use second function to
input the data and add the new data to the same linked list. However, It
doesn't work. Did I miss sth or do sth wrong in the second function ?


CPP / C++ / C Code:
//Load data from restaurant text file
void rest_list(istream& is, Position p, List * restL) {
	
    string temp_name;

    p = ListHead(restL);
    int num = 1;

    //get the restaurant name in restaurnat.txt
    while ( getline(is, temp_name, "\n")) {
	char * name;

	//change type of name of restaurant from string to char
	name = new char[temp_name.length() + 1 ];
	strcpy( name, temp_name.c_str());
		
        if (!insert_name(restL, p, name, num )) {
           exit (EXIT_FAILURE);
        }

	//Move to the next node
	p = ListNext(restL, p);
        num++;

	}

}

CPP / C++ / C Code:
int AddRestaurant( List * l, Position pos, int num){

     ListNode *p;

     for ( p = l->head; p != NULL; p = p->next) {
         if( (int)p->number == num ){
	     cout << (char*)p->item;
	         if (!insert_name(l, pos, (char*)p->item, num )) {
		     exit (EXIT_FAILURE);
		 }
	      return 0;
	   }
			
}
Last edited by LuciWiz : 25-Mar-2006 at 12:53. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 03-Sep-2004, 09:52
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,031
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
Please use the code tags, or nobody will understand your code!
Also, please don't send me private messages regarding your problems. If I don't post an answer it is because:
1. I don't have time right now (I'm at work and I'm very busy)
2. I'm not online
3. (very important ) I DON'T KNOW
When I will have time, if I can, I will answer, trust me!

CPP / C++ / C Code:
//Load data from restaurant text file
void rest_list(istream& is, Position p, List * restL) 
{

	string temp_name;

	p = ListHead(restL);
	int num = 1;

	//get the restaurant name in restaurnat.txt
	while ( getline(is, temp_name, "\n")) 
	{
		char * name;

		//change type of name of restaurant from string to char
		name = new char[temp_name.length() + 1 ];
		strcpy( name, temp_name.c_str());

		if (!insert_name(restL, p, name, num )) 
		{
			exit (EXIT_FAILURE);
		}

		//Move to the next node
		p = ListNext(restL, p);
		num++;

	}

}

int AddRestaurant( List * l, Position pos, int num)
{

	ListNode *p;

	for ( p = l->head; p != NULL; p = p->next) 
	{
		if( (int)p->number == num )
		{
			cout << (char*)p->item;
			if (!insert_name(l, pos, (char*)p->item, num )) 
			{
				exit (EXIT_FAILURE);
			}
			return 0;
		}
	}
}

Also, you missed a bracket in your second function (I added it in).

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

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

Recent GIDBlogProblems with the Navy (Enlisted) 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
Merge sort on a linked list Temujin_12 C++ Forum 1 06-Mar-2008 20:33
Insert problem in Linked list Kay Chan C Programming Language 1 03-Sep-2004 17:06
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

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

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


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