Linked list terminates

This is a discussion on Linked list terminates within the C++ Programming forums, part of the General Programming Boards category; I'm trying to construct a linked list, with nodes containing strings as oppose to integers. Here's what I have so ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    Linked list terminates

    I'm trying to construct a linked list, with nodes containing strings as oppose to integers. Here's what I have so far:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class family {
        private:
            struct person {
                char name[20];
                person *next;
            }*head;
        public:
            family();
            ~family();
            void addname(char c[20]);
            int count();
    };
    
    family::family() {
        head = NULL;
    }
    
    family::~family() {
        person *temp;
        if (head == NULL) return;
        
        while (head != NULL) {
            temp = head->next;
            delete head;
            head = temp;
        }
    }
    
    void family::addname(char c[20]) {
        person *p, *q;
        if (head == NULL) {
            head = new person;
            head->name[20] = c[20];
            head->next = NULL;
        }
        else {
            q = head;
            while (q->next != NULL) {
                q = q->next;
            }
            p = new person;
            p->name[20] = c[20];
            p->next = NULL;
            q->next = p;
        }
    }
    
    int family::count() {
        int counter = 0;
        person *q;
        q = head;
        while (q->next != NULL) {
            counter++;
            q = q->next;
        }
        return counter;
    }
    
    int main()
    {
        char familyname[20];
        cout<<"Enter new family: ";
        cin>>familyname;
        
        family familia;
        
        cout<<"The "<<familyname<<" family with "<<familia.count()<<" is created."<<endl;
        return 0;
    }
    The program runs, but it terminates abnormally right after when I enter the family name. After some basic debugging, I noticed that the problem is apparently from the count() function, but I can't see what's wrong.

    Any help is appreciated.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Here is what you do
    Code:
    head = NULL; //in constructor
    
    p = head; //in count
    
    while(p->next) //crash
    in the count - check starting from the current node, not the next node.

    PS. You have also issues whith copying the strings in your code, you'll see them when you'll start adding strings... So may be its time to switch to string class
    Last edited by vart; 11-01-2006 at 02:10 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    you're right, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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