数据结构之顺序表C++实现
来源:互联网 发布:c语言二叉树非递归遍历 编辑:程序博客网 时间:2024/06/05 06:49
/*----------sqlist.h----------*/
#ifndef SQLIST_H
#define SQLIST_H
//起始下标为 1
//最大下标为 SIZE
const int SIZE(10);
//此处修改默认表长大小
template <typename Type> class Sqlist{
public:
Sqlist(int maxsize=SIZE)
{
while(maxsize<1){
cout << "设置最大表长有误,请重新设置->: " ;
cin >> maxsize;
}
MaxSize=maxsize;
data=new Type[MaxSize+1]; // data[MaxSize] 为表满状态
last=0; //空表状态
}
~Sqlist()
{
delete [] data;
}
void Clear() //清空顺序表
{
last=0;
}
bool Empty() //判空
{
if(last==0)
return true;
else
return false;
}
status push_back(const Type &value) //压入(后插)
{
if(last==MaxSize){
cerr << "顺序表已满,操作失败! " << endl;
return 0;
}else {
data[last+1]=value;
last++;
return 1;
}
}
void Display() //显示
{
for(int i=1;i<=last;i++)
cout << data[i] << " ";
cout << endl;
}
Type GetElem(int i) //取元素
{
if(last==0){
cerr << "顺序表为空,操作失败! " ;
goto end;
}
if(i<1||i>last){
cerr << "下标越界,请检查->: ";
goto end;
}
return data[i];
end:cout << endl;
}
int Locate(const Type &value) //定位查找
{
if(last==0){
cerr << "顺序表为空,操作失败!" << endl;
return 0;
}
for(int i=1;i<=last;i++)
if(data[i]==value)
return i;
cerr << "顺序表中没有此元素:" << value << " !" <<endl;
return 0;
}
status Insert(int i,const Type &value) //插入操作
{
if(last==MaxSize){
cerr << "顺序表已满,操作失败!" << endl;
return 0;
}
if(i<1||i>last+1){
cerr << "插入位置有误,请检查: " << endl;
return 0;
}
for(int j=last;j>=i;j--)
data[j+1]=data[j];
data[i]=value;
last++;
}
status Delete(int i) //删除操作
{
if(last==0){
cerr << "顺序表为空,操作失败! " << endl;
return 0;
}
if(i<1||i>last){
cerr << "下标越界,请检查:" << endl;
return 0;
}
for(int j=i;j<last;j++)
data[j]=data[j+1];
last--;
return 1;
}
int length()
{
return last;
}
int size()
{
return last;
}
status Replace(int i,const Type &value) //替换
{
if(last==0 || i<1 || i > last){
cerr << "下标越界,操作失败。" << endl;
return 0;
}else{
data[i]=value;
return 1;
}
}
Type operator [](int i) //重载 []
{
if(i<1||i>last||last==0){
cerr << "下标越界,请检查! " << endl;
exit(0);
}else
return data[i];
}
private:
Type *data;
int last;
int MaxSize;
};
#endif
//本人正在自学数据结构,欢迎老师批评指正,谢谢关注
- 数据结构C语言实现之顺序表
- 简单数据结构之顺序表(C实现)
- 数据结构---顺序表c实现
- 数据结构之---c语言实现线性表的顺序表
- 一、数据结构基础之顺序表C语言实现
- C语言数据结构之:顺序表的实现
- 数据结构c语言实现之顺序线性表
- 数据结构之---C语言实现顺序表查找(一)
- 数据结构C语言实现之顺序栈
- 数据结构之顺序表实现
- 数据结构之顺序表实现
- 【数据结构C实现】1.顺序表实现
- C语言实现数据结构中的顺序表
- 数据结构(C++)顺序表的实现
- 数据结构的C实现_顺序表
- 数据结构---队列顺序表c实现
- 数据结构---栈顺序表c实现
- c语言实现数据结构顺序表源代码
- C#制作CAB压缩包压缩解压类
- Funtion call conventions
- 如何使用 Net Use 从内部网中的电脑上Copy 文件?
- JMS OutLine
- 附加数据库失败,无法升级数据库,因为它是只读的
- 数据结构之顺序表C++实现
- PHP码农进化史6-PHPER地图 【转】
- 优化数据库的思想:
- Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’
- Linux网桥的实现分析
- 实体过滤的应用
- 数据库创建
- 关于flex的事件分发的一点期望
- Blend基础-项目与层次