#include #include #include void Exception(int Condition, char *ErrorMsg){ if(Condition){cerr << ErrorMsg << endl; abort();} } template class SeqList{ private: ElemType *elem; //顺序表存储数组,存放实际的数据结点 int length; //顺序表中的结点个数,亦称表的长度 int MaxSize; //顺序表的最大可能长度 public: SeqList(int InitSize); //构造函数 ~SeqList(); //析构函数 void Clear(); //清空顺序表 bool IsEmpty() {return(length==0);}//表为空返回TRUE,否则返回FALSE bool IsFull() {return(length==MaxSize);}//表为满返回TRUE,否则返回FALSE int Length() ; //表的长度 ElemType Get(int i) ; //返回第i个结点的值 int Next(int i) ; //若第i个结点的直接后继结点存在,返回其下标,否则返回0 int Prior(int i) ;//若第i个结点的直接前驱结点存在,返回其下标,否则返回0 int Find(ElemType e); //返回值等于e的结点的序号,无则返回0 bool Insert(int i, ElemType e);//在第i个位置上插入新的结点(值为e), //并使原来的第i个结点至最后一个结点的序号依次加1。插入成功返回1,否则为false bool Delete(int i, ElemType &e); //若第i个结点存在,删除并将其值放人e, //若i非法,则删除失败,返回false。 void inputPolynomial(void); void Output(ostream& out) ; }; template SeqList::SeqList(int InitSize){ //构造函数 if(InitSize >0){ elem = new ElemType[InitSize]; //申请连续空间,返回空间首地址 Exception(!elem, "there is no space in memory"); //若申请空间失败,则程序中断 length = 0; MaxSize = InitSize - 1; } } template SeqList::~SeqList(){ //析构函数 delete []elem; //释放占用的连续空间 } template void SeqList::Clear(){ //清空顺序表 length = 0; //声明顺序表List中结点个数为零 } template int SeqList::Length() { //返回表的长度 return length; } template ElemType SeqList::Get(int i) { //读出第i个结点的值并返回 Exception((i<1)||(i>length), "Out of bounds access."); //越界、非法 return elem[i]; } template int SeqList::Next(int i) { //若第i个结点的直接后继结点存在,返回其下标 Exception((i<1)||(i>length-1), "Out of bounds access."); return i+1; //返回直接后继结点的下标 } template int SeqList::Prior(int i) { //若第i个结点的直接前驱结点存在,返回其下标 Exception((i<=1)||(i>length), "Out of bounds access."); //无直接前驱结点,错 return i-1; //返回直接前驱结点的下标 } template int SeqList::Find(ElemType e){ //按照下标从大到小顺序查找值为e的数组结点的下标并将其返回。 elem[0] = e; //elem[0]做哨兵用,保证即使查找失败,也可以在哨兵位上能找到值e。 int i = length; //初始位置为尾结点所在下标 while(elem[i] != e) i--; //不等时继续向前比较,找到返回结点下标,否则返回0 return i; } template bool SeqList::Insert(int i, ElemType e){ //在位置i上插入一个值为e的结点,原来的第i个结点变为第i+1个结点, //其余后继结点序号同样加1,插入成功返回true。 Exception((i<1)||(i>length+1), "i is not correct."); //插入位置i不合法 Exception(MaxSize == length, "no space for new item."); //存储空间已经满了,无法插入 //从尾结点起到第i个结点逐个向后移动一个位置 for(int j = length; j >= i; j--) elem[j+1] = elem[j]; elem[i] = e; //将要插入的结点值放人第i个结点的位置 length++; //顺序表结点个数加1 return true; //插入成功返回true } template bool SeqList::Delete(int i, ElemType &e){ //若第i个结点存在,删除并将其值放人e,若i非法,删除失败 Exception((i<1)||(i>length), "i is not correct."); e = elem[i]; //将第i个结点值首先读出,用于返回 for(int j = i; j < length; j++) elem[j] = elem[j + 1]; //第i+1个结点到尾结点逐个前移 length--; //顺序表长度减1 return true; //删除成功返回true } template void SeqList::Output(ostream& out) { int j = 0; if(length <= 0){ out << "The List is empty. " << endl; return; } else for(int i =1; i <= length ; i++){ out << setw(3) << Get(i) << ' '; j++; if(j % 15 == 0) out << endl; } out << endl; return; } // overload << template ostream& operator<<(ostream& out, SeqList& x) {x.Output(out); return out;} void main(void) { //按实际情况给出 }