当显示具有多个节点的List时,C ++链接列表崩溃

时间:2017-11-01 07:43:12

标签: c++ linked-list

我的链表计划遇到问题。程序要求用户提供不同类型的数据。当我尝试显示仅包含一个节点的列表时,它工作正常。但是当我添加另一个节点时,程序就会崩溃。

我用一个int值尝试了程序,它似乎完美无缺。我不知道为什么当一个节点中有5个值时它没有。

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

class List
{
private:
    typedef struct node
    {
        int data, year;
        char gender;
        string name, degree;
        node* next;
    }*nodeptr;

    nodeptr head;
    nodeptr curr;
    nodeptr temp;

public:
    List();
    void AddNode(int adddata, string addname, char addgender, string adddegree, int addyear);
    void DeleteNode(int deldata);
    void SearchNode(int searchdata);
    void PrintList();
};

List::List()
{
    head = NULL;
    curr = NULL;
    temp = NULL;
}

void List::AddNode(int adddata, string addname, char addgender, string adddegree, int addyear)
{
    nodeptr n = new node;
    n->next = NULL;
    n->data = adddata;
    n->name = addname;
    n->gender = addgender;
    n->degree = adddegree;
    n->year = addyear;

    if (head != NULL)
    {
        curr = head;
        while (curr->data != NULL)
        {
        curr = curr->next;
        }
    curr->next = n;
    cout << "Student " << n->data << " was added.\n\n";
    }
    else
    {
        head = n;
        cout << "Student " << n->data << " was added.\n\n";
    }
}

void List::DeleteNode(int deldata)
{
    nodeptr delptr = NULL;
    temp = head;
    curr = head;

    while (curr != NULL && curr->data != deldata)
    {
        temp = curr;
        curr = curr->next;
    }

    if (curr == NULL)
    {
        cout << "Student " << deldata << " was not in the list\n";
        delete delptr;
    }
    else
{
        delptr = curr;
        curr = curr->next;
        temp->next = curr;
        if (delptr == head)
        {
            head = head->next;
            temp = NULL;
        }
        delete delptr;
        cout << "Student " << deldata << "was deleted.\n";
    }
}

void List::SearchNode(int searchdata)
{
    nodeptr searchptr = NULL;
    temp = head;
    curr = head;

    while (curr != NULL && curr->data != searchdata)
    {
        temp = curr;
        curr = curr->next;
    }

    if (curr == NULL)
    {
        cout << "Student " << searchdata << " was not in the list\n";
        delete searchptr;
    }
    else
    {
        cout << "Student name: " << curr->name << "\nStudent Number: " << curr->data << "\nGender: " << curr->gender << "\nDegree: " << curr->degree << "\nYear of Graduation: " << curr->year << endl << endl;
}
}
void List::PrintList()
{
    curr = head;
    while (curr != NULL)
    {
        cout << "Student name: " << curr->name << "\nStudent Number: " <<     curr->data << "\nGender: " << curr->gender << "\nDegree: " << curr->degree << "\nYear of Graduation: " << curr->year << endl << endl << endl;
    curr = curr->next;
    }
}

void main()
{
    List List;
    int c = 0, d;
    int data, year;
    char gender;
    string name, degree;

    do
    {
        do
        {
            cout << "[1] Add Student\n[2] Delete Student\n[3] Search     Student\n[4] Print Students\n[5] Exit\n";
            cin >> c;
        } while (c < 1 || c > 4);

        if (c == 1)
        {
            system("cls");
            cout << "Enter Student Number: ";
            cin >> data;
            cin.ignore();
            cout << "Enter Student's Name: ";
            getline(cin, name); 
            cout << "Enter Student's Gender: ";
            cin >> gender;
            cin.ignore();
            cout << "Enter Student's Degree: ";
            getline(cin, degree);
            cout << "Enter Student's Year of Graduation: ";
            cin >> year;
            system("cls");
            List.AddNode(data, name, gender, degree, year);
        }
        if (c == 2)
        {
            system("cls");
            cout << "Enter entry you want to delete: ";
            cin >> d;
            system("cls");
            List.DeleteNode(d);
        }
        if (c == 3)
        {
            system("cls");
            cout << "Enter Student Number: ";
            cin >> data;
            List.SearchNode(data);
        }
        if (c == 4)
        {
            system("cls");
        List.PrintList();
        }
        if (c == 5)
        {
            exit(0);
        }
    } while (c != 5);
}

1 个答案:

答案 0 :(得分:0)

List::AddNode()中,您要按curr->data != NULL而不是curr->next != NULL检查最后一个节点。最后一个节点的data将不是null,您将分配NULLcurr,然后会尝试访问NULL->data

if (head != NULL)
{
    curr = head;
    while (curr->data != NULL) // should be curr->next
    {
        curr = curr->next;
    }
    // ...
}