err.h
#ifndef _Err_
#define _Err_
#include<new.h>
class OutOfMemory
{
public:
OutOfMemory(){}
};
int my_Handler(size_t size)
{
throw OutOfMemory();
return 0;
};
_PNH old_handler=_set_new_handler(my_Handler);
//The type _PNH is a pointer to a function that returns type int and takes a single argument of type size_t.
//The _set_new_handler function returns the address of the previous new handler.
class OutOfBound
{
public:
OutOfBound(){}
};
#endif
List.h
#ifndef _LinearList_
#define _LinearList_
#include<iostream>
#include"err.h"
using namespace std;
template<class T>
class LinearList
{
private:
T *elements;
int size;
int length;
public:
LinearList(int size=10);
~LinearList();
LinearList<T> & clearList();
bool listEmpty()const;
int listLength()const;
bool getElem(int k,T &x)const;
int locateElem(T const &x)const;
T priorElem(T const &x)const;
T nextElem(T const &x)const;
LinearList<T> &listInsert(int k,T const x);
LinearList<T> &listDelete(int k);
friend ostream &operator<<(ostream &os,LinearList<T> &L)
{
if(L.length>0)
{
for(int i=0;i<L.length;i++)
os<<L.elements[i]<<"\t";
}
else
{
os<<"list is empty"<<endl;
}
return os;
};
};
#endif
List.cpp
#include"List.h"
template<class T>
LinearList<T>::LinearList(int size)
{
this->size=size;
length=0;
elements=new T[size];
};
template<class T>
LinearList<T>::~LinearList()
{
delete [] elements;
};
template<class T>
LinearList<T> & LinearList<T>::clearList()
{
delete [] elements;
elements=new T[size];
return *this;
};
template<class T>
bool LinearList<T>::listEmpty()const
{
return length==size;
};
template<class T>
int LinearList<T>::listLength()const
{
return length;
};
template<class T>
bool LinearList<T>:: getElem(int k,T &x)const
{
if(k<1||k>length)
{
throw OutOfBound();
return false;
}
x=elements[k-1];
return true;
};
template<class T>
int LinearList<T>:: locateElem(T const &x)const
{
for(int i=0;i<length;i++)
if(elements[i]==x)
return ++i;
return 0; //if LinearList does not exist x;
}
template<class T>
T LinearList<T>:: priorElem(T const &x)const
{
int k=locateElem(x);
if(k)
{
return elements[k-2];
}
return 0;
};
template<class T>
T LinearList<T>:: nextElem(T const &x)const
{
int k=locateElem(x);
if(k)
{
return elements[k];
}
return 0;
};
template<class T>
LinearList<T> &LinearList<T>:: listInsert(int k,T const x)
{
if(k<1||k>size)
{
throw OutOfBound();
return *this;
}
length++;
for(int j=length;j>=k;j--)
elements[j]=elements[j-1];
elements[k-1]=x;
return *this;
};
template<class T>
LinearList<T> & LinearList<T>::listDelete(int k)
{
if(k<1||k>length)
{
throw OutOfBound();
return *this;
}
for(int j=k-1;j<length;j++)
elements[j]=elements[j+1];
length--;
return *this;
};
main_c.cpp
#include"List.cpp"
int main(void)
{
LinearList<int> L(5);
cout<<"befor insert: listEmpty? "<<L.listEmpty()<<endl;
L.listInsert(1,1).listInsert(2,2).listInsert(3,3).listInsert(4,4).listInsert(5,5);
cout<<"after insert: listEmpty? "<<L.listEmpty()<<endl;
cout<<"list are:"<<endl;
cout<<L;
cout<<"list length is: "<<L.listLength()<<endl;
int x;
L.getElem(2,x);
cout<<"getElem(2,x) : "<<x<<endl;
cout<<"locateElem(x) : "<<L.locateElem(x)<<endl;
cout<<"priorElem(x) : "<<L.priorElem(x)<<endl;
cout<<"nextElem(x) : "<<L.nextElem(x)<<endl;
L.listDelete(2);
cout<<"after delet(2),list are:"<<endl;
cout<<L<<endl;
cout<<"list length is: "<<L.listLength()<<endl;
cout<<endl;
system("pause");
return 0;
}