#include<bits/stdc++.h>
using namespace std;
struct listNode {
int num;
listNode* next;
listNode(int v)
{
num = v;
next = NULL;
}
};
listNode* buildlist()
{
int n;
listNode* head = NULL;
listNode* tail = NULL;
listNode* p = NULL;
while (cin>>n) { //同时按住ctrl+z结束循环
p = new listNode(n);
if (head == NULL) head = p;
else tail->next = p;
tail = p;
}
return head;
}
void printlist(listNode* head)
{
listNode* p = head;
while (p) //当p指向的下一位没有对象时就结束
{
cout << p->num << ' ';
p = p->next;
}
}
int main()
{
listNode* p = NULL;
p=buildlist();
printlist(p);
return 0;
}
C++创建列表
最新推荐文章于 2024-07-22 12:11:04 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)