Like Tree1Likes
  • 1 Post By MK27

add to end of linked list

This is a discussion on add to end of linked list within the C Programming forums, part of the General Programming Boards category; this is what i did to add to the end of a linked list but when i print it out ...

  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.
    Salem likes this.
    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
    21,744
    *original code restored; thread closed*
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 01:45 AM

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