链表实现多项式的合并
#include <stdio.h>#include <stdlib.h>
//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->zs<pb->zs)
{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->zs<pb->zs)
{
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==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肯定有问题,老是输出不出来,哪位大神帮忙改下,不要改变代码形式,谢谢了!