3.6编写两个多项式相加的函数。用一个链表实现。
我的思路是,首先创建两个链表,接着将两个链表合并为一个链表(mergeList)函数。
之后将合并后的链表进行排序(sortList)函数,将排好序的链表进行多项式相加(及指数相同的数字进行相加),定义一个数组,将指数(index)看为数组的下标,系数的加和看为数组的值。最后创建结点和新的链表,将数组的下标赋值作为新结点的指数,数组值作为新结点的系数,通过尾插法插入新链表,即可完成多项式的加和。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define max 1000
typedef struct node {
int index;//指数
float coef;//系数
struct node* next;
}Node,*LinkList;
//用到的函数
LinkList LinkListInit();
void printLinkList(LinkList L);
int getLen(LinkList L);//计算链表长度
//初始化一个链表
LinkList LinkListInit() {
Node* L;
L = (Node*)malloc(sizeof(Node)); //申请结点空间
if (L == NULL) { //判断是否有足够的内存空间
printf("申请内存空间失败\n");
}
L->next = NULL;//将next设置为NULL,初始长度为0的单链表
return L;
}
//创建链表
LinkList createList(LinkList L) {
Node* s, * r = L;
int num;//指数
float factor;//系数
printf("请输入系数和指数:\n");
scanf("%f %d", &factor, &num);
while (num != 0 && factor != 0) {
s = (Node*)malloc(sizeof(Node));
s->index = num;
s->coef = factor;
r->next = s;
r = s;
scanf("%f %d", &factor, &num);
}
r->next = NULL;
return L;
}
//链表长度
int getLen(LinkList L) {
int len = 0;
LinkList R;
if (L->next == NULL)//头指针为空,则链表不包含元素,返回0
return 0;
else {
R = L->next;
while (R != NULL) {
R = R->next;
len++;
}
}
return len;
}
//根据指数大小对链表进行排序
LinkList sortList(LinkList L) {
LinkList R;
R = (LinkList)malloc(sizeof(Node));
Node* p, * q;
int temp;
float temp1;
p = L->next;
q = p->next;
for (; p; p = p->next) {
for (; q; q = q->next) {
if (p->index > q->index) {
temp = p->index;
temp1 = p->coef;
p->index = q->index;
p->coef = q->coef;
q->index = temp;
q->coef = temp1;
}
}
q = L->next;
}
return L;
}
//链表合并
LinkList mergeList(LinkList L1, LinkList L2, LinkList P) {
LinkList r, s;
Node* t, * m;
m = P;
r = L1->next;
s = L2->next;
while (r) {//将链表L1的值接入P中
t = (Node*)malloc(sizeof(Node));
t->index = r->index;
t->coef = r->coef;
m->next = t;
m = t;
r = r->next;
}
m->next = NULL;
while (s) {
t = (Node*)malloc(sizeof(Node));
t->index = s->index;
t->coef = s ->coef;
m->next = t;
m = t;
s = s->next;
}
m->next = NULL;
return P;
}
//链表多项式相加
LinkList addList(LinkList R) {
LinkList L;
L = (LinkList)malloc(sizeof(Node));
L->next = NULL;
int maxNum,minNum,len,i;
float arr[100] = { 0 };
len = getLen(R);
Node* p,*q,*s,*r = L;
p = R->next;
q = p;
while (q->next != NULL) {
q = q->next;
}
minNum = q->index;//记录最小指数
maxNum = p->index;//记录最大指数
for (i = 0; i < len; i++) {//将数组中的值置为空
arr[i] = 0;
}
while (p != NULL) {
i = p->index;
arr[i] += p->coef;
p = p->next;
}
for (i = minNum; i <= maxNum; i++) {
s = (Node*)malloc(sizeof(Node));
s->index = i;
s->coef = arr[i];
r->next = s;
r = s;
}
r->next = NULL;
return L;
}
//输出链表
void printLinkList(LinkList L)
{
LinkList p;
p = L->next;
while (p)
{
printf("\n");
printf("系数:%.2f,指数:%d",p->coef, p->index);
p = p->next;
}
}
int main()
{
LinkList L1, L2,List1,List2,R,P,L;
P = LinkListInit();
int len1, len2;
L1 = LinkListInit();
L2 = LinkListInit();
printf("请输入第一个链表的指数和系数:\n");
List1 = createList(L1);
printf("请输入第二个链表的指数和系数:\n");
List2 = createList(L2);
R = mergeList(List1, List2, P);
sortList(R);
L = addList(R);
printLinkList(L);
return 0;
}
效果图: