复习过程自写
为方便未来使用,首先定义线性表基类linealist.h
#ifndef LINEARLIST_H_INCLUDED
#define LINEARLIST_H_INCLUDED
template<class T>
class Linearlist{
public:
Linearlist(){};
~Linearlist(){};
virtual int Size() const = 0;
virtual int Length() const =0;
virtual int Search(T& ) const =0;
virtual int Locate(int ) const =0;
virtual bool getData(int ,T&) const =0;
virtual bool setData(int ,T&) =0;
virtual bool Insert(int ,T&) =0;
virtual bool Remove(int ,T&) =0;
virtual bool IsEmpty() const =0;
virtual bool IsFull() const =0;
};
#endif // LINEARLIST_H_INCLUDED
再根据顺序表的定义:把线性表中的所有表项按照其鲁普基顺序依次存储到计算机存储中指定位置开始的一块连续存储空间中。优点:寻址快,知道一个元素地址就能利用公式轻易的找到其余地址,可按照下标直接访问;缺点:空间要求高,连续存储。
下面给出顺序表的一个可能实现类:
#ifndef SEQLIST_H_INCLUDED
#define SEQLIST_H_INCLUDED
#include<iostream>
#include<cstdlib>
#include"linearlist.h"
using namespace std;
const int defaultSize = 100;
template<class T>
class SeqList:public Linearlist<T>{
protected:
T * data;
int maxSize;
int last;
void reSize(int );
public:
SeqList(int sz = defaultSize);
SeqList(SeqList<T>& );
~SeqList(){delete[] data;}
int Size()const{return maxSize;}
int Length() const{return last+1;}
int Search(T&) const;
int Locate(T&) const;
SeqList<T> operator = (SeqList<T>&) ;
bool getData(int i,T& x)const{
if(i>0&&i<=last+1){
x = data[i-1];
return true;
}
else return false;
}
bool setData(int i,T& x)const{
if(i>0 && i<=last+1){
data[i-1] = x;
return true;
}
else
return false;
}
bool Insert(int ,T&);
bool Remove(int ,T&);
bool IsEmpty()const{
return (last==-1);
}
bool IsFull()const{
return (last==maxSize - 1);
}
SeqList<T> operator =(SeqList<T>&);
template<class W>
friend
istream& operator >>(istream&,SeqList<W>&);
template<class W>
friend
ostream& operator <<(ostream&,SeqList<W>&);
};
template<class T>
SeqList<T>::SeqList(int sz){
if(sz >0){
maxSize = sz;
data = new T[maxSize];
last =-1;
if(!=data){
cerr<<"Error memory allocation!"<<endl;
exit(1);
}
}
}
template<class T>
SeqList<T>::SeqList(SeqList<T>& L){
maxSize = L.Size();
last = L.Length()-1;
T value;
int i;
data = new T[maxSize];
if(!data){
cerr<<"Error memory allocation!"<<endl;
exit(1);
}
for(i =1;i<=last+1;i++){
L.getData(i,value);
data[i-1]=value;
}
}
template<class T>
void SeqList<T>::reSize(int newsz){
if(newsz < = 0){
cerr<<"Error"<<endl;
return;
}
if(newsz != maxSize){
T* newarray = new T[newsz];
if(!newarray){
cerr<<"Memory allocation error!"<<endl;
exit(1);
}
int deslast=(newsz < last+1)? newsz-1:last;
int n = deslast+1;
T* srcptr = data;
T* desptr = newarray;
while(n--)
*desptr++ = *srcptr++;
delete[] data;
data = newarray;
maxSize = newsz;
last = deslast;
}
}
template<class T>
int SeqList<T>::Search(T& x) const{
for(int i=0;i<=last;i++){
if(data[i]==x)
return i+1;
}
return 0;
}
template<class T>
int SeqList<T>::Locate(int i)const{
if(i>=1&&i<=last+1)
return i;
return 0;
}
template<class T>
bool SeqList<T>::Insert(int i,T& x){
if(last == maxSize-1) ReSize(maxSize + 1);
if(i<0||i>last+1)return false;
for(int j = last;j<=i;j--){
data[j+1]=data[j];
}
data[i]=x;
last++;
return true;
}
template<class T>
bool SeqList<T>::Remove(int i,T& x){
if(i<0||i>last+1)return false;
for(int j=i;j<=last;j++)
data[j-1]=data[j];
last=last-1;
return true;
}
template<class T>
SeqList<T> SeqList<T>::operator=(SeqList<T>& L){
maxSize = L.Size();
last = L.Length()-1;
T value;
int i;
data = new T[maxSize];
if(!data){
cerr<<"Error memory allocation!"<<endl;
exit(1);
}
for(i =1;i<=last+1;i++){
L.getData(i,value);
data[i-1]=value;
}
}
template<class W>
istream& operator>>(istream& in,SeqList<W>& L){
int llength;
while(1){
cout<<"请输入线性表的长度:"<<endl;
in>>llength;
if(llength>0&&llength<=L.Size())
break;
cout<<"超出设定长度!"<<endl;
}
cout<<"请按顺序依次输入线性表的元素!"<<endl;
W value;
for(int i=0;i<llength;i++){
in>>value;
if(!L.Insert(i,value)){
cout<<"input error"<<endl;
break;
}
}
return in;
}
template<class W>
ostream& operator<<(ostream& out,SeqList<W>&L){
cout<<"线性表有"<<L.Length()<<"个元素"<<endl;
W value;
for(int i =0;i<L.Length();i++){
if(L.getData(i+1,value))
out<<"#"<<i+1<<":"<<value<<endl;}
return out;
}
#endif // SEQLIST_H_INCLUDED
如有错误,欢迎指出。我会努力学习和改正的!