此處鏈表是加了表頭Head。這個程序有兩個頭文件poly.h和fatal.h,一個庫函數poly.c和一個測試函數testpoly.c
頭文件poly.h如下:
-
#ifndef Poly
-
typedef int Integer;
-
struct Node;
-
typedef struct Node *Polynomial;
-
typedef Polynomial Position;
-
Polynomial MakeEmpty(Polynomial L);
-
void DeleteList(Polynomial L);
-
void Insert(Integer Coefficient, Integer Exponent, Position P);
-
void PrintPoly(Polynomial L);
-
Position Header(Polynomial L);
-
Position First(Polynomial L);
-
int IsLast(Position P, Polynomial L);
-
int IsEmpty(Polynomial L);
-
Position Advance(Position P, Polynomial L);
-
Polynomial addpolynomial(const Polynomial poly1, const Polynomial poly2);
-
Polynomial subpolynomial(const Polynomial poly1, const Polynomial poly2);
-
//int Max(int A, int B);
-
Polynomial MultPolynomial(const Polynomial Poly1, const Polynomial Poly2);
-
void InsertSortPolynomial(const Polynomial Poly);
-
void CombSimPolynomial(const Polynomial Poly);
-
int CountPolynomial(const Polynomial Poly);
-
#endif // !1
頭文件fatal.h如下:
-
#include <stdio.h>
-
#include <stdlib.h>
-
#define Error( Str ) FatalError( Str )
-
#define FatalError( Str ) fprintf( stderr, "%s\n", Str ), exit( 1 )
所有的庫函數poly.c如下:
-
//引入頭文件
-
#include "poly.h"
-
#include "fatal.h"
-
//結構體,Coefficient表示多項式系數,Exponent表示多項式的指數
-
struct Node
-
{
-
int Coefficient;
-
int Exponent;
-
Position Next;
-
};
-
//初始化鏈表
-
Polynomial MakeEmpty(Polynomial L)
-
{
-
if (L != NULL)
-
DeleteList(L);
-
L = malloc(sizeof(struct Node));
-
if (L == NULL)
-
Error("Out of Space!!!");
-
L->Next = NULL;
-
return L;
-
}
-
//刪除鏈表
-
void DeleteList(Polynomial L)
-
{
-
Position P, Temp;
-
P = L->Next;
-
L->Next = NULL;
-
while (P != NULL)
-
{
-
Temp = P->Next;
-
free(P);
-
P = Temp;
-
}
-
free(L);
-
}
-
//在鏈表中插入值,在P指向的位置后插入
-
void Insert(Integer Coefficient, Integer Exponent, Position P)
-
{
-
Position TempCell;
-
TempCell = malloc(sizeof(struct Node));
-
if (TempCell == NULL)
-
FatalError("Out of Space!!!");
-
TempCell->Coefficient = Coefficient;
-
TempCell->Exponent = Exponent;
-
TempCell->Next = P->Next;
-
P->Next = TempCell;
-
}
-
-
//打印鏈表
-
void PrintPoly(Polynomial L)
-
{
-
Position P = First(L);
-
if (IsEmpty(L))
-
printf("Empty List!");
-
else
-
{
-
while (P->Next != NULL)
-
{
-
if (P->Exponent == 0)//如果是常數
-
{
-
if (P->Next->Coefficient < 0)//如果后面是負的,就不加"+"因為會自帶負號
-
printf("%d", P->Coefficient);
-
else
-
printf("%d+", P->Coefficient);//如果后面是正的,就加"+"
-
}
-
else
-
{
-
if (P->Next->Coefficient < 0)
-
printf("%dx^%d", P->Coefficient, P->Exponent);
-
else
-
printf("%dx^%d+", P->Coefficient, P->Exponent);
-
}
-
P = P->Next;
-
}
-
if (P->Exponent == 0)
-
printf("%d", P->Coefficient);
-
else
-
printf("%dx^%d", P->Coefficient, P->Exponent);
-
}
-
}
-
//獲取鏈表的表頭
-
Position Header(Polynomial L)
-
{
-
return L;
-
}
-
//獲取除去表頭后鏈表的第一個指針位置
-
Position First(Polynomial L)
-
{
-
return L->Next;
-
}
-
//判斷P是不是未指針,是,返回1,不是,返回0
-
int IsLast(Position P, Polynomial L)
-
{
-
return P->Next == NULL;
-
}
-
//判斷鏈表是不是空鏈表,如果是,返回1,不是,返回0
-
int IsEmpty(Polynomial L)
-
{
-
return L->Next == NULL;
-
}
-
//獲取鏈表中 P位置的后一個位置
-
Position Advance(Position P, Polynomial L)
-
{
-
return P->Next;
-
}
-
//實現兩個多項式相加,新建了一個鏈表Result作為存儲結果
-
Polynomial addpolynomial(const Polynomial poly1, const Polynomial poly2)
-
{
-
Polynomial Result;
-
Integer InsertCoe, InsertExp;
-
Position poly1_pos, poly2_pos, ResultPos;
-
-
poly1_pos = First(poly1); poly2_pos = First(poly2);
-
Result = MakeEmpty(NULL);
-
ResultPos = Header(Result);
-
while (poly1_pos != NULL&&poly2_pos != NULL)
-
{
-
if (poly1_pos->Exponent > poly2_pos->Exponent)
-
{
-
InsertCoe = poly1_pos->Coefficient;
-
InsertExp = poly1_pos->Exponent;
-
poly1_pos = Advance(poly1_pos,poly1);
-
}
-
else if(poly2_pos->Exponent > poly1_pos->Exponent)
-
{
-
InsertCoe = poly2_pos->Coefficient;
-
InsertExp = poly2_pos->Exponent;
-
poly2_pos = Advance(poly2_pos, poly2);
-
}
-
else
-
{
-
InsertCoe = poly1_pos->Coefficient+ poly2_pos->Coefficient;
-
InsertExp = poly1_pos->Exponent;
-
poly1_pos = Advance(poly1_pos, poly1);
-
poly2_pos = Advance(poly2_pos, poly2);
-
}
-
Insert(InsertCoe, InsertExp, ResultPos);
-
ResultPos = Advance(ResultPos,Result);
-
}
-
while (poly1_pos != NULL)
-
{
-
Insert(poly1_pos->Coefficient, poly1_pos->Exponent, ResultPos);
-
poly1_pos = Advance(poly1_pos,poly1);
-
ResultPos = Advance(ResultPos,Result);
-
}
-
while (poly2_pos != NULL)
-
{
-
Insert(poly2_pos->Coefficient, poly2_pos->Exponent, ResultPos);
-
poly2_pos = Advance(poly2_pos, poly2);
-
ResultPos = Advance(ResultPos, Result);
-
}
-
return Result;
-
}
-
//實現兩個多項式相減,新建了一個鏈表Result作為存儲結果
-
Polynomial subpolynomial(const Polynomial poly1, const Polynomial poly2)
-
{
-
Polynomial Result;
-
Integer InsertCoe, InsertExp;
-
Position poly1_pos, poly2_pos, ResultPos;
-
-
poly1_pos = First(poly1); poly2_pos = First(poly2);
-
Result = MakeEmpty(NULL);
-
ResultPos = Header(Result);
-
while (poly1_pos != NULL&&poly2_pos != NULL)
-
{
-
if (poly1_pos->Exponent > poly2_pos->Exponent)
-
{
-
InsertCoe = poly1_pos->Coefficient;
-
InsertExp = poly1_pos->Exponent;
-
poly1_pos = Advance(poly1_pos, poly1);
-
}
-
else if (poly2_pos->Exponent > poly1_pos->Exponent)
-
{
-
InsertCoe = -poly2_pos->Coefficient;
-
InsertExp = poly2_pos->Exponent;
-
poly2_pos = Advance(poly2_pos, poly2);
-
}
-
else
-
{
-
InsertCoe = poly1_pos->Coefficient - poly2_pos->Coefficient;
-
InsertExp = poly1_pos->Exponent;
-
poly1_pos = Advance(poly1_pos, poly1);
-
poly2_pos = Advance(poly2_pos, poly2);
-
}
-
Insert(InsertCoe, InsertExp, ResultPos);
-
ResultPos = Advance(ResultPos, Result);
-
}
-
while (poly1_pos != NULL)
-
{
-
Insert(poly1_pos->Coefficient, poly1_pos->Exponent, ResultPos);
-
poly1_pos = Advance(poly1_pos, poly1);
-
ResultPos = Advance(ResultPos, Result);
-
}
-
while (poly2_pos != NULL)
-
{
-
Insert(-poly2_pos->Coefficient, poly2_pos->Exponent, ResultPos);
-
poly2_pos = Advance(poly2_pos, poly2);
-
ResultPos = Advance(ResultPos, Result);
-
}
-
return Result;
-
}
-
//插入排序算法實現鏈表中由大到小排序
-
void InsertSortPolynomial(const Polynomial Poly)
-
{
-
//插入排序算法
-
Position head = Poly;
-
Position first; /*為原鏈表剩下用於直接插入排序的節點頭指針*/
-
Position t; /*臨時指針變量:插入節點*/
-
Position p=head; /*臨時指針變量,初始化*/
-
Position q; /*臨時指針變量*/
-
if (Advance(head, Poly) == NULL)
-
printf("Empty List!");//判斷是不是空鏈表
-
else
-
{
-
first = head->Next->Next; /*原鏈表剩下用於直接插入排序的節點鏈表:可根據圖12來理解。*/
-
head->Next->Next = NULL; /*只含有一個節點的鏈表的有序鏈表:可根據圖11來理解。*/
-
while (first != NULL) /*遍歷剩下無序的鏈表*/
-
{
-
/*注意:這里for語句就是體現直接插入排序思想的地方*/
-
-
for (t = first, q = head->Next; ((q != NULL) && (q->Exponent > t->Exponent)); p = q, q = q->Next); /*無序節點在有序鏈表中找插入的位置*/
-
/*退出for循環,就是找到了插入的位置*/
-
first = first->Next; /*無序鏈表中的節點離開,以便它插入到有序鏈表中。*/
-
if (q == head->Next)
-
head->Next = t;
-
else p->Next = t;
-
t->Next = q; /*完成插入動作*/
-
/*first = first->next;*/
-
}
-
}
-
}
-
//將排序后的鏈表合並同類項
-
void CombSimPolynomial(const Polynomial Poly)
-
{
-
//輸入鏈表為已經排好序的鏈表
-
Position p = Poly->Next;
-
Position q;
-
if (p != NULL)//防止鏈表只有一個元素
-
{
-
q = p->Next;
-
while (q != NULL)//搜索鏈表結束標志
-
{
-
if (p->Exponent == q->Exponent)
-
{
-
p->Coefficient += q->Coefficient;
-
p->Next = q->Next;
-
q = q->Next;
-
}
-
else
-
{
-
p = q;
-
q = q->Next;
-
}
-
}
-
}
-
-
-
}
-
//實現鏈表的相乘
-
Polynomial MultPolynomial(const Polynomial Poly1, const Polynomial Poly2)
-
{
-
Polynomial MultResult = MakeEmpty(NULL);//對結果進行初始化
-
Position p1, p2, p;
-
p1 = First(Poly1);
-
p2 = First(Poly2);
-
p = Header(MultResult);
-
Integer Coe, Exp;
-
if (IsEmpty(Poly1) || IsEmpty(Poly2))
-
Error("EmptyList!!!");
-
while (p1 != NULL)
-
{
-
p2 = First(Poly2);
-
while (p2 != NULL)
-
{
-
Coe = p1->Coefficient*p2->Coefficient;
-
Exp = p1->Exponent + p2->Exponent;
-
Insert(Coe, Exp, p);
-
p = p->Next;
-
p2 = p2->Next;
-
}
-
p1 = p1->Next;
-
}
-
InsertSortPolynomial(MultResult);//給多項式乘積排序
-
CombSimPolynomial(MultResult);//給多項式合並同類項
-
return MultResult;
-
}
-
-
//判斷鏈表中有多少個元素
-
int CountPolynomial(const Polynomial Poly)
-
{
-
int i = 0;
-
Position P = First(Poly);
-
while (P != NULL)
-
{
-
i += 1;
-
P = Advance(P,Poly);
-
}
-
return i;
-
}
測試函數testpoly.c
-
#include "poly.h"
-
#include<stdio.h>
-
#include<malloc.h>
-
int main()
-
{
-
Polynomial L1;
-
L1=MakeEmpty(NULL);
-
Insert(3, 5, L1);
-
Insert(-4, 8, Advance(L1,L1));
-
Insert(2, 3, Advance(Advance(L1, L1),L1));
-
Insert(2, 4, Advance(Advance(Advance(L1, L1), L1),L1));
-
PrintPoly(L1);
-
printf("\n");
-
-
Polynomial L2,Result;
-
L2 = MakeEmpty(NULL);
-
Insert(3, 1, L2);
-
//Insert(4, 5, Advance(L2, L2));
-
//Insert(2, 4, Advance(Advance(L2, L2), L2));
-
PrintPoly(L2);
-
printf("\n乘積結果:");
-
Result = MultPolynomial(L1, L2);
-
PrintPoly(Result);
-
////Result=addpolynomial(L1, L2);
-
////PrintPoly(Result);
-
////printf("\n");
-
//Result = subpolynomial(L1, L2);
-
//PrintPoly(Result);
-
printf("\n");
-
}