#include “stdio.h”
#include “malloc.h”
#include “stdlib.h”
/*
用顺序表存储的稀疏多项式数据
*/
#define MAXSIZE 10 //顺序表可能达到的最大长度
#define OK 1;
#define ERROR 0;
#define OVERFLOW -2;
typedef int Status;
//-----顺序表的储存结构:
typedef struct{ //多项式非零项的定义
float coef; //系数
int expn; //指数
}Polynomial;
typedef struct{ //定义typedef结构体类型函数 其命名为SqList
Polynomial *elem; //储存空间的基地址
int length; //多项式中当前项的个数
}SqList; //顺序表的结构类型为SqList
//–顺序表的初始化
Status InitList(SqList &L) { //(不带&则此参数只在该函数被修改,函数外不发生变化,带&则在该函数外也发生了变化)
L.elem = new Polynomial[MAXSIZE]; //为顺序表分配一个大小为MAXSIZE的数组空间
if(!L.elem) exit OVERFLOW; //存储分配失败退出
L.length = 0; //空表长度为0
return OK; //Status类型函数返回值为任意值
}
//–顺序表取值
Status GetElem(SqList L , int i , Polynomial &e){
if (i<1 || i>L.length) return ERROR; //判断i值是否合理,如果不合理则返回ERROR
e=L.elem[i-1]; //elem[i-1]单元储存第i个数据元素
return OK;
//时间复杂度O(1)
}
//–顺序表的查找
int LocateElem(SqList L, Polynomial e){
//在顺序表中查找值为e的元素,返回其序号
int i;
for(i=0;i<L.length;i++)
if(L.elem[i] == e)
return i+1; //查找成功返回序号i+1
return 0; //查找失败,返回0
//平均时间复杂度O(n)
}
//–顺序表的插入
Status ListInsert(SqList &L,int i ,Polynomial e){
//在顺序表L中第i个位置之前插入新的元素e,i值的合法范围是 1<=i<=L.length+1
int j;
if( (i<1) || (i>L.length+1) ) //判断i值是否合法
return ERROR;
if(L.length==MAXSIZE) //判断储存空间是否已满
return ERROR;
for(j=L.length-1;j>=i-1;j–) //插入位置及之后的元素后移
L.elem[j+1] = L.elem[j];
L.elem[i-1]=e; //将新元素放在第i个位置
++L.length; //表长加1
return OK;
//移动次数:n-i+1
//平均时间复杂度为O(n)
}
//–顺序表的删除
Status ListDelete(SqList &L,int i){
int j;
//在顺序表中删除第i个元素,i的合法范围是1≤i≤L.length
if( (i<=0) || (i>L.length) ) // 如果i值不合法则返回ERROR
return ERROR;
for(j=i;j<=L.length+1;j++){
L.elem[j-1] = L.elem[j]; //被删除元素向前移
}
–L.length; //表长减一
return OK;
//平均时间复杂度O(n)
}
在顺序表的查找一栏
报错[Error] no match for ‘operator==’ (operand types are ‘Polynomial’ and ‘Polynomial’)