C++ 显示链接列表

C++ 显示链接列表,c++,linked-list,C++,Linked List,我在显示链接列表中的数据时遇到问题。我尝试在for循环和out循环中包含display循环,只是为了检查指针和数据是否有问题,但我得到了相同的结果 它显示第一个数据,但随后开始显示胡言乱语 #include <stdio.h> #include <conio.h> #include <iostream> #include <string> void main(void) { clrscr(); struct Student {

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

它显示第一个数据,但随后开始显示胡言乱语

#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();
}
#包括
#包括
#包括
#包括
真空总管(真空){
clrsc();
结构学生{
字符串名;
双GPA;
学生*下一位;
};
学生*校长;
head=NULL;
int ch,i;
字符串名;
双gpa;
cout>ch;
cout-gpa;
cout Name=名称;
newstudent->GPA=GPA;
newstudent->next=NULL;
如果(!头)
head=新闻学生;
否则{
studentptr=头部;
while(studentptr->next){
studentptr=studentptr->next;
}
studentptr->next=新学生;
}
}
clrsc();
学生*显示器;
显示器=头部;
while(显示){

cout
studentptr->next=newstudent;
应该是
studentptr->next=newstudent;
,因为问题是关于正确方向的建议:

  • 在尝试读取任何您想读取的内容后,您需要始终检查输入是否成功,例如,
    if(std::cin>>value){…}
  • 您正在循环中创建一个多余的
    Student
    对象
  • 您没有将创建的
    Student
    对象与列表连接起来。您可能打算在创建新对象时执行此操作

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

    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(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();
    }
    
    int main(无效){
    clrsc();
    学生名单;
    int ch,i;
    字符串名;
    双gpa;
    cout>ch;
    cout-gpa;
    
    请将
    void main
    更改为
    int main
    两条注释:(1)学习初始化声明上的指针,从而将其不确定的时间段变为接近零;(c)经常检查您的流操作是否成功。现在我正式感觉自己像一个迟钝的、严肃的thanx伙伴。我的头没有放在正确的位置,我认真地检查了代码一百次,然后才把它发布到这里。@majory碰巧发生在我们所有人身上-有时这只需要一双新的眼睛!@Rollie thanx表示乐观我想这是需要的:我实际上是在找回我的概念,所以我只是像菜鸟一样写代码,暂时不关心后果。:)有趣的是,这表明我忘记了多少。Thanx的建议,肯定会让我更好。
    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();
    }