本文共 3677 字,大约阅读时间需要 12 分钟。
templateclass AbstractList{ public: virtual bool isEmpty() const=0; virtual int length() const=0; virtual bool findElement(int k,T& x)const =0; //find the element whose index is k and assign the result to the x virtual int searchElement(const T& x)const =0; //search the x element and return its index virtual AbstractList & delete(int k,T& x)=0; //delete the x element ,return the deleted Linklist and return the deleted element virtual AbstractList & insert(int k,const T& x)=0; //insert the element at the position whose index is k virtual void output() const=0;};
使用“virtual … =0”的方式去声明纯虚函数,而在结尾加上对应的const,则表明该成员函数并不能够对类的成员进行修改
对于大型的数据量,要尽量调用的引用来进行传值,减少内存消耗
如果不需要修改形参的值,可在形参前面加上const,声明为的常量
LinerList.h文件
#ifndef _LINERLIST_H#define _LINERLIST_H#include "AbstractList.h"#includeusing namespace std;template class LinerList:public AbstractList { public: LinerList(int maxSize=10); ~LinerList(){ delete []element;} bool isEmpty()const{ return length==0;}; int getLength()const { return length;}; bool findElement(int k,T& x)const; //find the element whose index is k and assign the result to the x int searchElement(const T& x)const; //search the x element and return its index AbstractList & Delete(int k,T& x); //delete the x element ,return the deleted Linklist and return the deleted element AbstractList & insert(int k,const T& x); //insert the element at the position whose index is k void output() const; AbstractList & add(const T& x);private: int length; int maxSize; T* element;};template LinerList ::LinerList(int maxSize){ element = new T[maxSize]; this->maxSize = maxSize; length = 0;}//find the element whose index is k and assign the result to the xtemplate bool LinerList ::findElement(int k,T& x)const{ if(k<0 || k>length) { cout<<"error:out of index!"< int LinerList ::searchElement(const T& x)const{ if(isEmpty()) return -1; //for the illegal value use illegal value to represent for! for(int i = 0;i < length;i++) { if(element[i] == x) { return ++i; //caution:the index of the LinerList is one more than array! } } return -1;}//delete the x element ,return the deleted Linklist and return the deleted elementtemplate AbstractList & LinerList ::Delete(int k,T& x){ if(findElement(k,x)) { for(int i = k; i < length; i ++) { element[i-1] = element[i]; } length --; return *this; } else { cout<<"out of the index"< AbstractList & LinerList ::insert(int k,const T& x){ if(!(length == 0 && k == 1)) { if(k>length || k<1) { cout<<"out of the index!"< = k-1 && i >= 0; i --) { element[i + 1] = element[i]; } //insert the element in the targeted position } element[k - 1] = x; length ++; return *this;}template void LinerList ::output() const{ cout<<"the length of the LinerList is:"< < < length;i ++) { cout< <<" "; } cout< AbstractList & LinerList ::add(const T& x){ if(length == maxSize) { cout<<"out of memeory!add the element absortively"<
#include#include "LinerList.h"using namespace std;int main(){ LinerList L(5); cout<<"length:"< <
转载地址:http://cwwvb.baihongyu.com/