原题:已知两个链表A和B,其元素值递增排序,编程将A和B合并成一个递减有序(相同元素只留下一个)的链表C,并要求利用原表节点。
分析:该程序是由LNode *MergeList()函数完成题设要求的,该函数的原理:采用插入在表头的方法实现先插入的值小的节点在表尾(相当于被后插入的结点推向表尾),而后插入的值大的结点在表头。此外,该程序的链表都是有头结点的。
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>
#include<time.h>
struct LinkList //表结点定义
{
int data;
LinkList *next;
};
struct LNode //头结点定义
{
LinkList *next;
};
LinkList *head;
LNode *hp=NULL;
int static flag=1;
int DisplayList(LNode *head)//输出链表中的数据域的值
{
if(head->next==NULL)
{
printf("链表为空!\n");
return 0;
}
LinkList *p;
p=head->next;
while(p->next!=NULL)
{
printf("%5d ->",p->data);
p=p->next;
}
printf("%5d",p->data); //输出最后一个数据
printf("\n");
return 0;
}
LNode *Create()//生成链表的函数
{
int i=0;
LinkList *p=NULL;
Link