顺序表
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MaxSize 100
typedef int Elemtype;
typedef struct LNode
{
Elemtype data;
struct LNode *next;
} LinkNode;
void CreateListR(LinkNode *&L,Elemtype a[],int n)
{
LinkNode *s,*r;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
r=L;
for (int i=0;i<n;i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void InitList(LinkNode *&L)
{
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
}
void DestroyList(LinkNode *&L)
{
LinkNode *pre=L,*p=pre->next;
while (p!=NULL)
{ free(pre);
pre=p;
p=pre->next;
}
free(pre);
}
void UnionList1(LinkNode*LA,LinkNode*LB,LinkNode *&LC)
{
LinkNode *pa=LA->next,*pb=LB->next,*r,*s,*z;
LC=(LinkNode *)malloc(sizeof(LinkNode));
r=LC;
while(pa!=NULL&&pb!=NULL)
{
if(pa->data<pb->data)
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=pa->data;
r->next=s;
r=s;
pa=pa->next;
}
else
{
s=(LinkNode*)malloc(sizeof(LinkNode));
s->data=pb->data;
r->next=s;
r=s;
pb=pb->next;
}
}
while(pa!=NULL)
{
s=(LinkNode*)malloc(sizeof(LinkNode));
s->data=pa->data;
r->next=s;
r=s;
pa=pa->next;
}
while(pb!=NULL)
{
s=(LinkNode*)malloc(sizeof(LinkNode));
s->data=pb->data;
r->next=s;r=s;
pb=pb->next;
}
r->next=NULL;
}
void DispList(LinkNode *L)
{
LinkNode *p=L->next;
while (p!=NULL)
{ printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
int main()
{
LinkNode *LA;
LinkNode *LB;
LinkNode *LC;
int a[5]={1,3,5,7,9};
int b[5]={2,4,6,8,10};
int c[5]={0,0,0,0,0};
InitList(LA);
printf("LA初始化完成\n");
InitList(LB);
printf("LB初始化完成\n");
CreateListR(LA,a,5);
CreateListR(LB,b,5);
UnionList1(LA,LB,LC);
DispList(LC);
DestroyList(LA);
printf("已释放链表LA\n");
DestroyList(LB);
printf("已释放链表LB\n");
DestroyList(LC);
printf("已释放链表LC");
}