- 编程
324
- @ 2025-12-2 17:18:08
#include using namespace std; struct Node{ int data; Node*pre,next; }; Node c(){ Node *h=nullptr,*r=nullptr,*p=nullptr; int n; cin>>n; while(n--){ int data; cin>>data; p=new Node{data,nullptr,nullptr}; if(h==nullptr){ h=r=p; }else{ r->next=p; p->pre=r; r=p; } } return h; } void p(Node h){ Node p=h; while(p){ cout<data<<" "; p=p->next; } } void i(Node h,int pos,int data) { Node p=h; for(int i=1;i<pos;i++){ p=p->next; } Node *s=new Node{data,nullptr,nullptr}; p->pre->next=s; s->pre=p->pre; s->next=p; p->pre=p; } int main(){ Node *h=c(); i(h,2,20); p(h); return 0; }