2.8 案例分析与实现
2.8.1 多项式的创建
typedef struct PNode
{
float coef;
int expn;
struct PNode * next;
}PNode, * Polynomial;
void Createpolyn(Polynomial &P,int n)
{
P=new PNode;
P->next=NULL;
for(i=1;i<=n;++i)
{
s=new PNode;
cin>>s->coef>>s->expn;
pre=P;
q=P->next;
while(q&&q->expn<s->expn)
{
pre=q;
q=q->next;
}
s->next=q;
pre->next=s;
}
}
单链表C语言实现
#include<stdio.h>
#include<stdlib.h>
struct Lnode {
float coef;
int expn;
struct Lnode* next;
};
struct Lnode* Create(struct Lnode* L, int n);
void printPolyn(struct Lnode* L);
void printPolynTerm(struct Lnode* L);
int main()
{
struct Lnode* LNODE = NULL;
struct Lnode* list = Create(LNODE, 5);
printPolyn(list);
printPolynTerm(list);
return 0;
}
struct Lnode* Create(struct Lnode* L, int n)
{
struct Lnode* s;
L = (struct Lnode*)malloc(sizeof(struct Lnode));
L->next = NULL;
float c;
int e;
struct Lnode* pre = L;
struct Lnode* p = L->next;
for (int i = 1; i <= n; i++)
{
s = (struct Lnode*)malloc(sizeof(struct Lnode));
printf("请输入第%d项的系数:", i);
scanf_s("%f", &c);
printf("请输入第%d项的指数:", i);
scanf_s("%d", &e);
s->coef = c;
s->expn = e;
struct Lnode* pre = L;
struct Lnode* p = L->next;
while (p && p->expn < s->expn)
{
pre = p;
p = p->next;
}
s->next = p;
pre->next = s;
}
printf("创建成功!\n");
return L;
}
void printPolyn(struct Lnode* L)
{
struct Lnode* pMove = L->next;
int i = 0;
for (i = 1; pMove != NULL; i++)
{
printf("第%d个系数:%f\n", i, pMove->coef);
printf("第%d个指数:%d\n", i, pMove->expn);
pMove = pMove->next;
}
printf("\nList_length : %d\n\n", i);
}
void printPolynTerm(struct Lnode* L)
{
struct Lnode* pMove = L->next;
while(pMove != NULL)
{
if ( pMove->coef != 0)
{
if (pMove->coef > 0 && pMove != L->next )
{
printf("+");
}
if (pMove->expn == 0)
{
printf("%.2f", pMove->coef);
}
else
{
printf("%.2fx^%d", pMove->coef, pMove->expn);
}
}
pMove = pMove->next;
}
printf("\n");
}
顺序表C语言实现
#include <stdio.h>
#include <stdlib.h>
struct PolynomialTerm
{
float coef;
int expn;
};
int main()
{
int numTerms;
printf("请输入一元多项式的项数:");
scanf_s("%d", &numTerms);
struct PolynomialTerm* polynomial = (struct PolynomialTerm*)malloc(numTerms * sizeof(struct PolynomialTerm));
for (int i = 0; i < numTerms; i++)
{
printf("请输入第%d项的系数:", i + 1);
scanf_s("%f", &polynomial[i].coef);
printf("请输入第%d项的指数:", i + 1);
scanf_s("%d", &polynomial[i].expn);
}
int m = 0;
int n = 0;
int temp1=0;
float temp2 = 0;
for (m = 1; m < numTerms; m++)
{
for (n = m - 1; n >= 0 && polynomial[n].expn > polynomial[n+1].expn; n--)
{
temp1 = polynomial[n].expn;
temp2 = polynomial[n].coef;
polynomial[n].expn = polynomial[n + 1].expn;
polynomial[n].coef = polynomial[n + 1].coef;
polynomial[n + 1].expn = temp1;
polynomial[n + 1].coef = temp2;
}
}
printf("一元多项式的表达式为:");
for (int i = 0; i < numTerms; i++)
{
if(polynomial[i].coef != 0)
{
if (i != 0 && polynomial[i].coef > 0)
{
printf("+");
}
if (polynomial[i].expn == 0)
{
printf("%.2f", polynomial[i].coef);
}
else
{
printf("%.2fx^%d", polynomial[i].coef, polynomial[i].expn);
}
}
}
printf("\n");
free(polynomial);
return 0;
}
2.8.2 多项式的相加
单链表C语言实现
#include<stdio.h>
#include<stdlib.h>
struct Lnode {
float coef;
int expn;
struct Lnode* next;
};
struct Lnode* Create(struct Lnode* L, int n);
void printPolyn(struct Lnode* L);
void printPolynTerm(struct Lnode* L);
void AddPolyn(struct Lnode* Pa, struct Lnode* Pb);
int main()
{
struct Lnode* LNODE1 = NULL;
struct Lnode* LNODE2 = NULL;
struct Lnode* list1 = Create(LNODE1, 5);
printPolyn(list1);
printf("y1 = ");
printPolynTerm(list1);
struct Lnode* list2 = Create(LNODE2, 5);
printPolyn(list2);
printf("y2 = ");
printPolynTerm(list2);
AddPolyn(list1, list2);
printf("y3 = y1 + y2 = ");
printPolynTerm(list1);
return 0;
}
struct Lnode* Create(struct Lnode* L, int n)
{
struct Lnode* rear = L;
struct Lnode* s;
L = (struct Lnode*)malloc(sizeof(struct Lnode));
L->next = NULL;
float c;
int e;
struct Lnode* pre = L;
struct Lnode* p = L->next;
for (int i = 1; i <= n; i++)
{
s = (struct Lnode*)malloc(sizeof(struct Lnode));
printf("请输入第%d项的系数:", i);
scanf_s("%f", &c);
printf("请输入第%d项的指数:", i);
scanf_s("%d", &e);
s->coef = c;
s->expn = e;
struct Lnode* pre = L;
struct Lnode* p = L->next;
while (p && p->expn < s->expn)
{
pre = p;
p = p->next;
}
s->next = p;
pre->next = s;
}
printf("创建成功!\n");
return L;
}
void printPolyn(struct Lnode* L)
{
struct Lnode* pMove = L->next;
int i = 0;
for (i = 1; pMove != NULL; i++)
{
printf("第%d个系数:%f\n", i, pMove->coef);
printf("第%d个指数:%d\n", i, pMove->expn);
pMove = pMove->next;
}
printf("\nList_length : %d\n\n", i);
}
void printPolynTerm(struct Lnode* L)
{
struct Lnode* pMove = L->next;
int i = 0;
while(pMove != NULL)
{
if ( pMove->coef != 0)
{
if (pMove->coef > 0 && pMove != L->next)
{
printf("+");
}
if (pMove->expn == 0)
{
printf("%.2f", pMove->coef);
}
else
{
printf("%.2fx^%d", pMove->coef, pMove->expn);
}
}
pMove = pMove->next;
}
printf("\n");
}
void AddPolyn(struct Lnode* Pa, struct Lnode* Pb)
{
struct Lnode* p1 = Pa->next;
struct Lnode* p2 = Pb->next;
struct Lnode* p3 = Pa;
struct Lnode* r = p2;
int sum = 0;
while(p1 && p2)
{
if (p1->expn == p2->expn)
{
sum = p1->coef + p2->coef;
if (sum != 0)
{
p1->coef = sum;
p3->next = p1;
p3 = p1;
p1 = p1->next;
r = p2;
p2 = p2->next;
free(r);
}
else
{
r = p1;
p1 = p1->next;
free(r);
r = p2;
p2 = p2->next;
free(r);
}
}
else if (p1->expn < p2->expn)
{
p3->next = p1;
p3 = p1;
p1 = p1->next;
}
else
{
p3->next = p2;
p3 = p2;
p2 = p2->next;
}
}
if (p1)
{
p3->next = p1;
p3 = p1;
p1 = p1->next;
}
else
{
p3->next = p2;
p3 = p2;
p2 = p2->next;
}
free(Pb);
}
各算法时间复杂度和空间复杂度