顺序表应用实例
2012-12-10 21:39
120 查看
#include "stdafx.h"
template <class T>
class arrList
{ // 顺序表,向量
private: // 线性表的取值类型和取值空间
T *aList ; // 私有变量,存储顺序表的实例
int maxSize; // 私有变量,顺序表实例的最大长度
int curLen; // 私有变量,顺序表实例的当前长度
int position; // 私有变量,当前处理位置
public: // 顺序表的运算集
arrList(const int size)
{ // 创建一个新的顺序表,参数为表实例的最大长度
maxSize = size; aList = new T[maxSize]; curLen = position = 0;
}
~arrList()
{ // 析构函数,用于消除该表实例
delete [] aList;
cout<<"析构函数"<<endl;
}
void clear()
{ // 将顺序表存储的内容清除,成为空表
delete [] aList; curLen = position = 0;
aList = new T[maxSize];
}
int length(); // 返回此顺序表的当前实际长度
bool append(const T value); // 在表尾添加一个元素value,表的长度增1
bool insert(const int p, const T value); // 在位置p上插入一个元素value,表的长度增1
bool delete1(const int p); // 删除位置p上的元素,表的长度减 1
bool setValue(const int p, const T value); // 用value修改位置p的元素值
bool getValue(const int p, T& value); // 把位置p的元素值返回到变量value中
bool getPos(int & p, const T value); // 查找值为value的元素,并返回第1次出现的位置
void output();
};
template <class T>
int arrList<T>::length()
{
return curLen;
}
template <class T>
bool arrList<T>::append(const T value)
{
aList[curLen++]=value;
return true;
}
template <class T>
bool arrList<T>::insert(const int p, const T value) // 在位置p上插入一个元素value,表的长度增1
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>=maxSize)
cout<<"The position is illegal!"<<endl;
for(int i=curLen-1;i>=p;i--)
aList[i+1]=aList[i];
curLen++;
aList[p]=value;
return true;
}
template <class T>
bool arrList<T>::delete1(const int p) // 删除位置p上的元素,表的长度减 1
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>=maxSize)
cout<<"The position is illegal!"<<endl;
for(int i=p;i<curLen-1;i++)
aList[i]=aList[i+1];
curLen--;
return true;
}
template <class T> bool arrList<T>::setValue(const int p, const T value) // 用value修改位置p的元素值
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>maxSize)
cout<<"The position is illegal!"<<endl;
aList[p]=value;
return true;
}
template <class T>
bool arrList<T>::getValue(const int p, T& value) // 把位置p的元素值返回到变量value中
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>maxSize)
cout<<"The position is illegal!"<<endl;
value=aList[p];
return true;
}
template <class T>
bool arrList<T>::getPos(int & p,const T value) // 查找值为value的元素,并返回第1次出现的位置
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
for(int i=0;i<curLen;i++)
{
if(aList[i]==value)
{
p=i;
break;
}
}
return true;
}
template <class T>
void arrList<T>::output() //输出顺序表函数
{
for(int i=0;i<length();i++)
{
T value;
getValue(i,value);
cout<<value<<" ";
}
cout<<endl;
}
template <class T>
class arrList
{ // 顺序表,向量
private: // 线性表的取值类型和取值空间
T *aList ; // 私有变量,存储顺序表的实例
int maxSize; // 私有变量,顺序表实例的最大长度
int curLen; // 私有变量,顺序表实例的当前长度
int position; // 私有变量,当前处理位置
public: // 顺序表的运算集
arrList(const int size)
{ // 创建一个新的顺序表,参数为表实例的最大长度
maxSize = size; aList = new T[maxSize]; curLen = position = 0;
}
~arrList()
{ // 析构函数,用于消除该表实例
delete [] aList;
cout<<"析构函数"<<endl;
}
void clear()
{ // 将顺序表存储的内容清除,成为空表
delete [] aList; curLen = position = 0;
aList = new T[maxSize];
}
int length(); // 返回此顺序表的当前实际长度
bool append(const T value); // 在表尾添加一个元素value,表的长度增1
bool insert(const int p, const T value); // 在位置p上插入一个元素value,表的长度增1
bool delete1(const int p); // 删除位置p上的元素,表的长度减 1
bool setValue(const int p, const T value); // 用value修改位置p的元素值
bool getValue(const int p, T& value); // 把位置p的元素值返回到变量value中
bool getPos(int & p, const T value); // 查找值为value的元素,并返回第1次出现的位置
void output();
};
template <class T>
int arrList<T>::length()
{
return curLen;
}
template <class T>
bool arrList<T>::append(const T value)
{
aList[curLen++]=value;
return true;
}
template <class T>
bool arrList<T>::insert(const int p, const T value) // 在位置p上插入一个元素value,表的长度增1
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>=maxSize)
cout<<"The position is illegal!"<<endl;
for(int i=curLen-1;i>=p;i--)
aList[i+1]=aList[i];
curLen++;
aList[p]=value;
return true;
}
template <class T>
bool arrList<T>::delete1(const int p) // 删除位置p上的元素,表的长度减 1
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>=maxSize)
cout<<"The position is illegal!"<<endl;
for(int i=p;i<curLen-1;i++)
aList[i]=aList[i+1];
curLen--;
return true;
}
template <class T> bool arrList<T>::setValue(const int p, const T value) // 用value修改位置p的元素值
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>maxSize)
cout<<"The position is illegal!"<<endl;
aList[p]=value;
return true;
}
template <class T>
bool arrList<T>::getValue(const int p, T& value) // 把位置p的元素值返回到变量value中
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
if(p<0||p>maxSize)
cout<<"The position is illegal!"<<endl;
value=aList[p];
return true;
}
template <class T>
bool arrList<T>::getPos(int & p,const T value) // 查找值为value的元素,并返回第1次出现的位置
{
if(curLen>=maxSize)
cout<<"The list is overflow!"<<endl;
for(int i=0;i<curLen;i++)
{
if(aList[i]==value)
{
p=i;
break;
}
}
return true;
}
template <class T>
void arrList<T>::output() //输出顺序表函数
{
for(int i=0;i<length();i++)
{
T value;
getValue(i,value);
cout<<value<<" ";
}
cout<<endl;
}
相关文章推荐
- 顺序栈,C++基本实例应用
- 顺序表的应用实例
- C语言 算数表达式求值(顺序栈应用实例)
- JQuery应用实例学习(强烈推荐)(转)
- java 输入3个数a,b,c,按大小顺序输出的实例讲解
- 决策树和随机决策森林基本原理和应用实例
- js原生设计模式——7原型模式之new+call(this)组合应用再探讨实例
- 大话西游之Office应用实例系列! <3>
- Silverlight实例教程 - Out of Browser的自定义应用 推荐
- javascript图片切换综合实例(循环切换、顺序切换)
- 红外遥控原理及应用实例
- CppCMS应用启动顺序问题
- 数据库设计三大范式应用实例剖析
- gsoap CXF2.7.5 ssh vc++ webservice应用实例(二)
- osworkflow跨步骤应用实例
- 一个反射方法应用实例
- JAVA类初始化及实例初始化时内部的执行顺序
- 华为交换机STP简单应用实例
- Struts2使用Interceptor实现权限控制的应用实例详解
- 顺序表应用5:有序顺序表归并