数据结构的习题
//设有两个线性表A和B皆是单链表存储结构。
//同一个表中的元素各不相同,且递增有序。
//写一算法,构成一个新的线性表C,使C为A和B的交集,且C中元素也递增有序。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int ElemType;
typedef struct node
{
ElemType data;
struct node* next;
}LNode, * LinkList;
void CreLinkListTail(LinkList L, int n)
{
LNode* s, * r;
ElemType x;
int i;
r = L;
printf("输入结点:\n");
for (i = n; i > 0; i--)
{
scanf_s("%d", &x);
s = (LNode*)malloc(sizeof(LNode));
s->next = NULL;//
s->data = x;
r->next = s;
r = s;
}
}
void OutPut(LinkList L)
{
LNode* p;
p = L;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void linkedlist_delete(LinkList La, LinkList Lb, LinkList Lc)
{
LNode* pa, * pb, * pc, * q, * u;
pa = La->next;
pb = Lb->next;
pc = Lc = La;
while (pa && pb)
{
if (pa->data == pb->data)
{
pc->next = pa;
pc = pa;
pa = pa->next;
u = pb;
pb = pb->next;
free(u);
}
else if (pa->data < pb->data)
{
u = pa;
pa = pa->next;
free(u);
}
else
{
u = pb;
pb = pb->next;
free(u);
}
}
while (pa)
{
u = pa;
pa = pa->next;
free(u);
}
while (pb)
{
u = pb;
pb = pb->next;
free(u);
}
pc->next = NULL;
free(Lb);
}
void main()
{
int n = 0;
scanf_s("%d", &n);
LinkList La = (LinkList)malloc(sizeof(LNode));
La->next = NULL;
CreLinkListTail(La, n);
printf("带头结点尾插法--输出建立后的单链表:\n");
OutPut(La->next);
LinkList Lb = (LinkList)malloc(sizeof(LNode));
Lb->next = NULL;
CreLinkListTail(Lb, n);
printf("带头结点尾插法--输出建立后的单链表:\n");
OutPut(Lb->next);
LinkList Lc = (LinkList)malloc(sizeof(LNode));
Lc->next = NULL;
CreLinkListTail(Lc, n);
printf("输出归并后的Lc单链表:\n");
linkedlist_delete(La, Lb, Lc);
OutPut(Lc->next);
}
希望能帮忙改进一下代码,错的地方注释出来并优化一下,希望可以详细注释一下