//LinearList.h
#ifndef LinearList_
#define LinearList_
//LinearList 线性表
//定义
template<class T>
class LinearList
{
public :
LinearList(int MaxListSize=10);
~LinearList(){delete[] elements;}
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;
LinearList<T>& Delete(int k,T& x);
LinearList<T>& Insert(int k,const T& x);
void Output(std::ostream& out) const;
private:
int length;
int MaxSize;
T *elements;//一维动态数组
};
class NoMem{
public:
NoMem(){}
};
void my_new_handler()
{
throw NoMem();
}
//new_handler Ole_Handler_=set_new_handler(my_new_handler);
//实现...
template<class T>
LinearList<T>::LinearList(int MaxListSize)
{ //基于公式的线性表的构造函数
MaxSize=MaxListSize;
elements=new T[MaxSize];
length=0;
}
template<class T>
bool LinearList<T>::Find(int k,T& x)const
{
if(k<1||k>length) return false;
x=elements[k-1];
return true;
}
template<class T>
int LinearList<T>::Search(const T& x)const
{
for(int i=0;i<length;i++)
if(elements[i]==x) return ++i;
return 0;
}
LinearList<int> y(100); //创建一个整数线性表y,其最大长度为100
template<class T>
LinearList<T> & LinearList<T>::Delete(int k,T& x)
{
//....
if(Find(k,x))
{
for(int i=k;i<length;i++)
elements[i-1]=elements[i];
length--;
return *this;
}
else ;//throw OutOfBounds();
}
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--)
elements[i+1]=elements[i];
elements[k]=x;
length++;
return *this;
}
template<class T>
void LinearList<T>::Output(std::ostream& out)const
{
for(int i=0;i<length;i++)
out<<elements[i]<<" ";
}
//重<<
template<class T>
std::ostream& operator<<(std::ostream& out,const LinearList<T>& x)
{
x.Output(out);return out;
}
#endif
// Win32Console.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "baseCplusplus1.h";
#include "baseClassRelInfo2.h";
#include "LinearList.h";
void baseCplusplusOp(); //C++函数模板递归一二维数组动态分配存储空间实例1
void baseClassRelInfo2();//C++类的相关基础2
void baseClassRelInfo2_1();
void LinearListSample();
int _tmain(int argc, _TCHAR* argv[])
{
baseCplusplusOp();
baseClassRelInfo2();
std::cout<<"/操作符重载"<<std::endl;
baseClassRelInfo2_1();
LinearListSample();
//暂停操作
char str;
std::cin>>str;
//程序结束
return 0;
}
void LinearListSample()
{
LinearList<int> L(5);
std::cout<<"Length="<<L.Length()<<std::endl;
std::cout<<"IsEmpty="<<L.IsEmpty()<<std::endl;
L.Insert(0,2).Insert(1,6);
std::cout<<"List is "<<L<<std::endl;
std::cout<<"IsEmpty="<<L.IsEmpty()<<std::endl;
int z;
L.Find(1,z);
std::cout<<"First elemet is "<<z<<std::endl;
std::cout<<"Length="<<L.Length()<<std::endl;
L.Delete(1,z);
std::cout<<"Deleted element is "<<z<<std::endl;
std::cout<<"List is "<<L<<std::endl;
}
原文链接: https://www.cnblogs.com/sqlite3/archive/2010/07/09/2566932.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/12604
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!