溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

選前5 名的鏈表問(wèn)題

發(fā)布時(shí)間:2020-06-11 13:03:02 來(lái)源:網(wǎng)絡(luò) 閱讀:178 作者:wzdouban 欄目:編程語(yǔ)言
 
#include<iostream>
#include<assert.h>
#include<string>
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"<<endl;break;
} 

//to dealt with two kinds stutation ,same and not same
 if(q->next->x==q->x)
q->next->next=NULL;
else q->next=NULL;
/*****************/

 }

 
 ~link()
{ 
  delete head;
}

void print()
{
 node *p=head;  
 
while(p)
{
cout<<p->x<<" ";
p=p->next;
}cout<<endl;
}
};
/*****************

原來(lái) 一切的代碼 在眼里都是可以擊破的  當(dāng)然不同行業(yè)外  都是不削一顧的  就同人一樣
這里的預(yù)測(cè)  不太好  還沒(méi)有完全實(shí)現(xiàn) 比如 后面還有一個(gè)沒(méi)有前 五名時(shí)的處理
思路是程序中判斷了鏈表中不同的數(shù)有幾個(gè) 如m個(gè)來(lái)控制循環(huán) 并打印出前m名兒不是n名
**************************/
int main()
{
int a1[5]={1,2,3,4,5};
int a2[5]={7,7,7,9,10};
int a3[5]={1,2,3,4,5};
int a4[5]={7,7,7,9,10};
int a5[5]={6,7,8,9,10};
class link l1(a1);
class link l2(a2);
class link l3(a3);
class link l4(a4);
class link l5(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();
return 0;
}
/*********************
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--;}
// 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<<n;//3
  }

}

************************/


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI