您的位置:首页 > 编程语言

选前5 名的链表问题

2016-04-21 18:55 302 查看
#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
);
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
}

}

************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  小代码