合并:每个链表中都有一个头指针head和length,两个链表分别建立前,将head赋给temp,让temp来完成连接操作。注意连接时第一个的temp和第二个的head要一个一个地来连接。
最后通过第一个的head指针来实线现合链表的输出。
逆序:用选择法,不管比较结果是大于还是小于,指针都要前进。
#include<iostream>
#include<algorithm>
using namespace std;
class list
{
public:
list(){}
struct node
{
int figure;
node(int a):figure(a){}
node*next;
};
node*head=new node(0);
node*temp=head;
node*pointer=head;
node*pointer1=head;
int length=0;
void addAttail(int b)
{
temp->next=new node(b);
temp=temp->next;
length++;
}
void display()
{
node*find=head;
for(int i=0;i<this->length;i++)
{
cout<<find->next->figure<<" ";
find=find->next;
}
}
void link(list&b)
{
for(int i=0;i<b.length;i++)
{
temp->next=b.head->next;
temp=temp->next;
b.head=b.head->next;
}
}
void nixusort()
{
for(int i=0;i<12;i++)
{
for(int j=i+1;j<12;j++)
{if(pointer1->next->figure<pointer->next->next->figure)
{
swap(pointer1->next->figure,pointer->next->next->figure);
}
pointer=pointer->next;
}
pointer1=pointer1->next;
pointer=pointer1;
}
};
void display2()//输出函数
{
for(int i=0;i<12;i++)
{
cout<<head->next->figure<<" ";
head=head->next;
}
}
};
int main()
{
list a,b;
a.addAttail(1);
a.addAttail(3);
a.addAttail(5);
a.addAttail(19);
a.addAttail(4);
a.addAttail(8);
a.display();cout<<endl;
b.addAttail(2);
b.addAttail(5);
b.addAttail(10);
b.addAttail(2);
b.addAttail(7);
b.addAttail(3);
b.display();cout<<endl;
a.link(b);
a.nixusort();
a.display2();
}