我遇到了顯示鏈接列表中數據的問題。我試圖在我的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();
}
任何建議和指向正確的方向?
顯然我在跟着別人的教程,但是發生了這個錯誤。
請更改'無效main'爲'INT main' –
兩點意見:(1)學習來初始化聲明三分球,從而把他們的時間不定時期接近零; (c)**始終**檢查您的流操作是否成功。 – WhozCraig