数据结构链表合并c语言实现,链表实现多项式的合并

已结贴√

问题点数:20 回复次数:4

ca56232b3bbedf9a539d07f37fffb99a.gif

3144d8b7615c79d9f638db40d5689d26.gif

a218af6549b45ee526caf607ebff1358.gif

0f8df0e29816ae721419de940fb833d1.gif

链表实现多项式的合并

#include

#include

//the definiton of list

typedef struct node

{

int xs;

int zs;

node *next;

}lb;

typedef lb* lbx ;

//the creation of list (tail insert with head node)

lbx create(void)

{   int ch=0;

int ch1=0;

lbx head=NULL;

lbx p=NULL;

lbx q=NULL;

head=(lbx)malloc(sizeof(lb));

head->next=NULL;

head->xs=0;

head->zs=0;

p=head;//save the head

printf("Input coefficient:\n");

printf("when ch==* means the first linklist is over. you should input another one\n");

ch=getchar();//input

fflush(stdin);//use fflush mainly clear the cache region

printf("Input index:\n");

ch1=getchar();

fflush(stdin);

while(ch!='*') //using * as the ending symbol

{

q=(lbx)malloc(sizeof(lb));//apply a new space

q->xs=ch;

q->zs=ch1;

q->next=NULL;

p->next=q;

p=q;

printf("Input coefficient:\n");

ch=getchar();

fflush(stdin);

printf("Input index:\n");

ch1=getchar();

fflush(stdin);

}

return head;

}

//print the list

void print(lbx l)

{

lbx p=NULL;

printf("the new list is: \n");

p=l->next;

while (p!=NULL)

{

printf("%d ",p->xs-48);//getchar() getting's number is ASCLL,should minus 48

printf("%d ",p->zs-48);

printf("\t");

p=p->next;

}

printf("\n");

}

void print1(lbx l)

{

lbx p=NULL;

printf("the new list is: \n");

p=l->next;

while (p!=NULL)

{

printf("%d ",p->xs-96);//getchar() getting's number is ASCLL,should minus 48

printf("%d ",p->zs-48);

printf("\t");

p=p->next;

}

printf("\n");

}

/*lbx link(lbx l1,lbx l2)

{

lbx p;

p=l1;

while(p->next)

p=p->next;

p->next=l2->next;

return l1;

}*/

/*link two list

lbx link(lbx l1,lbx l2)

{

lbx pa=NULL,pb=NULL,pc=NULL;

pa=l1->next;

pb=l2->next;

pc=l1;//pc this is the current indicator

while(pb!=NULL)

{

if(pa->zszs)

{pc->next=pa;pc=pa;pa=pa->next;}

else if(pa->zs>pb->zs)

{pc->next=pb;pc=pb;pb=pb->next;}

else if(pa->zs==pb->zs)

{

if(pa->xs+pb->xs==0)

{

//lbx q1=NULL,q2=NULL ;//using to release useless indicator

pa=pa->next;

pb=pb->next;

pc->next=pa;

pc=pa;

}

else

{

pc->xs=pa->xs+pb->xs;

pa=pa->next;

pb=pb->next;

pc->next=pa;

pc=pa;

}

}

}

return l1;

}*/

void link(lbx l1,lbx l2)

{

lbx pa,pb,pc;

pa=l1->next;pb=l2->next;

pc=l1;

while(pa&&pb)

{

if(pa->zszs)

{

pc->next=pa;

pc=pa;

pa=pa->next;

}

else if(pa->zszs)

{

pc->next=pb;

pc=pb;

pb=pb->next;

}

else

{

if(pa->zszs==0)

{

pa=pa->next;

pb=pb->next;

pc->next=pa;

pc=pa;

}

else

{

pc->xs=pa->xs+pb->xs;

pa=pa->next;

pb=pb->next;

pc->next=pa;

pc=pa;

}

}

}

}

void main()

{

lbx l1=NULL,l2=NULL;

l1=create();

print(l1);

l2=create();

print(l2);

link(l1,l2);

print1(l1);

}

请教一下:link肯定有问题,老是输出不出来,哪位大神帮忙改下,不要改变代码形式,谢谢了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值