【C++】线性表——顺序表V1.0(源码提供)
2010-01-17 11:13
375 查看
我把顺序表的源代码贴到这里(只提供顺序表类(seqlist)的定义的头文件(.h),其他.cpp测试文件用户可自行编写、调用、测试),假如你在顺序表上遇到了问题,我的blog也有幸被你Google或者百度到了,那希望我的这个顺序表代码能够帮助你。假如你发现代码中还存在一些问题,请及时留言给我,大家共同提高。
这个顺序表包含大多数常用的成员函数,以便于用户轻松调用。
这个顺序表包含大多数常用的成员函数,以便于用户轻松调用。
/*线性表——顺序表V1.0 Coded by Arthur Yoo*/ #ifndef SEQLIST_H #define SEQLIST_H using namespace std; const int maxlen=1000; template <class type> class seqlist { public: seqlist();//构造函数(无参数) seqlist(int x);//构造函数(含X个元素,且缺省值都为0) ~seqlist();//析构函数 int Seqlen() const;//返回线性表的长度 void Showlist() const;//显示线性表 void Set(const type &a,int x);//将第X位的值设为a void Insertbef(const type &a,int x);//在X号元素前插入一个元素,使之成为第X号元素 void Insertaft(const type &a,int x);//在X号元素后插入一个元素,使之成为第X+1号元素 type Get(int x) const;//返回第X号元素值 int Locate(type &x) const;//返回值为X的元素序号 void Delete(int x);//删除第X号元素 void Clear();//清空线性表 int Isempty() const;//判断是不是空表 void Nonrepeat();//去除顺序表中重复的元素 void AddOrdered();//使得顺序表按值由小到大排序 void DeleteBetween(const type &a,const type &b);//删去经顺序排序后(已经由AddOrdered()函数排序)顺序表中元素值介于a和b的元素 private: type data[maxlen]; int len;//元素个数 }; #endif template <class type> seqlist<type>::seqlist()//构造函数(无参数) { len=0; } template <class type> seqlist<type>::seqlist(int x)//构造函数(含X个元素,且缺省值都为0) { int i; for(i=0;i<x;i++) data[i]=0; len=x; } template <class type> seqlist<type>::~seqlist()//析构函数 { } template <class type> int seqlist<type>::Seqlen() const//返回线性表的长度 { return len; } template <class type> void seqlist<type>::Showlist() const//显示线性表 { if(len==0) cout<<"此线性表为空!"<<endl; else { int i; cout<<"["<<1<<"]"<<data[0]; for(i=1;i<len;i++) cout<<"->"<<"["<<i+1<<"]"<<data[i]; cout<<endl; } } template <class type> void seqlist<type>::Insertbef(const type &a,int x)//在X号元素前插入一个元素,使之成为第X号元素 { if(len==maxlen) { cout<<"此表元素已满,插入失败。"<<endl; } if(x<0 || x>=len) { cout<<"插入序号输入有误,插入失败。"<<endl; } else { int i;len++; for(i=len-1;i>x;i--) { data[i]=data[i-1]; } data[x]=a; } } template <class type> void seqlist<type>::Set(const type &a,int x) { data[x]=a; } template <class type> void seqlist<type>::Insertaft(const type &a,int x)//在X号元素后插入一个元素,使之成为第X+1号元素 { if(len==maxlen) { cout<<"此表元素已满,插入失败。"<<endl; } if(x<0 || x>=len) { cout<<"插入序号输入有误,插入失败。"<<endl; } else { int i;len++; for(i=len-1;i>x+1;i--) { data[i]=data[i+1]; } data[x+1]=a; } } template <class type> type seqlist<type>::Get(int x) const//返回第X号元素值 { return data[x]; } template <class type> int seqlist<type>::Locate(type &x) const//返回值为X的元素序号 { int i; for(i=0;i<len;i++) { if(data[i]==x) return i; } return -1; } template <class type> void seqlist<type>::Delete(int x)//删除第X号元素 { if(x<0 || x>=len) { cout<<"插入序号输入有误,插入失败。"<<endl; } else { int i; for(i=x;i<len;i++) { data[i]=data[i+1]; } len--; } } template <class type> void seqlist<type>::Clear()//清空线性表 { len=0; } template <class type> int seqlist<type>::Isempty() const//判断是不是空表 { if(len==0) return 1; else return 0; } template <class type> void seqlist<type>::Nonrepeat() { int x,y; type a; for(x=0;x<len-1;x++) { a=this->Get(x); for(y=x+1;y<len;y++) { if(a==this->Get(y)) { this->Delete(y); y=y-1; } } } } template <class type> void seqlist<type>::AddOrdered() { type temp; int i,j,flag=1; for(i=1; i<len&&flag==1; i++) { flag=0; for(j=0;j<len-i;j++) //采用冒泡排序 { if(this->Get(j)>this->Get(j+1)) { flag=1; temp=this->Get(j); this->Set(this->Get(j+1),j); this->Set(temp,j+1); } } } } template <class type> void seqlist<type>::DeleteBetween(const type &a,const type &b) { int j; if(a>=b || this->Isempty()) cout<<"删除有误。"<<endl; else { for(j=0; j<len; j++) { if(this->Get(j)>=a && this->Get(j)<=b) { this->Delete(j); j=j-1; } } } }
相关文章推荐
- 【C++】线性表——单向链表V1.0(源码提供)
- 【C++】队列——顺序表实现V1.0(源码提供)
- 【C++】栈——顺序表实现V1.0(源码提供)
- CYQ.IISLogViewer 一款IIS 日志分析工具 V1.0 发布[提供源码]
- C/C++线性表---顺序表算法全解析
- CYQ.IISLogViewer 一款IIS 日志分析工具 V1.0 发布[提供源码]
- CYQ.IISLogViewer 一款IIS 日志分析工具 V1.0 发布[提供源码]
- 提供c/c++源码的网址
- 提供TURN Client的源码库,不过是C++的
- 提供音频变速不变调组件或源码(C++)
- CYQ.IISLogViewer 一款IIS 日志分析工具 V1.0 发布[提供源码]
- 将C++源码封装为dll,并提供接口给调用
- CYQ.Data 数据框架 V3.5 开源版本发布(源码提供下载)
- 特殊线性表(一)栈的C++实现
- Mysql数据库操作类( 1127版,提供源码下载 )
- c/c++字符串操作函数源码解析
- Qing1.0快速数据访问框架(源码)
- 简单实用审批流程组件 - 提供源码
- 解析DirectShow SDK提供的播放器例子源码PlayWnd
- 有的时候总是需要过滤一些没用的东西,歌词过滤工具V1.0的实现(提供下载)