单链表
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef int Elemtype;
typedef struct LNode
{
Elemtype date;
struct LNode *next;
} LinkNode;
void CreateListF(LinkNode *&L,Elemtype a[],int n)
{
LinkNode *s;int i;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
for (i=0;i<n;i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->date=a[i];
s->next=L->next;
L->next=s;
}
}
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);
}
bool ListEmpty(LinkNode *L)
{
return(L->next==NULL);
}
int ListLength(LinkNode *L)
{
LinkNode *p=L;int i=0;
while (p->next!=NULL)
{ i++;
p=p->next;
}
return(i);
}
void DispList(LinkNode *L)
{
LinkNode *p=L->next;
while (p!=NULL)
{ printf("%d ",p->date);
p=p->next;
}
printf("\n");
}
void UnionList1(LinkNode*LA,LinkNode*LB,LinkNode *&LC)
{
LinkNode *pa=LA->next,*pb=LB->next,*r,*s;
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;
}
bool ListInsert(LinkNode *&L,int i,Elemtype e)
{
int j=0;
LinkNode *p=L,*s;
if (i<=0) return false;
while (j<i-1 && p!=NULL)
{ j++;
p=p->next;
}
if (p==NULL)
return false;
else
{ s=(LinkNode *)malloc(sizeof(LinkNode));
s->date=e;
s->next=p->next;
p->next=s;
return true;
}
}
bool ListDelete(LinkNode *&L,int i,Elemtype &e)
{
int j=0;
LinkNode *p=L,*q;
if (i<=0) return false;
while (j<i-1 && p!=NULL)
{ j++;
p=p->next;
}
if (p==NULL)
return false;
else
{ q=p->next;
if (q==NULL)
return false;
e=q->date;
p->next=q->next;
free(q);
return true;
}
}
int main()
{
LinkNode *LA;
LinkNode *LB;
LinkNode *LC;
Elemtype a[MaxSize];
Elemtype b[MaxSize];
int n1;
printf("请输入n1多项式的系数个数:");
scanf("%d",&n1);
printf("请输入n1多项式的系数")
for(int i=0;i<n1;i++)
{
scanf("%d",a[i]);
}
int n2;
printf("请输入n2多项式的系数个数:");
scanf("%d",&n2);
printf("请输入n2多项式的系数")
for(int i=0;i<n2;i++)
{
scanf("%d",b[i]);
}
Elemtype e;
InitList(LA);
InitList(LB);
printf("初始化完成\n");
CreateListF(L,a,5);
printf("头插入元素后输出的链表为:\n");
DispList(LA);
DispList(LB);
DestroyList(LA);
DestroyList(LB);
DestroyList(LC);
printf("释放全部链表");
return 0;
}