c语言编写无序顺序表,请用c语言写 在非递减有序的顺序表中插入一个元素x,保持顺序表有序性...

满意答案

00e27ab806e4881f8254fe7ae8741834.png

A2用户

2015.04.24

00e27ab806e4881f8254fe7ae8741834.png

采纳率:43%    等级:9

已帮助:1315人

#include

#include

struct link{

int data;

struct link *next;

};

struct link *fun(struct link *h1,struct link *h2)

{

struct link *h,*p=NULL;

if(h1 && h2)

h=h1->datadata?h1:h2;

else

{

if(h1)

return h1;

if(h2)

return h2;

return NULL;

}

while(h1 && h2)

{

if(p)

{

if(h1->datadata)

{

p->next=h1;

h1=h1->next;

}

else

{

p->next=h2;

h2=h2->next;

}

p=p->next;

}

else

if(h1->datadata)

{

p=h1;

h1=h1->next;

}

else

{

p=h2;

h2=h2->next;

}

}

p->next=h1?h1:h2;

return h;

}

void main()

{

struct link *h1=NULL,*h2=NULL,*p1,*p2;

int i,a1[10]={1,3,4,5,9,11,15,16,20,25},a2[10]={2,8,10,12,13,19,21,22,23,24};

for(i=0;i<10;i++)

{

if(h1==h2)

{

h1=p1=(struct link *)malloc(sizeof(struct link));

h2=p2=(struct link *)malloc(sizeof(struct link));

}

else

{

p1->next=(struct link *)malloc(sizeof(struct link));

p2->next=(struct link *)malloc(sizeof(struct link));

p1=p1->next;

p2=p2->next;

}

p1->data=a1[i];

p1->next=NULL;

p2->data=a2[i];

p2->next=NULL;

}

h1=fun(h1,h2);

for(p1=h1;p1;p1=p1->next)

printf("%3d",p1->data);

}

00分享举报

你可能感兴趣的:(c语言编写无序顺序表)