- 题解
求教程
- @ 2026-8-31 19:22:38
动态创建双向链表并输出数据 题目描述 动态创建双向链表并输出链表数据,以-1结束
输入 输入链表数据,以-1结束
输出 输出链表数据
样例 输入数据 1 1 2 3 4 5 -1 输出数据 1 1 2 3 4 5
6 条评论
-
-
#include <bits/stdc++.h> using namespace std; struct Node{ int data; Node *pre,*next; }; int main(){ Node *head=new Node(); head->pre=NULL; head->next=NULL; Node *tail=head; int x; while(cin>>x&&x!=-1){ Node *p=new Node(); p->data=x; p->pre=tail; p->next=NULL; tail->next=p; tail=p; } Node *p=head->next; while(p!=NULL){ cout<<p->data; if(p->next!=NULL) cout<<" "; p=p->next; } return 0; } -
#include <bits/stdc++.h> using namespace std; struct Node{ int data; Node *pre,*next; }; int main(){ Node *head=new Node(); head->pre=NULL; head->next=NULL; Node *tail=head; int x; while(cin>>x&&x!=-1){ Node *p=new Node(); p->data=x; p->pre=tail; p->next=NULL; tail->next=p; tail=p; } Node *p=head->next; while(p!=NULL){ cout<data; if(p->next!=NULL) cout<<" "; p=p->next; } return 0; }
- 1