您的当前位置:首页正文

数据结构上机实验线性表单链表源代码

来源:帮我找美食网
#include

template

class LinearList

{

public:

virtual bool IsEmpty()const=0;

virtual int Length()const=0;

virtual bool Find(int i,T& x)const=0;

virtual int Search(T x)const=0;

virtual bool Insert(int i,T x)=0;

virtual bool Update(int i,T x)=0;

virtual bool Delete(int i)=0;

virtual void Output(ostream& out)const=0;

protected:

int n;

};

#include \"linearlist\"

template

class SeqList:public LinearLisr

{

public:

SeqList(int mSize);

~SeqList(){delete [] elements;}

bool IsEmpty()const;

bool Find(int i,T& x)const;

int Length()const;

int Search(T x)const;

bool Insert(int i,T x);

bool Update(int i,T x);

bool Delete(int i);

void Output(ostream& out)const;

private:

int maxLength;

T *elements;

};

template

SeqList::SeqList(int mSize)

{

maxLength=mSize;

elements=new T[maxLength];

n=0;

}

template

bool SeqList::IsEmpty()const

{

return n==0;

}

template

int SeqList::Length()const

{

return 0;

}

template

bool SeqList::Find(int i,T& x)const

{

if(i<0||i>n-1)

{

cont<<\"Out of Bounds\"<return false;

}

x=elements[i];

return true;

}

template

int SeqList::Search(T x)const

{

for(int j=0;jif(elements[j]==x)

return -1;

}

template

bool SeqList::Insert(int i,T x)

{

if(i<-1||i>n-1)

{

cout<<\"OUt of Bounds\"<}

if(n==maxLength){

cout<<\"OverFlow\"<return false;

}

for(int j=n-1;j>i;j--)

elements[j+1]=elements[j];

elements[j+1]=x;

n++;return true;

}

template

bool SeqList::Delete(int i)

{

if(!n){

cout<<\"UnderFlow\"<return false;

}

if(i<0||i>n-1)

{

cout<<\"OUt of Bounds\"<}

for(int j=i+1;jelements[j-1]=elements[j];

n--;return true;

}

template

bool SeqList::Update(int i,T x)

{

if(i<0||i>n-1)

{

cout<<\"OUt of Bounds\"<}

elements[i]=x;

return true;

}

template

bool SeqList::Output(ostream& out)const

{

for(int i=0;iout<}

因篇幅问题不能全部显示,请点此查看更多更全内容

Top