Linked list inserting items from two different funcion

Discussion in 'C++' started by Kay, Sep 3, 2004.

  1. Kay

    Kay Guest

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

    }
     
    Kay, Sep 3, 2004
    #1
    1. Advertising

  2. "Kay" <> wrote in message
    news:...
    > 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
     
    John Harrison, Sep 3, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    397
    Alvin Bruney - ASP.NET MVP
    Sep 28, 2005
  2. Robert Wierschke
    Replies:
    3
    Views:
    11,118
    Louise56
    Aug 3, 2004
  3. fool
    Replies:
    14
    Views:
    546
    Barry Schwarz
    Jul 3, 2006
  4. Pete C
    Replies:
    8
    Views:
    395
    Jay Nabonne
    Oct 9, 2005
  5. joshd
    Replies:
    12
    Views:
    704
    John Carson
    Oct 2, 2006
Loading...

Share This Page