C++ 从用户';s输入
我们被要求使用列表结构制作一个程序来列出学生信息,但我似乎找不到一种方法来输出所有信息。它只显示一个,是最后一个输入。我是做链表的新手,所以我不知道如何操作它们。任何帮助都将不胜感激!谢谢你们C++ 从用户';s输入,c++,C++,我们被要求使用列表结构制作一个程序来列出学生信息,但我似乎找不到一种方法来输出所有信息。它只显示一个,是最后一个输入。我是做链表的新手,所以我不知道如何操作它们。任何帮助都将不胜感激!谢谢你们 #include <iostream> #include <iomanip> using namespace std; int main(){ struct StudList{ string studID, studName, Course; int Yr_level; St
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
struct StudList{
string studID, studName, Course;
int Yr_level;
StudList *next;
};
StudList *head;
head = NULL;
StudList *stud;
StudList *studptr;
int studNum;
system("color F0");
cout << "Enter number of stuedents: ";
cin >> studNum;
for (int i=1; i<=studNum; i++)
{
stud = new StudList;
cout << "\nEnter Student ID : ";
cin >> stud -> studID;
cout << "Enter Student Name : ";
cin >> stud -> studName;
cout << "Course : ";
cin >> stud -> Course;
cout << "Year Level : ";
cin >> stud -> Yr_level;
stud -> next = NULL;
cout << endl;
}
cout << "\nThe students are: ";
while (stud != NULL)
{
cout << endl << endl << left << setw(10) << stud -> studID << setw(10) << stud -> studName <<
setw(8) << stud -> Course << setw(4) << stud -> Yr_level;
stud = stud -> next;
}
if (head == NULL)
{
head == stud;
}
else
{
studptr = head;
while (studptr -> next)
{
studptr = studptr -> next;
}
studptr -> next = stud;
}
}
#包括
#包括
使用名称空间std;
int main(){
结构StudList{
字符串studID、studName、Course;
国际青年大学水平;
学生名单*下一步;
};
学生名单*负责人;
head=NULL;
螺柱列表*螺柱;
StudList*studptr;
int studNum;
系统(“颜色F0”);
cout>studNum;
对于(int i=1;i stud->studID;
cout>stud->studName;
cout>stud->cours;
cout>stud->Yr_级别;
stud->next=NULL;
cout在for
循环中,您应该保存新的stud
对象,以便在下一次迭代中设置stud->next
在循环中,您只检查上次创建的stud
对象。您必须为列表中的第一个对象设置stud
,您可以使用head
对象保存第一个列表元素
我认为如果(head==NULL).
部分是不必要的
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
struct StudList{
string studID, studName, Course;
int Yr_level;
StudList *next;
};
StudList *head;
head = NULL;
StudList *stud;
int studNum;
system("color F0");
cout << "Enter number of stuedents: ";
cin >> studNum;
StudList * temp = nullptr;
for (int i=1; i<=studNum; i++)
{
stud = new StudList;
if ( i == 1 )
{head = stud;}
else
{temp->next = stud;}
cout << "\nEnter Student ID : ";
cin >> stud -> studID;
cout << "Enter Student Name : ";
cin >> stud -> studName;
cout << "Course : ";
cin >> stud -> Course;
cout << "Year Level : ";
cin >> stud -> Yr_level;
stud -> next = NULL;
temp = stud;
cout << endl;
}
stud = head;
cout << "\nThe students are: ";
while (stud != NULL)
{
cout << endl << endl << left << setw(10) << stud -> studID << setw(10) << stud -> studName <<
setw(8) << stud -> Course << setw(4) << stud -> Yr_level;
stud = stud -> next;
}
}
#包括
#包括
使用名称空间std;
int main(){
结构StudList{
字符串studID、studName、Course;
国际青年大学水平;
学生名单*下一步;
};
学生名单*负责人;
head=NULL;
螺柱列表*螺柱;
int studNum;
系统(“颜色F0”);
cout>studNum;
StudList*temp=nullptr;
对于(int i=1;inxt=stud;}
cout>stud->studID;
cout>stud->studName;
cout>stud->cours;
cout>stud->Yr_级别;
stud->next=NULL;
温度=螺柱;
cout在for
循环中,您应该保存新的stud
对象,以便在下一次迭代中设置stud->next
在循环中,您只检查上次创建的stud
对象。您必须为列表中的第一个对象设置stud
,您可以使用head
对象保存第一个列表元素
我认为如果(head==NULL).
部分是不必要的
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
struct StudList{
string studID, studName, Course;
int Yr_level;
StudList *next;
};
StudList *head;
head = NULL;
StudList *stud;
int studNum;
system("color F0");
cout << "Enter number of stuedents: ";
cin >> studNum;
StudList * temp = nullptr;
for (int i=1; i<=studNum; i++)
{
stud = new StudList;
if ( i == 1 )
{head = stud;}
else
{temp->next = stud;}
cout << "\nEnter Student ID : ";
cin >> stud -> studID;
cout << "Enter Student Name : ";
cin >> stud -> studName;
cout << "Course : ";
cin >> stud -> Course;
cout << "Year Level : ";
cin >> stud -> Yr_level;
stud -> next = NULL;
temp = stud;
cout << endl;
}
stud = head;
cout << "\nThe students are: ";
while (stud != NULL)
{
cout << endl << endl << left << setw(10) << stud -> studID << setw(10) << stud -> studName <<
setw(8) << stud -> Course << setw(4) << stud -> Yr_level;
stud = stud -> next;
}
}
#包括
#包括
使用名称空间std;
int main(){
结构StudList{
字符串studID、studName、Course;
国际青年大学水平;
学生名单*下一步;
};
学生名单*负责人;
head=NULL;
螺柱列表*螺柱;
int studNum;
系统(“颜色F0”);
cout>studNum;
StudList*temp=nullptr;
对于(int i=1;inxt=stud;}
cout>stud->studID;
cout>stud->studName;
cout>stud->cours;
cout>stud->Yr_级别;
stud->next=NULL;
温度=螺柱;
在输入学生信息的for
循环中,不能将新创建的stud
连接到列表本身(您应该保留列表的尾部,并将stud
指针保存在tail->next
字段中的for
循环中,在该循环中输入学生信息,您不会将新创建的stud
连接到列表本身(您应该保留列表的尾部,并将stud
指针保存在tail->next
字段。我非常感谢!我下次会注意到这一点!我非常感谢!我下次会注意到这一点!