#include <bits/stdc++.h> // 引入所有标准库头文件 using namespace std; // 使用标准命名空间

/* 赵,钱,孙,李 */

// 定义一个结构体 people struct people { string name; // 字符串类型的 name 成员变量 struct people *next; // 指向结构体 people 的指针类型的 next 成员变量 };

#include // 引入输入输出流头文件 using namespace std; // 使用标准命名空间

// 创建一个链表并返回链表头指针 struct people* createLink() { // 分别创建 5 个 people 类型的指针变量,命名为 p0、p1、p2、p3、p4 people* p0=new people; people* p1=new people; people* p2=new people; people* p3=new people; people* p4=new people;

// 为每个指针变量的 name 成员赋值
p1->name="赵";
p2->name="钱";
p3->name="孙";
p4->name="李";

// 将指针变量按顺序连接起来,形成链表
p0->next=p1;
p1->next=p2;
p2->next=p3;
p3->next=p4;
p4->next=NULL;

// 将链表头指针指向 p0,并返回
people *p=p0;
return p;

}

int main() { // 创建链表头 struct people *head=createLink(); // 从链表头开始遍历链表,输出每个结点的 name 值 struct people *p=head->next; while(p!=NULL) { cout<name<<"->"; p=p->next; } cout<<"NULL"<<endl; return 0;

#include bitsstdc++husing namespace std;赵钱孙李struct people string name; struct people next; ;#includeiostreamusing namespace std;struct people createLink people p0=new people; people p1=new people; peo

原文地址: https://www.cveoy.top/t/topic/cD56 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录