温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

选前5 名的链表问题

发布时间:2020-06-11 13:03:02 来源:网络 阅读:176 作者:wzdouban 栏目:编程语言
 
#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;
}
};
/*****************

原来 一切的代码 在眼里都是可以击破的  当然不同行业外  都是不削一顾的  就同人一样
这里的预测  不太好  还没有完全实现 比如 后面还有一个没有前 五名时的处理
思路是程序中判断了链表中不同的数有几个 如m个来控制循环 并打印出前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问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI