上代码,自己模拟
#include<iostream> using namespace std; struct node { int data; node *next; } ; void search(node *,node *); void insert(node *,node *,node *); void print(node *); int main() { node *head,*p,*s; s=new node; head=new node; head->next=s; cin>>s->data; p=s; s->next=NULL; while(s->data!=0) { s=new node; cin>>s->data; search(head,s); } print(head); } void search(node *head,node *s) { node *p; bool swi; swi=false; for(p=head->next;p->next!=NULL;p=p->next) { if(s->data>=p->data&&s->data<p->next->data) { insert(p,p->next,s); swi=1; goto abc; } } if(swi==false) insert(p,s); abc: ; } void insert(node *p,node *s) { p->next=s; s->next=NULL; } void insert(node *p,node *q,node *s) { s->next=q; p->next=s; } void print(node *head) { node *p; for(p=head->next;p->next!=NULL;p=p->next) cout<<" "<<p->data; }写程序的时候纠结了一个小时,不出结果,还以为链表建错了,最后发现没输出,汗!!!! 原文链接:https://www.f2er.com/datastructure/383259.html