Thread: Insert a node into the end of a linked list

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    15

    Insert a node into the end of a linked list

    So I have been trying everything I can think of to insert a node at the end of a linked list. I dont want to give all my code since this is an assignment but I have spent hours trying to get this working. I feel so close like its only 1 or 2 lines away.


    newPtr has already been created with the values being inserted and it is pointing to NULL


    Code:
            if(test != 1) {  /*insert new node into appropriate place in list*/
                            head = *sPtr;
                previousPtr = NULL;
                check = 0;     /*this is used to check if there is at least 1 node with a value*/
                while(head != NULL) {
                    previousPtr = head;      /*move through list until the end*/
                    head = head->nextPtr;
                    check++;                    /*increase check to show at least 1 node used*/
                }
                if(check > 0){     /*if there exists a node in the list, put new node at the end*/
                head = newPtr;        /*shouldnt head be pointing to NULL after the loop so this should be adding it to the end?*/
                }
                else       /*if there are no nodes being used, put a node at the beginning*/
                {
                    newPtr->nextPtr = *sPtr;
                    *sPtr = newPtr;
                }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could search and have had your answer by now, but I'll walk you through it anyway:
    Code:
    if list is null
        set list to new node
    else
        prev = list
        curr = list->next
        while cur
            prev = cur
            cur = cur->next
        if cur
            append to cur
    Basically keep track of where you are, and where you just were. If where you are is NULL, stick the new node on the end of where you just were.


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

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    A little bit of a strange implementation, but generally you're on the right track. It is a little tough for us since we don't know what sPtr or newPtr are, but here are some notes:
    Code:
             if(test != 1) {  /*insert new node into appropriate place in list*/
                            head = *sPtr;
                previousPtr = NULL;
                check = 0;     /*this is used to check if there is at least 1 node with a value*/
                while(head != NULL) {
                    previousPtr = head;      /*move through list until the end*/
                    head = head->nextPtr;
                    check++;                    /*increase check to show at least 1 node used*/
                }
    This is generally correct, though you don't need the check variable. Usually you just check if head is NULL. If it is, you simply set head to point to the new node. If not, you run the above loop.
    Code:
                if(check > 0){     /*if there exists a node in the list, put new node at the end*/
                head = newPtr;        /*shouldnt head be pointing to NULL after the loop so this should be adding it to the end?*/
                }
    No, it wont add it to the end. head just contains an address. Before the assignment here, it contained NULL. Now it contains the same address that newPtr contains. To "link" up your list, you need to point prev->nextPtr to the new node.
    Code:
                else       /*if there are no nodes being used, put a node at the beginning*/
                {
                    newPtr->nextPtr = *sPtr;
                    *sPtr = newPtr;
                }
    }
    Not sure this is correct. You should be setting the head of the list to point to newPtr, and newPtr->nextPtr to point to NULL, since it's the only element thus far.

    EDIT: Looks like Quzah beat me

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    node_type *n, *previousPtr;
    for(n = head, previousPtr = NULL; n; previousPtr = n, n = n->next)
      ;
    if(!previousPtr)
      head = newPtr;
    else
      previousPtr->next = newPtr;
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    After spending hours I finally decided to post somewhere. Then within 10min of posting I finally figure it out what was going wrong. Thanks for the response though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-04-2010, 01:18 PM
  2. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  3. Insert node in a linked list.
    By antonis in forum C Programming
    Replies: 2
    Last Post: 10-22-2005, 02:30 PM
  4. Replies: 4
    Last Post: 09-10-2005, 01:07 PM
  5. Replies: 5
    Last Post: 10-04-2001, 03:42 PM