有两个数组a[4]={3,5,8,11};和 b[7]={2,6,8,9,11,15,20};分别把它们放入两个动态链表中,然后按由小到大的顺序把它们放到第三个空链表中。
#include
"
stdafx.h
"
#include < stdio.h >
typedef struct L ... {
int num;
struct L *next;
} L, * LinkList;
/**/ /** 此函数的功能是建立链表,返回值为头指针。*/
void CreatList (LinkList & m, int * pa, int n)
... {
m = (LinkList)malloc(sizeof(L));
m->next = NULL;
for(int i = 0; i < n; i++)
...{
LinkList s = (LinkList)malloc(sizeof(L));
s->num = *(pa + i);
s->next = m->next;
m->next = s;
}
}
void Insert(LinkList & x, LinkList & y, LinkList & z)
... {
z = (LinkList)malloc(sizeof(L));
z->next = NULL;
while(y->next)
...{//这部分不具有通用性,完全是看到你的数组已经排好了序,才这样偷工减料了。
//你自己可以改进,使其具有通用性。
LinkList p = (LinkList)malloc(sizeof(L));
p->num = y->next->num;
p->next = z->next;
z->next = p;
y = y->next;
}
while(x->next)
...{
LinkList ppre = z;
LinkList q = (LinkList)malloc(sizeof(L));
q->num = x->next->num;
while(q->num >= ppre->next->num)
ppre = ppre->next;
q->next = ppre->next;
ppre->next = q;
x = x->next;
}
}
void main()
... {
int a[4] = ...{3, 5, 8, 11};
int b[7] = ...{2, 6, 8, 9, 11, 15, 20};
LinkList LA = NULL, LB = NULL, LC = NULL;
CreatList (LA, a, 4);
CreatList (LB, b, 7);
Insert(LA, LB, LC);
for(LinkList i = LC->next; i != NULL; i = i->next)
printf("%d ", i->num);
}
#include < stdio.h >
typedef struct L ... {
int num;
struct L *next;
} L, * LinkList;
/**/ /** 此函数的功能是建立链表,返回值为头指针。*/
void CreatList (LinkList & m, int * pa, int n)
... {
m = (LinkList)malloc(sizeof(L));
m->next = NULL;
for(int i = 0; i < n; i++)
...{
LinkList s = (LinkList)malloc(sizeof(L));
s->num = *(pa + i);
s->next = m->next;
m->next = s;
}
}
void Insert(LinkList & x, LinkList & y, LinkList & z)
... {
z = (LinkList)malloc(sizeof(L));
z->next = NULL;
while(y->next)
...{//这部分不具有通用性,完全是看到你的数组已经排好了序,才这样偷工减料了。
//你自己可以改进,使其具有通用性。
LinkList p = (LinkList)malloc(sizeof(L));
p->num = y->next->num;
p->next = z->next;
z->next = p;
y = y->next;
}
while(x->next)
...{
LinkList ppre = z;
LinkList q = (LinkList)malloc(sizeof(L));
q->num = x->next->num;
while(q->num >= ppre->next->num)
ppre = ppre->next;
q->next = ppre->next;
ppre->next = q;
x = x->next;
}
}
void main()
... {
int a[4] = ...{3, 5, 8, 11};
int b[7] = ...{2, 6, 8, 9, 11, 15, 20};
LinkList LA = NULL, LB = NULL, LC = NULL;
CreatList (LA, a, 4);
CreatList (LB, b, 7);
Insert(LA, LB, LC);
for(LinkList i = LC->next; i != NULL; i = i->next)
printf("%d ", i->num);
}