// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include<string>
#include "sort.h"
using namespace std;
struct Student {
int val; // 存储数据的变量
string name;
Student* next; // 指向下一个节点的指针
};
Student* creatlist(Student* currentcode)
{
Student* newcode = new Student;
currentcode->next = newcode; //将当前节点的next指向new
cout << "请输入名字"<<"\n";
cin >> newcode->name;
cout << "成绩" << "\n";
cin >> newcode->val;
newcode->next = NULL;
currentcode = newcode;
return currentcode;
}
void printflist(Student* head)
{
cout << head->name << "\n";
cout << head->val << "\n";
Student* currentcode = head->next;
Student* nextcode = currentcode->next;
while (currentcode != NULL)
{
cout << currentcode->name << "\n";
cout << currentcode->val << "\n";
currentcode = nextcode; //移动节点到下一个节点
nextcode = currentcode->next;
}
}
int main()
{
///*int a[10];
//int max, index_max;
//for (int i = 0; i < 10; i++)
// cin >> a[i];
//max = find_max_or_min(a, &index_max, 10, 1);
//sort_max(a,10);
//for (int i = 0; i < 10; i++)
//{
// cout << a[i]<<"\n";
//}
//std::cout << "Hello World!\n";
//cout << max; */
string a;
char b;
a = "人民";
if (a=="人")
cout << a;
Student* head = new Student();
Student* currentcode;
currentcode = head; //保存头节点
head->val = 1;
head->name = "Alice";
head->next = NULL;
cout << "是否输入学习成绩和姓名 Y/N"<<"\n";
cin >> b;
while (b != 'n')
{
currentcode = creatlist(currentcode);
cout << "是否输入学习成绩和姓名 Y/N" << "\n";
cin >> b;
}
printflist(head);
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
C++学习笔记 9.8 链表的创建和理解
最新推荐文章于 2024-10-11 23:53:00 发布