need help with a function

This is a discussion on need help with a function within the C++ Programming forums, part of the General Programming Boards category; I'm new at nodes and linked lists. I'm having a problem with my make_a_list function. It's supposed to call add_at_back ...

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

    need help with a function

    I'm new at nodes and linked lists. I'm having a problem with my make_a_list function. It's supposed to call add_at_back and add_at_front functions alternately to create a linked list. I have code written for my make_a_list function, but it doesn't work. I'm thinking that maybe it's because of the pointers or the last two codes that I wrote that makes the function not work, but I'm not sure.
    #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);
    node* MakeNewNode(int);
    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, *tptr2;
    for(int i = 20; i >= 0; i - 4) {
    tptr = add_at_back(head, i);
    tptr2 = add_at_front(head, i -2);
    }
    head = tptr2;

    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 = MakeNewNode(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;

    if(list == NULL) {
    MakeNewNode(addvalue);

    return;
    }
    // loop until you reach last node in list
    while(tempptr->next != NULL) {
    tempptr = tempptr->next;
    }
    // create a new node with appropriate values
    // and add it to the list
    newptr = MakeNewNode(addvalue);
    tempptr->next = newptr;
    newptr->next = NULL;

    return;
    }
    node* MakeNewNode(int x) {

    node *ptr;

    ptr = new node;
    ptr->value = x;

    return ptr;
    }

  2. #2
    Registered User jasrajva's Avatar
    Join Date
    Oct 2001
    Posts
    99

    Arrow for(...;...;i-4)??????

    i think you meant for(...;...;i-=4)
    below
    // 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, *tptr2;
    for(int i = 20; i >= 0; i - 4) { <-----------------------HERE
    tptr = add_at_back(head, i);
    tptr2 = add_at_front(head, i -2);
    }
    head = tptr2;

    return head;
    }




    also

    in the below

    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) { <--------
    MakeNewNode(addvalue); <-------------

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

    you need to return a pointer even if the first node is created

    so you should do something like
    if(list == NULL)
    return MakeNewNode*addvalue);

  3. #3
    Unregistered
    Guest
    int main(){
    node* mylist;

    // create the list and print it
    mylist = make_a_list();
    return 0;
    }

    node* make_a_list()
    {
    node head = NULL;

    return head;
    }

    The code above (copied and simplified for original post) indicates that head is a variable of type node, not a pointer to type node. This means you have a type mismatch between head and return type for make_a_list. If you make head a pointer to type node it is still a local variable to make_a_list and when you return to to main and try to assign it to mylist it might cause your program to crash as head goes out of scope at the end of make_a_list. If the memory stored in head is dynamic, then you might avoid the crash, but then you might have problems with trying to assign one list to another. I suggest using a loop and copy one list to the other a node at a time much like you do in copying arrays one element at a time.

    Also, since at least some the memory you use to make the list is dynamic (see MakeNewNode), at some point you should get in the habit of always deleting the memory you explicitly request with the new operator in an effort to prevent memory leaks. Unfortunately, I see no call to the delete operator anywhere in your code. It might work in a simple program where you aren't copying list frequently, etc. but in a more complicated program down the road it can lead to major problems, so get in the habit now.

    In addition the code as posted seems to have some errors like sections were pasted and cut and they aren't quiite in the correct sequence, although I didn't work through it to be sure of this.

    Last, but not least, if the function has return type void, do not put the keyword return; just before the last }. Leave it out completely. return should only be used when you are actually returning something.

    Finally, when writing code I find it best to try to write one function, verify it, then write another function, verify it, then another, etc.; rather than writing it all and trying to fix it all at once with all the untested stuff hanging on.

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, 04:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21