请教一道题。这题是建立两个链表,再把它们连接起来,就是在第一条链表的结尾连接上第二条链表,下面是我写的程序,为什么打印出的数据少了两条链表的最后一个数据?
----------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#define M 4
#define N 3
#define LEN sizeof(struct ListData)
struct ListData
{
int num;
struct ListData *next;
};
typedef struct ListData LIST;
int n;
struct ListData * __CreatList(int count)
{
n = 0;
struct ListData *head;
struct ListData *p1, *p2;
p1 = p2 = (struct ListData *)malloc(LEN);
if (p1 == NULL)
{
printf("Can't get values.\n");
exit (1);
}
head = NULL;
printf("Input the data:\n");
scanf("%d", &p1->num);
while (count)
{
n++;
if (n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct ListData *)malloc(LEN);
if (NULL == p1)
{
exit (1);
}
printf("Input the data:\n");
scanf("%d", &p1->num);
count--;
}
p2->next = NULL;
return (head);
}
struct ListData *__LinkList(LIST *L1, LIST *L2)
{
struct ListData *P0, *head;
head = L1->next;
while (L1)
{
P0 = L1;
L1 = L1->next;
}
P0->next = L2->next;
free(L2);
return (head);
}
void __PrintList(struct ListData *P)
{
struct ListData *L;
L = P;
printf("Now the list_data is:\n");
if (P != NULL)
{
while (L != NULL)
{
printf("%d ", L->num);
L = L->next;
}
}
}
int main(void)
{
struct ListData *L1, *L2, *head;
L1 = __CreatList(M);
L2 = __CreatList(N);
head = __LinkList(L1, L2);
__PrintList(head);
system("Pause");
return 0;
}