Linked list inserting items from two different funcion

K

Kay

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 ?


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

}
 
J

John Harrison

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

Well one thing you don't do in the second function is input any data. You
just seem to be adding the data that is already there, is there any reason
for that?

Secondly in the first function insert_name is only used (apparently) to add
data to the end of a list. In the second function you seem to be adding data
to the middle of a list. Has insert_name been written in such a way that it
can handle both cases? If not then that would explain why the second
function doesn't work.

It's impossible to give specific advice because there is far to much missing
from the code you posted, like the definition of List and Position and
insert_name. It's also a good idea, when posting, to say exactly what 'it
doesn't work' means.

john
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top