/*含頭結(jié)點(diǎn)的循環(huán)鏈表的多項(xiàng)式*/
# include<stdio.h>
# include<stdlib.h>
struct plist /* 多項(xiàng)式結(jié)構(gòu)聲明*/
{
int coef; /*多項(xiàng)式的系數(shù)*/
int exp; /*多項(xiàng)式的指數(shù)*/
struct plist *next; /*指向下一結(jié)點(diǎn)的指針*/
};
typedef struct plist pnode; /* 多項(xiàng)式新類型*/
typedef pnode *plink; /* 多項(xiàng)式指針新類型*/
/*鏈表輸出*/
void printpoly(plink poly)
{
plink ptr;
ptr=poly->next; /*指向鏈表開始*/
while(poly!=ptr) /*鏈表遍歷循環(huán)*/
{
/*輸出結(jié)點(diǎn)數(shù)據(jù)*/
printf("%dX^%d",ptr->coef,ptr->exp);
ptr=ptr->next; /* 指向下一結(jié)點(diǎn)*/
if(poly!=ptr) printf("+");
}
printf("\n"); /* 換行*/
}
/*使用數(shù)組值創(chuàng)建多項(xiàng)式*/
plink createpoly(int *array,int len)
{
plink head; /*循環(huán)鏈表的指針*/
plink before; /*前一結(jié)點(diǎn)的指針*/
plink new_node; /*新結(jié)點(diǎn)的指針*/
int i;
/*創(chuàng)建頭結(jié)點(diǎn),分配結(jié)點(diǎn)內(nèi)存*/
head=(plink)malloc(sizeof(pnode));
if(!head) return NULL; /*檢查內(nèi)存指針*/
head->exp=-1; /*創(chuàng)建結(jié)點(diǎn)內(nèi)容*/
before=head; /*指向第一個結(jié)點(diǎn)*/
for(i=len-1;i>=0;i--) /*用循環(huán)創(chuàng)建其他結(jié)點(diǎn)*/
if(array[i]!=0)
{
/*分配結(jié)點(diǎn)內(nèi)存*/
new_node=(plink)malloc(sizeof(pnode));
if(!new_node) return NULL; /*檢查內(nèi)存指針*/
new_node->coef=array[i]; /*創(chuàng)建系數(shù)內(nèi)容*/
new_node->exp=i; /*創(chuàng)建指數(shù)內(nèi)容*/
new_node->next=NULL; /*設(shè)置指針初值*/
before->next=new_node; /*將前結(jié)點(diǎn)指向新結(jié)點(diǎn)*/
before=new_node; /*新結(jié)點(diǎn)成為前結(jié)點(diǎn)*/
}
new_node->next=head; /*創(chuàng)建環(huán)狀鏈接*/
return head; /*返回鏈表起始指針*/
}
/*多項(xiàng)式相加*/
plink polyadd(plink poly1,plink poly2)
{
plink head1; /*多項(xiàng)式1的開始*/
plink head2; /*多項(xiàng)式2的開始*/
plink result; /*多項(xiàng)式的結(jié)果*/
plink before; /*前一結(jié)點(diǎn)的指針*/
plink new_node; /*新結(jié)點(diǎn)的指針*/
head1=poly1->next; /*指向多項(xiàng)式1的開始*/
head2=poly2->next; /*指向多項(xiàng)式2的開始*/
/*創(chuàng)建頭結(jié)點(diǎn)且分配結(jié)點(diǎn)內(nèi)存*/
result=(plink)malloc(sizeof(pnode));
if(!result) return NULL; /*檢查內(nèi)存指針*/
result->exp=-1; /*創(chuàng)建結(jié)點(diǎn)內(nèi)容*/
before=result; /*指向第一個結(jié)點(diǎn)*/
while(poly1!=head1||poly2!=head2)
{
/*分配結(jié)點(diǎn)內(nèi)存*/
new_node=(plink)malloc(sizeof(pnode));
if(!new_node) return NULL; /*檢查內(nèi)存指針*/
if(head1->exp<head2->exp) /*多項(xiàng)式2的指數(shù)大*/
{
new_node->coef=head2->coef; /*設(shè)置系數(shù)*/
new_node->exp=head2->exp; /*設(shè)置指數(shù)*/
head2=head2->next; /*指向下一結(jié)點(diǎn)*/
}
else if(head1->exp>head2->exp) /*多項(xiàng)式1的指數(shù)大*/
{
new_node->coef=head1->coef; /*設(shè)置系數(shù)*/
new_node->exp=head1->exp; /*設(shè)置指數(shù)*/
head1=head1->next; /*指向下一結(jié)點(diǎn)*/
}
else /*多項(xiàng)式的指數(shù)相等*/
{
/*系數(shù)相加*/
new_node->coef=head1->coef+head2->coef;
new_node->exp=head1->exp; /*設(shè)置指數(shù)*/
head1=head1->next; /* 指向下一結(jié)點(diǎn)*/
head2=head2->next; /* 指向下一結(jié)點(diǎn)*/
}
before->next=new_node; /*將前一結(jié)點(diǎn)指向新結(jié)點(diǎn)*/
before=new_node; /*新結(jié)點(diǎn)成為前結(jié)點(diǎn)*/
}
new_node->next=result; /*創(chuàng)建環(huán)狀鏈接*/
return result; /*返回多項(xiàng)式的指針*/
}
void main()
{
plink poly1; /*多項(xiàng)式1的指針*/
plink poly2; /*多項(xiàng)式2的指針*/
plink result; /*多項(xiàng)式結(jié)果的指針*/
int list1[6]={4,0,3,0,7,0}; /*數(shù)組1的內(nèi)容*/
int list2[6]={9,7,1,0,5,6}; /*數(shù)組2的內(nèi)容*/
poly1=createpoly(list1,6); /*創(chuàng)建多項(xiàng)式1*/
printf("the content1:");
printpoly(poly1); /*輸出多項(xiàng)式1*/
poly2=createpoly(list2,6); /*創(chuàng)建多項(xiàng)式2*/
printf("the content2:");
printpoly(poly2); /*輸出多項(xiàng)式2*/
result=polyadd(poly1,poly2); /*多項(xiàng)式相加*/
printf("the add:");
printpoly(result); /*輸出多項(xiàng)式結(jié)果*/
}