200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 数据结构链表合并c语言实现 链表实现多项式的合并

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

时间:2021-06-08 00:03:08

相关推荐

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

已结贴√

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

链表实现多项式的合并

#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肯定有问题,老是输出不出来,哪位大神帮忙改下,不要改变代码形式,谢谢了!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。