Thread: add to end of linked list

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    20

    add to end of linked list

    this is what i did to add to the end of a linked list but when i print it out it doesn't come out right. this is my code:

    Code:
    /*functions allows user to add members to the end of list*/
        struct member_account *add_to_end(struct member_account *list,char *last, char *first, double balance, int count3) {
        struct member_account *current;
        struct member_account *new_list=NULL;
        new_list = (struct member_account *)malloc(sizeof(struct member_account));
    
        if(list == NULL) {
        list = (struct member_account *)malloc(sizeof(struct member_account));
        strcpy(list->member_last,last);
        strcpy(list->member_first,first);
        list->member_balance=balance;
        list->next = NULL;
        return(list);
        }
        else {
        current = (struct member_account *)malloc(sizeof(struct member_account));
        strcpy(current->member_last,last);
        strcpy(current->member_first,first);
        current->member_balance=balance;
        current->next=list;
        }
        while(list!=NULL){
        current=list;
        list=list->next;
        current->next = new_list;
        new_list= current;
    
        }
        return(new_list);
        count3++;
        } /* end add_to_end*/
    can any one explain to me how would i go about adding to the end of the list. THANKS
    Last edited by laserlight; 10-02-2011 at 08:54 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        new_list = (struct member_account *)malloc(sizeof(struct member_account));
         
        if(list == NULL) {
            list = (struct member_account *)malloc(sizeof(struct member_account));
    If you don't have a list to start with, you allocate a node. But you just allocated a node for new_list. So you really shouldn't have 2 nodes now, you should only have one.

    To find the end of a list, you should do:
    Code:
    for( n = firstnode; n && n->next; n = n->next )
        ; /* do nothing, the loop is doing it for you */
    if( n == NULL )
        /* the list was empty in the first place */
    else
        /* n is pointing at the last node, n->next is NULL */

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    20
    thanks for you replay. can you explain to me what you did with the for loop. you have set the n to a pointer which is firstnode, and then i am not sure what you have done after that.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    20
    how would you delete a post on this websit

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by johnhuge View Post
    how would you delete a post on this websit
    If you ever expect anyone to help you here again, DO NOT DELETE YOUR INITIAL POST. There is no good reason to do so, and if you are doing it for a bad reason, you should not be posting anything to begin with.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    *original code restored; thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Linked List inside Linked List
    By de4th in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2006, 11:17 AM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM