最近在复习数据结构与算法,决定做个完整的总结。以后会已两天一次的速度更新,希望能对在学数据结构的朋友有所帮助。当然,本人也不是什么大牛,难免出错,还请见谅。有问题请评论,博主会与你们一起探讨并一一回复。
废话少说,先从线性表的顺序存储开始。懒得打字,直接上代码(C++版)。
头文件 linearList.h
#ifndef LINEARLIST_H_INCLUDED#define LINEARLIST_H_INCLUDED#define DEFAULT_SIZE 10//定义状态码typedef int StatusCode;const static StatusCode RANGE_ERROR=0;const static StatusCode SUCCESS=1;const static StatusCode OVER_FLOW=2;//顺序表类模板templateclass SqList{protected: //顺序表的数据成员 int count; int maxSize; ElemType *elems; //辅助函数模板 bool Full()const;//判断线性表是否已满 void Init(int size);//初始化线性表public: //抽象数据类型方法声明 SqList(int size=DEFAULT_SIZE); virtual ~SqList(); int Length()const;//求线性表长度 bool Empty()const;//判断线性表是否为空 void clear();//清空线性表 void Traverse(void (*visit)(const ElemType));//遍历线性表 StatusCode GetElem(int position,ElemType &e)const;//求指定位置的元素 StatusCode SetElem(int position,const ElemType &e);//设置指定位置处的元素值 StatusCode Delete(int position,ElemType &e);//删除元素 StatusCode Insert(int position,const ElemType &e);//插入元素 /* SqList(const SqList ©);//赋值构造函数模板 SqList operater = (const SqList ©);//重载算数运算符 */};#endif // LINEARLIST_H_INCLUDED
函数实现 LinearList.cpp
#include "linearList.h"#includeusing namespace std;template bool SqList ::Full()const{ return maxSize==count;}template void SqList ::Init(int size){ maxSize=size; if(elems!=NULL) { delete []elems; } elems=new ElemType[maxSize]; count=0;}template SqList ::SqList(int size){ Init(size);}template SqList ::~SqList(){ delete []elems;}template int SqList ::Length()const{ return count;}template bool SqList ::Empty()const{ return count==0;}template void SqList ::clear(){ count=0;}template StatusCode SqList ::SetElem(int position,const ElemType &e){ if(position<1||position>Length()) { return RANGE_ERROR; } elems[position]=e; return SUCCESS;}template StatusCode SqList ::Insert(int position,const ElemType &e){ if(position<1||position>Length()+1) { return RANGE_ERROR; } if(Full()) { return OVER_FLOW; } ElemType elem; for(int curPosition=Length();curPosition>position;curPosition--) { elems[curPosition+1]=elems[curPosition]; } elems[position]=e; count++; return SUCCESS;}template StatusCode SqList ::GetElem(int position,ElemType &e)const{ if(position<1||position>Length()) { return RANGE_ERROR; } e=elems[position]; return SUCCESS;}template StatusCode SqList ::Delete(int position,ElemType &e){ if(position<1||position>Length()) { return RANGE_ERROR; } GetElem(position,e); for(int curPosition=position+1;curPosition<=Length();curPosition++) { elems[curPosition-1]=elems[curPosition]; } count--; return SUCCESS;}template void SqList ::Traverse(void(*visit)(const ElemType)){ for(int curPosition=1;curPosition<=Length();curPosition++) { visit(elems[curPosition]); }}
测试代码:
#include#include "linearList.h"#include "linearList.cpp"using namespace std;void show(const int e);int main(){ int size=12; SqList list(size); StatusCode status=list.Insert(1,2); cout<<"插入状态:"< < <<"第一个元素是:"< < << "线性表的长度为:"< << endl; cout<<"线性表是否为空:"< < <<"调用SetElem之后:"< <<"第一个元素是:"< < <<"删除前:"< <=list.Length();i++) { list.GetElem(i,k); cout< <<" "; } cout< <<"删除后:"< <=list.Length();i++) { int k; list.GetElem(i,k); cout< <<" "; } cout< <<"删除的元素是:"< < <<"遍历元素"< < <
或许你已注意到,SqList中有两个函数被注释并未实现。请读者自行完成。如需要完整实现,请留言。