- #include <iostream>
- #include <string>
- using std::cin;
- using std::cout;
- using std::endl;
- using std::string;
- struct Student
- {
- int id;
- double score;
- string name;
- Student *next;
- };
- Student *CreatStuList(const int num)
- {
- int t = 0; //学生个数计数器
- Student *node = new Student; //创建新节点
- Student *head = node; //保存首节点
- cout << "请输入学生编号/名字/得分" << endl;
- while (num > 0)
- {
- cin >> node->id >> node->name >> node->score; //输入数据
- t++;
- if (t == num) //结束条件
- {
- node->next = nullptr; //尾结点置空
- break;
- }
- node->next = new Student; //创建新节点
- node = node->next; //移动指针指向新节点
- }
- return head;
- }
- void printlist(Student *head)
- {
- while (head != nullptr)
- {
- cout << "\n编号" << head->id << "\n姓名" << head->name << "\n得分" << head->score << "\n"
- << endl;
- head = head->next;
- }
- }
- int main(int argc, char const *argv[])
- {
- int a = 4; //学生个数
- Student *head = CreatStuList(a); //创建链表
- printlist(head); //输出链表
- return 0;
- }
复制代码
---------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.18363.592]
(c) 2019 Microsoft Corporation。保留所有权利。
D:\My data\Documents\C++> cmd /C "c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.3\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-afybbiiy.s3m --stdout=Microsoft-MIEngine-Out-lvthf1zh.xnt --stderr=Microsoft-MIEngine-Error-mh3gydww.2kf --pid=Microsoft-MIEngine-Pid-iwn3yrwz.icl --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi "
请输入学生编号/名字/得分
1 a 1
2 d 3
3 d 0
4 f 0
编号1
姓名a
得分1
编号2
姓名d
得分3
编号3
姓名d
得分0
编号4
姓名f
得分0
D:\My data\Documents\C++>