代码:
#include <iostream> #include <string> #include <cmath> #include <algorithm> using namespace std; struct Node { int v; Node *next; }; Node* create_lian(int n) { Node *a=new Node,*b; b=a; cin>>a->v; a->next=NULL; for(int i=2;i<=n;i++) { a->next=new Node; a=a->next; cin>>a->v; a->next=NULL; } cout<<"Node created."<<endl; return b; } void out_lian(Node *p) { do { cout<<p->v<<endl; p=p->next; } while(p!=NULL); } main() { int n,x,y; Node *a,*b=new Node,*pre,*head; cin>>n; head=create_lian(n); cin>>x>>y;//在x前面插入y b->v=y; b->next=NULL; a=head; pre=b; while(a!=NULL) { if(a->v==x) { pre->next=b; b->next=a; } pre=a; a=a->next; } out_lian(head); }