显示链接列表

时间:2012-11-20 19:39:25

标签: c++ linked-list

我在链接列表中显示数据时遇到问题。我试图在我的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();
}

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

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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

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

  1. 在您尝试阅读想要阅读的内容后,您需要始终检查您的输入是否成功,例如if (std::cin >> value) { ... }
  2. Do not use std::endl
  3. 您正在循环中创建一个多余的Student对象。
  4. 您没有与列表挂钩创建的Student对象。您可能打算在创建新对象的地方执行此操作。

答案 2 :(得分: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();
}