资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

选前5名的链表问题

 
#include
#include
#include
using namespace std;
struct node
{
int x;
node *next;
node(int a){x=a;}
};
class link
{
public :
node *head;
link(int *a)
{ 
 int n=5;
head=new node(a[0]);
node *p=head;
while(--n)
 {
  node *add=new node(a[n]);
  p->next=add;
  p=p->next;
 }

}
void comfive(link & l)
{
 int n=10;
 int state=1;
 int t;
 int state2=1;
 node *p=head;
 node *lp=l.head;
while(p->next)
 { 
     p=p->next; 
 }
while(lp)
{
node *add=new node(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);
 else n++;
 p=p->next;
   //cout<<"no five"<next->x==q->x)
q->next->next=NULL;
else q->next=NULL;
/*****************/

 }

 
 ~link()
{ 
  delete head;
}

void print()
{
 node *p=head;  
 
while(p)
{
cout<x<<" ";
p=p->next;
}cout<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--;}
// 9 7 5 3
// 8 6 4 2
 while(--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<            
            
                        
本文标题:选前5名的链表问题
链接分享:http://cdkjz.cn/article/igccsj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220