help w/linked lists

This is a discussion on help w/linked lists within the C++ Programming forums, part of the General Programming Boards category; How do you add an element to the end of a linked list? I've tried to write code and it ...

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

    Unhappy help w/linked lists

    How do you add an element to the end of a linked list? I've tried to write code and it doesn't work. Everything gets displayed except for the element that I'm suppose to add.

    #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 = new node;
    newptr->next = list;
    newptr->value = addvalue;
    list = newptr;

    }
    return;
    }

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You'll have to return your list otherwise any changes you make will be local to your function. Also, your current function walks through the list but doesn't add the node to the end of it. You could do something like this -

    Code:
    node* 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 *tempptr=list; 
    	// 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 
    	tempptr->next = new node; 
    	tempptr->next->value = addvalue; 
    	tempptr->next->next=NULL;
     	return list; 
    }
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating lists of pointers
    By The_Kingpin in forum C Programming
    Replies: 2
    Last Post: 12-11-2004, 07:10 PM
  2. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 01:49 AM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 10:32 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM

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