博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linked List 的sample codes
阅读量:6708 次
发布时间:2019-06-25

本文共 1666 字,大约阅读时间需要 5 分钟。

#include
using namespace std;template
class LinearList{ public: LinearList(int MaxListSize = 10); ~LinearList() {delete [] element;} bool IsEmpty() const {
return length == 0;} int Length() const {
return length;} bool Find(int k, T& x) const; int Search(const T& x) const; LinearList
& Delete(int k, T& x); LinearList
& Insert(int k, const T& x); void Output(ostream& out)const; private: int length; int MaxSize; T *element;};//超出内存class NoMem{ public: NoMem(){}};void my_new_handler(){ throw NoMem();};class OutOfBounds{ public: OutOfBounds(){}};template
LinearList
::LinearList(int MaxListSize){ MaxSize = MaxListSize; element = new T[MaxSize]; length = 0;};template
bool LinearList
::Find(int k, T& x)const{ if(k<1 || k>length) return false; x = element[k-1]; return true;};template
int LinearList
::Search(const T& x)const{ for(int i=0; i < length ; i++) if(element[i]==x) return ++i; return 0;};template
LinearList
& LinearList
::Delete(int k, T& x){ if(Find(k,x)){ for(int i=k; i
LinearList
& LinearList
::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;};template
void LinearList
::Output(ostream& out)const{ for(int i=0;i
ostream& operator<<(ostream& out, const LinearList
& x){ x.Output(out); return out;};int main(){ try{ LinearList
L(5); cout<<"Length = "<
<

 

转载于:https://www.cnblogs.com/cosmo89929/archive/2013/01/03/2842813.html

你可能感兴趣的文章