顺序表类

来源:互联网 发布:小米抢购软件app 编辑:程序博客网 时间:2024/06/14 11:22

顺序表(Sequential List)需要存储器中的一块连续的空间。在高级语言的固有数据类型中,数组在存储器中表现为一块连续的空间,所以用数组实现顺序表是合适的。数组中各结点位置由其下标来表示,它同时就是相应结点的位置序号。我们可以将线性表中的n个结点,按照序号放入下标为0到n-1的数组元素中去,Legth为结点个数,MaxSize为Length的上界,用数组elem存储线性表,并且将下标为0的数组元素elem[0]用于其他特殊用途,不用来存放顺序表中的结点,这样,顺序表就可以从下标1的数组元素开始连续存放结点,最后一个结点的序号为length!

顺序表SeqList的定义如下:

//SeqList.h//线性表的顺序存储结构//顺序表类 #include<iostream>using namespace std;//顺序表模板类 template<class ElemType>class SeqList{private:ElemType *elem;//顺序表存储数组,存放实际的数据结点int length;    //顺序表中结点个数,也即表的长度 int MaxSize;   //顺序表最大可能的长度 public:SeqList(int InitSize);//构造函数~SeqList();//析构函数void Clear();//清空顺序表bool IsEmpty() const{return(length==0);}//表是否为空bool IsFull() const{return(length==MaxSize);}//表是否满int Length() const;//表的长度ElemType Get(int i) const;//返回第i结点的值int Next(int i) const;//若第i个结点的直接后继结点存在,返回其下标,否则返回0int Prior(int i) const;//若第i个结点的直接前驱结点存在,返回其下标,否则返回0 int Find(ElemType e) const;//返回等于e的结点的序号,无则返回0int Insert(int i,const ElemType &e);//在第i个位置上插入新的结点(值为e),并使原来的第i个结点至最后一个结点                                    //的序号依次加1.插入成功返回1,否则返回0 int Delete(ElemType &e,int i);//若第i个结点存在,删除并将其值放入e,若i非法,则                               //删除失败,返回0};//异常处理函数void Exception(int Condition,const char * ErrorMsg){if(Condition){cout<<ErrorMsg<<endl;abort();}}//SeqList模板类的实现template<class ElemType>SeqList<ElemType>::SeqList(int InitSize){if(InitSize>0){elem=new ElemType[InitSize];Exception(!elem,"there is no space in memory");//申请空间失败,程序中断length=0;MaxSize=InitSize-1;}}template<class ElemType>SeqList<ElemType>::~SeqList(){delete []elem;//释放占用的连续空间}template<class ElemType>void SeqList<ElemType>::Clear(){length=0;//声明顺序表中结点数为0}template<class ElemType>int SeqList<ElemType>::Length() const{return length;//返回表的长度}template<class ElemType>ElemType SeqList<ElemType>::Get(int i) const{Exception((i<1)||(i>length),"Out of bounds access.");//越界、非法return elem[i];}template<class ElemType>int SeqList<ElemType>::Next(int i) const{Exception((i<1)||(i>length-1),"Out of bounds access.");return i+1;}template<class ElemType>int SeqList<ElemType>::Prior(int i) const{Exception((i<=1)||(i>length),"Out of bounds access.");return i-1;}template<class ElemType>int SeqList<ElemType>::Find(ElemType e) const{//按照下标从大到小顺序查找值为e的数组结点的下标并将其返回。//elem[0]做哨兵,保证即使查找失败,也可以在哨兵位上能找到值eelem[0]=e;int i=length;//初始位置为尾结点所在下标while(elem[i]!=e) i--;//不等时继续向前比较,找到返回结点下标,否则返回0return i;}template<class ElemType>int SeqList<ElemType>::Insert(int i,const ElemType &e){Exception((i<1)||(i>length+1),"i is not correct.");Exception(MaxSize==length,"no space for new item.");for(int j=length;j>=i;j--) elem[j+1]=elem[j];elem[i]=e;length++;return 1;//插入成功返回1}template<class ElemType>int SeqList<ElemType>::Delete(ElemType &e,int i){Exception((i<1)||(i>length),"i is illegeal");e=elem[i];for(int j=i;j<length;j++) elem[j]=elem[j+1];length--;return i;//返回成功标志i}

例题:从键盘输入若干字符按照顺序放入一个顺序表中,删除第1个字符并将它插入到顺序表的中间位置,其实现方法见下面程序

//SeqList.cpp#include<iostream>#include"SeqList.h"using namespace std;int main(){SeqList<char> chlist(21);//定义了一个SeqList类的对象,其中ElemType定义为char,空间大小为21//除下标为0的数组元素外,最多可以放20个结点char ctemp;int i,n,result;cout<<"number of the elements:"<<endl;cin>>n;//输入一个正整数,并输入Enter作为结束标志cin.get(ctemp);//将Enter抛弃cout<<"input the elements:"<<endl;for(i=1;i<=n;i++)//将字符依次插入到表SeqList中,并依次插入到表尾{cin.get(ctemp);chlist.Insert(i,ctemp);}result=chlist.Delete(ctemp,1);if(result) result=chlist.Insert(chlist.Length()/2+1,ctemp);if(!result) cout<<"fail to insert the element!"<<endl;else{cout<<"output of the elements:"<<endl;for(i=1;i<=chlist.Length();i++)cout.put(chlist.Get(i));cout<<endl;}return 0;}
运行结果如下:




原创粉丝点击