动态创建双向链表并输出数据 题目描述 动态创建双向链表并输出链表数据,以-1结束

输入 输入链表数据,以-1结束

输出 输出链表数据

样例 输入数据 1 1 2 3 4 5 -1 输出数据 1 1 2 3 4 5

6 条评论

  • @ 2026-8-31 20:58:52

    没事

    • @ 2026-8-31 20:29:11

      你现在还记着呢!😒😒

      • 谢谢你给我的代码

        • 还我玫瑰金

          • @ 2026-8-31 20:04:52
            #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;
            }
            
            • @ 2026-8-31 20:04:43

              #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