程序:第02章:线性表.doc

  1. 1、本文档共1页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
程序:第02章:线性表

第2章 线性表 程序2.1 线性表类 #include template class LinearList public: virtual bool IsEmpty const 0; virtual int Length const 0; virtual bool Find int i,T x const 0; virtual int Search T x const 0; virtual bool Insert int i,T x 0; virtual bool Delete int i 0; virtual bool Update int i,T x 0; virtual void Output ostream out const 0; protected: int n; //线性表的长度 ;#include linearlist.h template class SeqList:public LinearList public: SeqList int mSize ; ~SeqList Delete [] elements ; bool IsEmpty const; int Length const; bool Find int i,T x const; int Search T x const; bool Insert int i,T x ; bool Delete int i ; bool Update int i,T x ; void Output ostream out const ; private: int maxLength; //顺序表的最大长度 T *elements; //动态一维数组的指针 ; template SeqList ::SeqList int mSize maxLength mSize; elements new T[maxLength]; //动态分配顺序表的存储空间 0; template bool SeqList ::IsEmpty const return n 0; template int SeqList ::Length const return n; template bool SeqList ::Find int i,T x const if i 0 || i n-1 //对i进行越界检查 cout Out of Bounds endl; return false; x elements[i]; //通过引用参数x返回下标为i的元素 return true; template int SeqList ::Search T x const for int j 0;j n;j++ if elements[j] x return j; //从头开始搜索值为x的元素 return -1; template bool SeqList ::Insert int i,T x if i -1 || i n-1 cout Out Of Bounds endl; return false; if n maxLength //上溢出检查 cout OverFlow endl; return false; for int j n-1;j i;j-- elements[j+1] elements[j]; //从后往前逐个后移元素 elements[i+1] x; //将x插入下标为i的元素后面 n++; return true; template bool SeqList ::Delete int i if !n //下溢出检查 cout UnderFlow endl; return false; if i 0 || i n-1 cout Out Of Bounds endl; return false; for int j i+1;j n;j++ elements[j-1] elements[j]; //从前往后逐个前移元素 --; return true; template bool SeqList ::Update int i,T x if i 0 || i n-1 cout Out Of Bounds endl; return false; elements[i] x; //将下标为i的元素值修改为x return true; template void SeqList ::Output ostream out const for int i 0; i n; i++ out elements[i] ; out

文档评论(0)

sh4125733 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档