选前5 名的链表问题
#include<iostream>#include<assert.h>#include<string>usingnamespacestd;structnode{intx;node*next;node(inta){x=a;}};classlink{public:node*head;link(int*a){intn=5;head=newnode(a[0]);node*p=head;while(--n){node*add=newnode(a[n]);p->next=add;p=p->next;}}voidcomfive(link&l){intn=10;intstate=1;intt;intstate2=1;node*p=head;node*lp=l.head;while(p->next){p=p->next;}while(lp){node*add=newnode(lp->x);lp=lp->next;p->next=add;p=p->next;}//p->next=l.head;p=head;node*q=p->next;while(n--&&state){p=head;q=p->next;while(q){if(p->x<q->x){t=p->x;p->x=q->x;q->x=t;state=0;}p=p->next;q=q->next;}if(state==1)break;state=1;}n=1;p=head;q=p->next;/*****************/while(n<5&&state2){q=p->next;if(p->x==q->x);elsen++;p=p->next;//cout<<"nofive"<<endl;break;}//todealtwithtwokindsstutation,sameandnotsameif(q->next->x==q->x)q->next->next=NULL;elseq->next=NULL;/*****************/}~link(){deletehead;}voidprint(){node*p=head;while(p){cout<<p->x<<"";p=p->next;}cout<<endl;}};/*****************原来一切的代码在眼里都是可以击破的当然不同行业外都是不削一顾的就同人一样这里的预测不太好还没有完全实现比如后面还有一个没有前五名时的处理思路是程序中判断了链表中不同的数有几个如m个来控制循环并打印出前m名儿不是n名**************************/intmain(){inta1[5]={1,2,3,4,5};inta2[5]={7,7,7,9,10};inta3[5]={1,2,3,4,5};inta4[5]={7,7,7,9,10};inta5[5]={6,7,8,9,10};classlinkl1(a1);classlinkl2(a2);classlinkl3(a3);classlinkl4(a4);classlinkl5(a5);/*l1.print();l2.print();l3.print();l4.print();l5.print();*/l1.comfive(l2);//l1.print();//l2.print();l1.comfive(l3);//l1.print();//l3.print();l1.comfive(l4);//l1.print();//l5.print();l1.comfive(l5);l1.print();//l5.print();return0;}/*********************head=((head->x)>((l.head)->x))?head:l.head;node*cur=head;node*p=head->next;node*q=l.head;if(head->x==l.head->x){head->next=l.head;cur=l.head;q=l.head->next;n--;}//9753//8642while(--n){if(p->x==q->x){//n++;bug//cur->next=p;cur=cur->next;cur->next=q;cur=cur->next;}else{if(p->x>q->x){cur->next=p;p=p->next;}else{cur->next=p;q=q->next;}}//cout<<n;//3}}************************/
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。