C++数据结构——顺序表

 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;

}



运行输出结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值