线性表LinearList的创建与使用
2013-11-13 16:13
225 查看
//基于公式的类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;
}
#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++]数据结构:公式化描述的线性表LinearList的创建与使用
- 使用gen_initramfs_list.sh脚本轻松创建cpio格式的initramfs文件
- 线性表(LinearList)
- unity_NGUI系统学习(十一)_PopupList下拉列表(弹出列表)的创建和使用
- 1>创建一个空的LinkedList通过使用ListIterator,将若干个Integer插入这个List中,插入时总是将他们插入到List的中间.
- 线性表(Linear List)
- 使用命令行创建Android工程报错:"Target id is not valid. Use 'android.bat list targets' to get the target ids"
- 使用Qml创建各种list(二)创建一个简单的动态列表
- 【数据结构】线性表(Linear List)
- Sharepoint学习笔记---SPList--使用Linq to Sharepoint间接查询External List(1.通过BCS创建External List)
- 使用Qml创建各种list(一)---创建一个简单图片列表
- 【黑马Android】(04)数据库的创建和sql语句增删改查/LinearLayout展示列表数据/ListView的使用和BaseAdater/内容提供者创建
- 使用Java API创建(create),查看(describe),列举(list),删除(delete)Kafka主题(Topic)
- javascript中值传递,地址传递,引用传递的问题(使用js创建list对象时会用到)
- 使用 WSPBuilder 创建List Instance WSP 包
- 使用模板创建RadioButton-List控件
- 为何使用EL表达式能够访问valueStack中对象的属性、采用OGNL表达式创建List/Map集合对象
- 使用Java API创建(create),查看(describe),列举(list),删除(delete)Kafka主题(Topic)--转载
- 使用htmlcontrol-for-symbian创建自定义list遇到的问题
- 使用Java API创建(create),查看(describe),列举(list),删除(delete)Kafka主题(Topic)