2

我在显示链接列表中的数据时遇到问题。我曾尝试在我的 for 循环中包含显示循环,并且只是为了检查它是否是指针和数据的问题,但我得到了相同的结果。

它显示第一个数据,但随后开始显示乱码。

#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>

void main(void) {
    clrscr();
    struct Student {
        string Name;
        double GPA;
        Student *next;
    };

    Student *head;
    head = NULL;

    int ch, i;
    string name;
    double gpa;

    cout << "How Many Records Do You Want To Enter?";
    cin >> ch;
    cout << endl;

    for (i = 0; i < ch; i++) {
        cout << (i + 1) << ". Name Of Student:";
        cin >> name;
        cout << "GPA Of Student:";
        cin >> gpa;
        cout << endl;

        Student *newstudent;
        Student *studentptr;

        newstudent = new Student;
        newstudent->Name = name;
        newstudent->GPA = gpa;
        newstudent->next = NULL;

        if (!head)
            head = newstudent;
        else {
            studentptr = head;

            while (studentptr->next) {
                studentptr = studentptr->next;
            }
            studentptr->next = new Student;
        }
    }

    clrscr();
    Student *display;
    display = head;

    while (display) {
        cout << "Name:" << display->Name << endl;
        cout << "GPA:" << display->GPA << endl;

        display = display->next;
    }
    getch();
}

对正确方向的任何建议和指示?

显然我正在关注某人的教程,但发生了这个错误。

4

3 回答 3

2

studentptr->next = new Student;应该studentptr->next = newstudent;

于 2012-11-20T19:43:41.530 回答
2

由于问题是关于正确方向的建议:

  1. 在您尝试阅读任何您想阅读的内容后,您需要始终if (std::cin >> value) { ... }检查您的输入是否成功,例如.
  2. 不要使用std::endl.
  3. 您正在循环中创建多余的Student对象。
  4. 您没有将创建的Student对象与列表挂钩。您可能打算在创建新对象的地方执行此操作。
于 2012-11-20T19:47:00.473 回答
1

我有一些建议可能会有所帮助:

struct Student {
    string Name;
    double GPA;
    Student *next;
    Student(const string& name, double GPA) : Name(name), GPA(GPA), next(NULL) {}
    void print() {
        cout << "Name:" << Name << endl;
        cout << "GPA:" << GPA << endl;
    }
};

现在而不是:

    newstudent = new Student;
    newstudent->Name = name;
    newstudent->GPA = gpa;
    newstudent->next = NULL;

你只需写:

newstudent = new Student(name, gpa);

为列表创建一个结构:

struct StudentList {
    Student* head;
    Student* current;

    StudentList() :head(NULL), current(NULL) {}

    ~StudentList() {/*Delete list here*/}

    void insert(string name, double gpa) {
        if(!head) {
            head = new Student(name, gpa);
            current = head;
        } else {
            current->next = new Student(name, gpa);
            current = current->next;
        }
    }

    void display() {
        Student *display = head;
        while (display) {
            display->print();
            display = display->next;
        }
    }
};

有了这一切,你的主要现在应该是:

int main(void) {
    clrscr();

    StudentList list;

    int ch, i;
    string name;
    double gpa;

    cout << "How Many Records Do You Want To Enter?";
    cin >> ch;
    cout << endl;

    for (i = 0; i < ch; i++) {
        cout << (i + 1) << ". Name Of Student:";
        cin >> name;
        cout << "GPA Of Student:";
        cin >> gpa;
        cout << endl;

        list.insert(name, gpa);
    }

    clrscr();
    list.display();
    getch();
}
于 2012-11-20T19:47:05.673 回答