线性结构2 一元多项式的乘法与加法运算 (20 分)
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。
输入样例:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
输出样例:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0
code
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct LNode{
ElemType factor;
ElemType index;
struct LNode *next;
}LNode,*LinkList;
LinkList creatlist();
LinkList sumList(LinkList list1,LinkList list2);
LinkList multiList(LinkList list1,LinkList list2);
void deletelist( LinkList head );
void printlist( LinkList head );
int main()
{
LinkList s1,s2,sum,multi;
s1 = creatlist();
s2 = creatlist();
sum = sumList(s1,s2);
multi = multiList(s1,s2);
deletelist(multi);
deletelist(sum);
printlist(multi);
printlist(sum);
return 0;
}
/*函数creatlist()从键盘读入多项式存储到链表中,并返回头指针*/
LinkList creatlist()
{
int n;
scanf("%d",&n);
LinkList head,p,q;
head = (LinkList)malloc(sizeof(LNode));
head->next = NULL;
q=head;
for(int i=0;i<n;i++)
{
p = (LinkList)malloc(sizeof(LNode));
scanf("%d %d",&p->factor,&p->index);
p->next = NULL;
q->next = p;
q = p;
}
return head->next;//由于头指针数据域为空,返回开始指针
}
/*两个多项式相加,返回结果多项式头指针*/
LinkList sumList(LinkList list1,LinkList list2)
{
LinkList head,p,q;
head = (LinkList)malloc(sizeof(LNode));
head->next = NULL;
q=head;
while(list1||list2)
{
p = (LinkList)malloc(sizeof(LNode));
/*如果多项式1遍历完后,多项式2未遍历结束,则继续遍历多项式2到和多项式中*/
if(list1==NULL&&list2!=NULL)
{
p->index = list2->index;
p->factor = list2->factor;
list2 = list2->next;
}
/*如果多项式2遍历完后,多项式1未遍历结束,则继续遍历多项式1到和多项式中*/
else if(list2==NULL&&list1!=NULL)
{
p->index = list1->index;
p->factor = list1->factor;
list1 = list1->next;
}
/*当多项式1和2都未遍历完毕时,算法如下: 1.若多项式1当前项指数大于多项式2该项,則直接将多项式1该项放到和多项式中; 若多项式1当前项指数等于多项式2该项,則系数相加后放到和多项式中; 若多项式1当前项指数小于多项式2该项,則直接将多项式2该项放到和多项式中*/
else
{
if(list1->index > list2->index )
{
p->index = list1->index;
p->factor = list1->factor;
list1 = list1->next;
}else if(list1->index == list2->index )
{
p->index = list1->index;
p->factor = list1->factor + list2->factor;
list1 = list1->next;
list2 = list2->next;
}else
{
p->index = list2->index;
p->factor = list2->factor;
list2 = list2->next;
}
}
p->next = NULL;
q->next = p;
q=p;
}
return head;
}
/*两个多项式相乘,返回结果多项式头指针*/
LinkList multiList(LinkList list1,LinkList list2)
{
LinkList head,p,q,temp;
head = (LinkList)malloc(sizeof(LNode));
head->next = NULL;
q=head;
int i=0;
temp = list2;
/*list1和list2每项相乘,得到初始结果链表*/
while(list1)
{
while(temp)
{
p = (LinkList)malloc(sizeof(LNode));
p->factor = list1->factor * temp->factor;
p->index = list1->index + temp->index;
temp = temp->next;
i++;
p->next = NULL;
q->next = p;
q=p;
}
temp = list2;
list1 = list1->next;
}
/*排序 1.对于指数相同项,进行合并; 2.冒泡排序,按照指数从高到低排序 */
ElemType t1,t2;
for(int j=i;j>0;j--)
{
p = head->next;
q = head;
int swap = 0;
for(int k=0;k<j-1;k++){
if(p->index == p->next->index)
{
swap = 1;
p->next->factor += p->factor;
q->next = p->next;
free(p);
p=q;
}else if(p->index < p->next->index)
{
swap = 1;
t1 = p->index;
t2 = p->factor;
p->index = p->next->index;
p->factor = p->next->factor;
p->next->index = t1;
p->next->factor = t2;
}
q = p;
p = p->next;
}
if(swap==0)
{
break;
}
}
return head;
}
/*如果多项式内某结点系数为0,則delete该结点*/
void deletelist( LinkList head )
{
LinkList p,q = head;
p = head->next;
while (p) {
if(p->factor==0)
{
q->next = p->next;
free(p);
p=q;
}
q=p;
p = p->next;
}
}
/*输出多项式链表*/
void printlist( LinkList head )
{
LinkList p = head->next;
if(p==NULL)
{
printf("0 0");
}else
{
while (p->next) {
printf("%d %d ", p->factor,p->index);
p = p->next;
}
if(p->next==NULL)
{
printf("%d %d", p->factor,p->index);
}
}
printf("\n");
}