Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Linked list inserting items from two different funcion

Reply
Thread Tools

Linked list inserting items from two different funcion

 
 
Kay
Guest
Posts: n/a
 
      09-03-2004
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;
}

}

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-03-2004

"Kay" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting a new node in an ordered linked list francesco C Programming 5 03-16-2011 02:21 PM
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM
Generating a char* from a linked list of linked lists Chris Ritchey C Programming 7 07-10-2003 10:12 PM



Advertisments