單鏈表操作之一元多項式的合併
阿新 • • 發佈:2018-11-26
多項式就是單項式的加減法,對於單項式可以用一組數表示,例如2X^3,可以表示為(2,3)。我們可以使用單鏈表來進行多項式的合併操作。
一:首先我們先建立新的資料型別multi
typedef struct multi{
int coef;
int index;
struct multi *next;
}multi;
二:建立連結串列。我這裡建立的為帶有頭結點的連結串列,這樣方便之後的合併操作(關於建立的兩種方法可以參考我的另一篇文章單鏈表的基礎操作(頭插法、尾插法、插入和刪除)https://blog.csdn.net/weixin_43260290/article/details/82843347
struct multi *tailcreat(int a) { struct multi *head; struct multi *p,*q; int n = 0; head = (struct multi*)malloc(sizeof(struct multi)); p = (struct multi*)malloc(sizeof(struct multi)); printf("請輸入多項式%d配對的係數和指數:\n",a); do{ q = (struct multi*)malloc(sizeof(struct multi)); scanf("%d%d",&q->coef,&q->index); if(q->coef == 0) break; if(n == 0) { p->next = q; head->next = q; p = q; n++; } else { p->next = q; p = q; } }while(true); p->next = NULL; return(head); }
三:連結串列的輸出
void display(struct multi *head,int i) { printf("第%d個多項式為:\n",i); while((head->next)!=NULL) { printf("%dx^%d+",(head->next)->coef,(head->next)->index); head = head->next; } printf("\b \n"); //\b為游標後退一格,後退後再輸出個空格就將多項式最後一個+號覆蓋啦~ }
四:連結串列的合併
struct multi *combine(struct multi *head1,struct multi *head2)
{
struct multi *pa,*pb,*L,*p;
pa = head1->next;
pb = head2->next;
L = p = head1;
while(pa&&pb)
{
if(pa->index == pb->index)
{
if(pa->coef + pb->coef)
{
pa->coef = pa->coef + pb->coef;
p->next = pa;
p = pa;
pa = pa->next;
pb = pb->next;
}
else{
pa = pa->next;
pb = pb->next;
}
}
else if(pa->index > pb->index)
{
p->next = pb;
p = pb;
pb = pb->next;
}
else
{
p->next = pa;
p = pa;
pa = pa->next;
}
}
/*
while(pa)
p->next = pa;
while(pb)
p->next = pb;
*/ //永遠無法跳出迴圈。我是智障叭!!!
if(pa == NULL)
{
p->next = pb;
}
else{
p->next = pa;
}
return(L);
}
期間謝謝機機的指教hhhh,大家共同進步!
下面是完整的程式碼~
#include<stdio.h>
#include<stdlib.h>
typedef struct multi{
int coef;
int index;
struct multi *next;
}multi;
struct multi *tailcreat(int a)
{
struct multi *head;
struct multi *p,*q;
int n = 0;
head = (struct multi*)malloc(sizeof(struct multi));
p = (struct multi*)malloc(sizeof(struct multi));
printf("請輸入多項式%d配對的係數和指數:\n",a);
do{
q = (struct multi*)malloc(sizeof(struct multi));
scanf("%d%d",&q->coef,&q->index);
if(q->coef == 0)
break;
if(n == 0)
{
p->next = q;
head->next = q;
p = q;
n++;
}
else
{
p->next = q;
p = q;
}
}while(true);
p->next = NULL;
return(head);
}
void display(struct multi *head,int i)
{
printf("第%d個多項式為:\n",i);
while((head->next)!=NULL)
{
printf("%dx^%d+",(head->next)->coef,(head->next)->index);
head = head->next;
}
printf("\b \n"); //\b為游標後退一格,後退後再輸出個空格就將多項式最後一個+號覆蓋啦~
}
struct multi *combine(struct multi *head1,struct multi *head2)
{
struct multi *pa,*pb,*L,*p;
pa = head1->next;
pb = head2->next;
L = p = head1;
while(pa&&pb)
{
if(pa->index == pb->index)
{
if(pa->coef + pb->coef)
{
pa->coef = pa->coef + pb->coef;
p->next = pa;
p = pa;
pa = pa->next;
pb = pb->next;
}
else{
pa = pa->next;
pb = pb->next;
}
}
else if(pa->index > pb->index)
{
p->next = pb;
p = pb;
pb = pb->next;
}
else
{
p->next = pa;
p = pa;
pa = pa->next;
}
}
/*
while(pa)
p->next = pa;
while(pb)
p->next = pb;
*/ //永遠無法跳出迴圈。我是智障叭!!!
if(pa == NULL)
{
p->next = pb;
}
else{
p->next = pa;
}
return(L);
}
int main()
{
struct multi *head,*head1,*head2;
head1 = tailcreat(1);
display(head1,1);
head2 = tailcreat(2);
display(head2,2);
head = combine(head1,head2);
display(head,3);
return 0;
}