[C++]数据结构:公式化描述的线性表LinearList的创建与使用
2012-11-21 20:56
549 查看
//基于公式的类LinearList #include <new.h> #include<exception> #include <iostream> using namespace std; template<class T> class LinearList{ public: LinearList(int MaxListSize=10); //构造函数,默认最大值为10 ~LinearList(){delete []element;} //析构函数 bool IsEmpty()const{return length==0;} int Length()const{return length;} bool Find(int k,T&x)const; //返回第k个元素到x中 int Search(const T&x)const; //返回x所在的位置 LinearList<T>& Delete(int k,T& x); //删除第k个元素并把它返回到x中 LinearList<T>& Insert(int k,const T& x); //在第k个元素之后插入x void Output(ostream& out)const; private: int length; int MaxSize; T *element; }; class OutOfBounds{ public: OutOfBounds(){ cout<<"Out Of Bounds!"<<endl; } }; //内存不足的异常类 class NoMem{ public: NoMem(){ cout<<"No Memory!"<<endl; } }; //使new引发NoMem异常而不是xalloc异常 //如果要恢复原始行为可以做以下调用 //_set_new_handler(Old_Handler); int my_new_handler(size_t size){ throw NoMem(); } //构造函数 //基于公式的线性表 template<class T> LinearList<T>::LinearList(int MaxListSize){ MaxSize = MaxListSize; element = new T[MaxSize]; length = 0; } //获取 //把第k个元素取到x中 //如果不存在第k个元素返回false //如果存在返回true template<class T> bool LinearList<T>::Find(int k,T& x)const{ if(k<1||k>length) return false; x=element[k-1]; return true; } //查找 //查找x如果找到返回位置 //如果x不在表中返回0 //注意:返回的不是所在数组的下标 template<class T> int LinearList<T>::Search(const T& x)const{ for(int i =0;i<length;i++){ if(element[i]==x) return ++i; } return 0 ; } //删除 //把第k个元素放到x中,然后删除第k个元素 //如果不存在第k个元素则引发异常OutOfBounds template<class T> LinearList<T>& LinearList<T>::Delete(int k,T& x){ if(Find(k,x)){ for(int i = k;i<length;i++){ element[i-1]=element[i]; } length--; return *this; }else{ throw OutOfBounds(); } } //插入 //在第k个元素之后插入x //如果不存在则引发异常OutOfBounds //如果表已经满了则引发异常NoMem template<class T> LinearList<T>& LinearList<T>::Insert(int k,const T& x){ if(k<0||k>length){ throw OutOfBounds(); } if(length==MaxSize){ throw NoMem(); } //后面的元素整体移位 for(int i = length-1;i>=k;i--){ element[i+1]=element[i]; } element[k]=x; length++; return *this; } //重载操作符 //输出到cout输出流中 template<class T> void LinearList<T>::Output(ostream& out)const{ for(int i = 0;i<length;i++){ out<<element[i]<<" "; } } template<class T> ostream& operator<<(ostream& out,const LinearList<T>& x){ x.Output(out); return out; } int main(){ _set_new_handler(my_new_handler); try{ LinearList<int>myList(10); cout<<"myList : "<<myList<<endl; cout<<"Length = "<<myList.Length()<<endl; cout<<"IsEmpty = "<<myList.IsEmpty()<<endl; myList.Insert(0,5); myList.Insert(1,2); myList.Insert(2,0); cout<<"myList : "<<myList<<endl; cout<<"Length = "<<myList.Length()<<endl; cout<<"IsEmpty = "<<myList.IsEmpty()<<endl; int f ; myList.Find(1,f);//把第一个位置的元素赋值给z了 cout<<"First is "<<f<<endl; cout<<"Length = "<<myList.Length()<<endl; myList.Delete(1,f); cout<<"Delete is "<<f<<endl; cout<<"myList : "<<myList<<endl; cout<<"Length = "<<myList.Length()<<endl; cout<<"IsEmpty = "<<myList.IsEmpty()<<endl; }catch(exception e){ cout<<"Hey!An exception has occurred!"; } return 0; }
相关文章推荐
- [C++]数据结构:公式化描述的堆栈Stack的简单创建与使用
- 线性表LinearList的创建与使用
- [C++]数据结构:线性表的公式化描述和链式描述的结构特点与基本操作
- [C++]数据结构:公式化描述的队列Queue类
- C++存储数据结构List、CArray、Vector和Map使用分析
- c++实现List数据结构
- [FAQ] 数据结构用 C 和 C++ 描述有什么区别
- 数据结构之线性表(linear_list)一
- 【C++学习笔记】使用new创建动态结构
- 使用LinkedList模拟一个堆栈或者队列数据结构
- [C++]数据结构实验04:使用堆栈进行简单的四则运算
- 使用C++实现二叉搜索树的数据结构
- Entity Framework(三):使用特性(数据注解)创建表结构
- 【黑马Android】(04)数据库的创建和sql语句增删改查/LinearLayout展示列表数据/ListView的使用和BaseAdater/内容提供者创建
- 数据结构,图的邻接矩阵创建,邻接矩阵与邻接表的交换,两种表的输出,过程用C++实现
- 【黑马Android】(04)数据库的创建和sql语句增删改查/LinearLayout展示列表数据/ListView的使用和BaseAdater/内容提供者创建
- C++存储数据结构之一List
- C#数据结构--线性表--List
- 数据结构之栈的初始化、创建、入栈、出栈、销毁-c++代码实现及运行实例结果
- 数据结构笔记(C++描述)