Thread: help!!!!!!!

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

    Unhappy help!!!!!!!

    I'm trying to print out all the elements in the linked list and I keep getting the same number printed out an infinite number of times. I don't know what condition I should put in the while statement to print out the numbers correctly.


    #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->value != NULL) {


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


    /* TAKE OUT COMMENTS MARKS BEFORE COMPILING

    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




    // return pointer that points to front of list



    }

    */



    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





    // create a new node with appropriate values
    // and add it to the list







    return;
    }

  2. #2
    Unregistered
    Guest
    change this:

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


    // print each element
    cout << ptr->value;
    list = ptr

    to this:

    while(ptr != NULL)
    cout ptr->value;
    ptr = ptr->next;

Popular pages Recent additions subscribe to a feed