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