LinkList a, b, c;
网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、微信平台小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了鼓楼免费建站欢迎大家使用!
printf("building a\n");
a = CreatLinlList();
printf("building b\n");
b = CreatLinlList();
printf("a is\n");
PrintList(a);
printf("b is\n");
PrintList(b);
c=merge(a,b)
printf("the merge is\n");
PrintList(c);
deleteLinkList(a);
deleteLinkList(b);
deleteLinkList(c);
应该把CreatLinlList()改为
LinkList CreatLinlList()
{
LinkList L;
Lnode *s,*r;
int x;
int len; // int flag;
L=r=NULL;
printf("input length:"); //
scanf("%d",len); //
scanf("%d",x);
while(len-- != 0) // while(x!=flag)
{
s=(Lnode *)malloc (sizeof (Lnode));
s-data = x;
if(L==NULL)
L=s;
else
r-next=s;
r=s;
scanf("%d",x);
}
if(r!=NULL)
r-next=NULL;
return L;
}
创建链表的函数有些问题吧
student *p;
student *q=I;
for (j=0;ji;j++)
{
p=(student*)malloc(sizeof(student));
p-next=Null;
q-next=p;
q=p;
}
注释的部分是采用数组实现的,未注释的是采用链表实现的,可以将链表实现的注释起来和数组实现的运行做对比
#include "stdio.h"
#include "stdlib.h"
/*采用数组实现
int merge(int* a,int* b,int*c,int alen,int blen)
{
int i=0,j=0,k=0;
//每次将a和b中当前的元素进行比较,并将小的一个存入到c中
while(ialen jblen)
{
if(a[i]b[j])
c[k]=a[i++];
else c[k]=b[j++];
k++;
}
//其中一个数组已经结束,将另一个数组剩余部分全部复制到c即可
while(ialen)
c[k++]=a[i++];
while(jblen)
c[k++]=b[j++];
return k;//返回值为c的有效长度
}
void main()
{
int a[]={1,3,5,7,9,10,12,14};
int b[]={2,4,6,8,10,11,12,13};
int c[100];
int i,clen;
clen=merge(a,b,c,sizeof(a)/sizeof(*a),sizeof(b)/sizeof(*b));
for(i=0;iclen;i++)
printf("%d ",c[i]);
printf("\n");
}
*/
struct Node
{
int value;
struct Node* next;
};
struct LinkList
{
struct Node* head;
};
//将数组a赋值给链表list
void setLinkList(int* a,int alen,struct LinkList* list)
{
int i;
struct Node* p,*temp;
list-head=(struct Node*)malloc(sizeof(struct Node));
p=list-head;
for(i=0;ialen;i++)
{
p-value=a[i];
p-next=(struct Node*)malloc(sizeof(struct Node));
temp=p;
p=p-next;
}
free(p);
temp-next=NULL;//尾指针赋NULL
}
void mergeLinkList(struct LinkList* a,struct LinkList* b,struct LinkList* c)
{
struct Node *p=a-head,*q=b-head,*r,*temp;
c-head=(struct Node*)malloc(sizeof(struct Node));
r=c-head;
while(p!=NULL q!=NULL)
{
if(p-valueq-value)
{
r-value=p-value;
p=p-next;
}
else
{
r-value=q-value;
q=q-next;
}
r-next=(struct Node*)malloc(sizeof(struct Node));
temp=r;
r=r-next;
}
while(p!=NULL)
{
r-value=p-value;
p=p-next;
r-next=(struct Node*)malloc(sizeof(struct Node));
temp=r;
r=r-next;
}
while(q!=NULL)
{
r-value=q-value;
q=q-next;
r-next=(struct Node*)malloc(sizeof(struct Node));
temp=r;
r=r-next;
}
free(r);
temp-next=NULL;
}
#define MAX_LEN 1000
void main()
{
int a[MAX_LEN];//={1,3,5,7,9,10,12,14};
int b[MAX_LEN];//={2,4,6,8,10,11,12,13};
int alen,blen,i;
struct LinkList alist,blist,clist;
struct Node* p;
printf("Please input the length of a : ");
scanf("%d",alen);
fflush(stdin);
printf("Please input %d number and separate them with space :\n",alen);
for(i=0;ialen;i++)
scanf("%d",a+i);
fflush(stdin);//这句很有必要,当输入的数据超过alen时,后面的数据将被忽略
printf("Please input the length of b : ");
scanf("%d",blen);
fflush(stdin);
printf("Please input %d number and separate them with space :\n",blen);
for(i=0;iblen;i++)
scanf("%d",b+i);
fflush(stdin);
setLinkList(a,alen,alist);
setLinkList(b,blen,blist);
mergeLinkList(alist,blist,clist);
p=clist.head;
printf("The result is :\n");
while(p!=NULL)
{
printf("%d ",p-value);
p=p-next;
}
printf("\n");
}