链表,帮帮我修改一下这个代码!
#include <malloc.h>#include<stdio.h>
#define ok 1
#define error 0
typedef int Elemtype;
typedef struct Node
{Elemtype data;
struct Node *next;
}Node,*LinkList;
int Initlist(LinkList *L) /*初始化链表*/
{
*L=(LinkList)malloc(sizeof(Node));
(*L)->next=NULL;
return(ok);
}
void createlist(LinkList *L,Elemtype a[],int n) /*创建链表*/
{Node *p,*r;
int i;
r=L;
for (i=0;i<=n;i++)
{
p = (Node *) malloc (sizeof (Node));
p->data=a[i];
p->next=r->next;
r->next =p;
p = r;
}
}
int inslist(LinkList L,int i,Elemtype e) /*插入元素e*/
{Node *pre,*s;
int k;
if(i<1) return error;
pre=L;k=0;
while(pre!=NULL&&k<i-1)
{pre=pre->next;
k=k+1;
}
if(!pre)
{printf("位置不合理");
return error;
}
s=(Node*)malloc(sizeof(Node));
s->data=e;
s->next=pre->next;pre->next=s;
return ok;
}
int DelList_L(LinkList L, int i, Elemtype *e) /*删除链表中的第i个元素*/
{Node *p,*q;
int j;
p = L;
j = 0;
while (p->next && j < i-1)
{
p = p->next; ++j;
if(!(p->next) || j > i-1)
return error;
q=p->next;
p->next=q->next;
*e = q->data;
}
free(q);
return ok;
}
LinkList MergeLinkList(LinkList La,LinkList Lb,LinkList Lc) /*合并链表*/
{
Node *pa,*pb,*r;
pa=La->next;
pb=Lb->next;
Lc->next=NULL;
r=Lc;
while(pa!=NULL&&pb!=NULL)
{
if(pa->data<=pb->data)
{
r->next=pa;
r=pa;
pa=pa->next;
}
else
r->next=pb;r=pb;pb=pb->next;
}
if(pa)
r->next=pa;
else r->next=pb;
free(Lb);
return Lc;
}
/*打印链表*/
int printlist(LinkList L)
{ Node *p;
p=L->next;
while(p!=NULL)
{ printf("%5d",p->data); p=p->next; }
}
void main()
{
LinkList la;
LinkList lb;
LinkList lc;
Elemtype a[]={6,5,4,3,1};
Elemtype b[]={9,6,5,2,1};
Elemtype x;
Initlist(la);
createlist(&la,a,3);
printlist(&la);
printf("\n");
Initlist(lb);
createlist(&lb,b,2);
printlist(&lb);
printf("\n");
Initlist(lc);
MergeLinkList(&la,&lb,&lc);
printlist(&lc);
printf("\n");
inslist(&la,6,10);
printlist(&la);
printf("\n");
DelList_L(&la,3,&x);
printlist(&la);
getch();
}