实现
代码
#include
#include
using namespace std;
typedef struct Node
{
int data;
struct Node * next;
}Node , * linkList;
void Travel(linkList H)//遍历
{
if(H -> next)
{
linkList p;
p = H -> next;
while(p != NULL)
{
cout << p -> data;
if(p -> next != NULL)
cout << " ";
p = p -> next;
}
}
}
linkList Creat_L()//头插法
{
linkList H , p;
int x;
H = (Node * ) malloc (sizeof(Node));
H -> next = NULL;
while(cin >> x && x != -1)
{
linkList p;
p = (Node *) malloc (sizeof(Node));
p -> data = x;
p -> next = H -> next;
H -> next = p;
}
return H;
}
linkList Com_L(linkList A , linkList B)
{
linkList p = A;
while(p -> next != NULL)
p = p -> next;
p -> next = B -> next;
return A;
}
void Lsort(linkList &H)
{
linkList p , q;
if(H -> next -> next != NULL)
{
for(p = H -> next ; p -> next != NULL ; p = p -> next)
for(q = p -> next ; q != NULL ; q = q -> next)
{
if(q -> data < p -> data)
{
int t;
t = q -> data;
q -> data = p -> data;
p -> data = t;
}
}
}
}
int main()
{
linkList A , B , C;
A = Creat_L();
B = Creat_L();
C = Com_L(A ,B);
Lsort(C);
Travel(C);
return 0;
}