您的位置:首页 > 理论基础 > 数据结构算法

[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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: