C++学习笔记 9.8 链表的创建和理解

// 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 文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值