Thread: linked list - crashes after inserting node to end of list

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132

    linked list - crashes after inserting node to end of list

    The program crashes at
    Code:
    (*temp).next = NULL;
    Code:
    #include <cstdlib>
    #include <iostream>
    //#include <cstring>
    #include <string.h>
    #include "..\include\LinkedList.h"
    
    LinkedList::LinkedList() : length(0), startPointer(NULL)
    {
    
    }
    
    LinkedList::~LinkedList()
    {
    
    }
    
    bool LinkedList::createNode(char name[])
    {
        if(startPointer == NULL)
        {
            startPointer = new Node;
            strcpy((*startPointer).name, name);//can't just assign with = because can't do arr1[]=arr2[]
            std::cout << "Your name is: " << (*startPointer).name <<  std::endl;
        }
        else
        {
            Node *temp = startPointer;
            while((*temp).next != NULL)
            {
                temp = (*temp).next;
                /*This isn't working*/
                (*temp).next = NULL;
            }
            (*temp).next = new Node;
            strcpy((*temp).name, name);
            std::cout << "Your last name is: " << (*startPointer).name <<std::endl;
        }
        return true;
    }
    Just for future referance, is it helpful if I leave in all the cout statment I use for debuging?
    Last edited by c_weed; 11-02-2010 at 11:42 AM. Reason: tidied up code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM