#include<iostream>
using namespace std;
struct node {
int val;
node* next;
node(int v,node*nxt):val(v),next(nxt){}
};
node* head = nullptr;
node* tail = nullptr;
void printlist() {
node* p = head;
while (p != nullptr)
{
cout << p->val << " ";
p = p->next;
}
}
int main() {
int n, x;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> x;
node* p = new node(x, nullptr);
if (head == nullptr)head = p;
else tail->next = p;
tail = p;
}
printlist();
return 0;
}
动态单链表
最新推荐文章于 2024-08-31 15:48:32 发布