C++:单向链表

C++:单向链表,第1张

C++:单向链表

题目概述:
单向链表。
编程:
#include< iostream>
using namespace std;
typedef struct node
{
char name[20];
int age;
struct node* next;
}Student;
Student* createList(int n)
{
Student* head = new Student;//头节点
Student* pre = head;
for (int i = 0; i < n; i++)
{
Student* p = new Student;
cout << “输入学生姓名和年龄” << endl;
cin >> p->name;
cin >> p->age;
pre->next = p;
pre = p;
p->next = NULL;
}
return head;
}
void display(Student* head)
{
Student* p = head->next;
while (p != NULL)
{
cout << p->name << " ";
cout << p->age << endl;
p = p->next;
}
}
int main()
{
int n;
cout << “输入n个人” << endl;
cin >> n;
Student* head = createList(n);
display(head);
return 0;
}
上机实践:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5634785.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存