Thread: need help with a function

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    23

    Unhappy need help with a function

    I need help with my add_at_back function. I'm trying to add an element to the end of a linked list and everytime I compile my program and run it I don't see the element at the end of the list.
    I think the while loop is correct. I just have problems with creating a new node and adding it to the back of the list.

    #include<iostream.h>
    using std :: cin;
    using std :: cout;
    using std :: endl;

    #include<stddef.h>

    struct node {
    int value;
    node* next;
    };

    node* make_a_list();
    void print_list(node *list);
    node* add_at_front(node* head, int addvalue);
    void add_at_back(node *list, int addvalue);

    int main(){
    node* mylist;

    // create the list and print it
    mylist = make_a_list();
    print_list(mylist);


    add_at_back( mylist, 40);
    print_list(mylist);


    mylist = add_at_front( mylist, 1);
    print_list(mylist);

    return 0;
    }

    node* make_a_list()
    // This function creates a linked list containing the even numbers
    // from 2 to 20. The pointer of the list is returned to the
    // calling function.
    {
    node *head = NULL, *tptr;
    int num = 20;


    while(num){
    tptr = new node;
    tptr->value = num;
    tptr->next = head;

    head = tptr;

    num -= 2;
    }

    return head;
    }


    void print_list(node *list)
    // This function accepts as input a pointer to the list
    // and prints out all of the elements of the list.
    // Each element is separated by a few spaces and
    // the last element of the list is terminated by
    // a newline.
    {
    node *ptr = list;

    // loop through entire list
    while(ptr != NULL) {


    // print each element
    cout <<" " << ptr->value;
    ptr = ptr->next;
    }
    cout << endl;
    return;
    }




    node* add_at_front(node* head, int addvalue)
    // This function accepts as input a pointer to the list
    // and an integer (addvalue). This integer is added
    // to the front of the list. The pointer to the start
    // of the list is then returned.
    {

    node *newptr;

    // create a new node
    newptr = new node;
    newptr->value = addvalue;
    newptr->next = head;

    // return pointer that points to front of list
    head = newptr;

    return head;

    }





    void add_at_back(node *list, int addvalue)
    // This function accepts as input a pointer to the list
    // and an integer (addvalue). This integer is added
    // to the end of the list.
    {

    node *newptr, *tempptr=list;

    // loop until you reach last node in list
    while(tempptr != NULL) {
    tempptr->value;
    tempptr = tempptr->next;

    // create a new node with appropriate values
    // and add it to the list
    newptr = tempptr->next;
    newptr = new node;
    newptr->value = addvalue;
    newptr->next = NULL;

    }
    return;
    }

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Didn't this help?
    zen

  3. #3
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    I saw this one at bolt~

    You should at least make your code a kind of more organized with the header files and name space use("whatta 'n' hell ov compiler u"se anyway);
    Ünicode¬>world = 10.0£

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    13
    Ok, this is WAY off. There are quite a few errors. First, this:

    // loop until you reach last node in list
    while(tempptr != NULL) {
    tempptr->value; //UNNECESSARY
    tempptr = tempptr->next;

    Is just plain wrong. The first line in the while loop isn't even necessary as it doesn't do anything other than eat up processor cycles. There's no reason to dereference the structure there.

    Second, your while loop should end after the line:
    tempptr = tempptr->next;

    But even that is wrong. Using that, tempptr is going to become NULL before you are ready to insert anything. If you try to dereference a NULL pointer to do the insertion (which would be wrong anyway), you are most likely going to get an access violation or worse. Instead, this is how you're loop should look:

    while(tempptr->next != NULL)
    tempptr = tempptr->next;

    You need to put a real simple if statement at the beginning of the function to check for an empty list (the base case) like this:

    if(list==NULL)
    {
    //add code to set list equal to the new node
    return;
    }


    Here is a working function:

    void add_at_back(node *list, int addvalue)
    // This function accepts as input a pointer to the list
    // and an integer (addvalue). This integer is added
    // to the end of the list.
    {

    node *newptr, *tempptr=list;

    if(list == NULL)
    {
    list = new node;
    list->value = addvalue;
    list->next = NULL;
    return;
    }

    // loop until you reach last node in list
    while(tempptr->next != NULL)
    tempptr = tempptr->next;
    //end of while loop

    // create a new node with appropriate values
    // and add it to the list
    newptr = new node;
    tempptr->next = newptr;
    newptr->value = addvalue;
    newptr->next = NULL;

    return;
    }

    That *should* work, but I don't have a compiler here, so I'm not 100% on that.

    When working with linked lists, remember that you must keep a valid pointer to whatever node comes before the point of insertion (and after in the case of a multi-directional linked list). You do NOT want to try to dereference a NULL pointer as that will cause some rather strange results.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM