Sequential List(顺序表)
来源:互联网 发布:淘宝运费险价格表 编辑:程序博客网 时间:2024/05/16 16:13
//本文件是顺序表(Sequential List)的头文件,使用C++模板类进行封装(This file is the header file of Sequential list,and packed by C++ tempalte class)#ifndef SEQLIST_H#define SEQLIST_H#include <iostream>#include "Exception.h"#include "AbstractList.h"using namespace std;template<class T>class SeqList:AbstractList<T>{public:SeqList(int MaxListSize=10);//constructor~SeqList(){delete [] element;}//destructorbool IsEmpty() const {return length==0;}//判断表是否为空(Judge the linear list is empty or not)int Length() const {return length;}//获取表的长度(Get the length of the linear list)bool Find(int k,T& x) const;//用x返回第k个元素,若没有第k个元素,返回假(Output the k-th element to x,return 0 if the it isn't exist)int Search(const T& x) const;//返回元素x的位置(Return the index of the element x)SeqList<T>& Delete(int k);//删除第k个元素(Delete the k-th element)SeqList<T>& Insert(int k,const T& x);//在第k个元素后面插入x(Insert x after the k-th element)//void Output(ostream& out) const;//输出顺序表(Output the linear list )private:int length;int MaxSize;T*element;};//实现构造函数(Instantiate constructor)template<class T>SeqList<T>::SeqList(int MaxListSize){MaxSize=MaxListSize;element=new T[MaxSize];length=0;}//实现Find(Instantiate Find)template<class T>bool SeqList<T>::Find(int k,T& x) const {//如果不存在第k个元素,则返回false,否则返回true(If the k-th element doesn't exist,it returns false,otherwise it returns true)if(k<1||k>length){return false;}//k超出顺序表的范围,非法的输入(k beyond the range of linear list,it's a illegal input )x=element[k-1];return true;}//实现Search(Instantiate Search)template<class T>int SeqList<T>::Search(const T& x) const{//查找x,如果找到,则返回x所在的位置;如果x不在表中,则返回0for(int i=0;i<length;i++){if(x==element[i]){return ++i;}}return 0;}//实现Insert(Instantiate Insert)template<class T>SeqList<T>& SeqList<T>::Insert(int k,const T& x){//在第k个位置插入x,如果不存在第k个位置,则引发异常OutOfBounds(Insert x at the k-position,an exception 'OutOfBounds' will be thrown if the k-position doesn't exist)if(k<0||k>length+1) throw OutOfBounds();//如果表已满,则引发异常NoMem(An exception 'NoMem' will be thrown if the linear list is full)if(length==MaxSize) throw NoMem();//向后移动一个位置(Move backwards one position)for(int i=length-1;i>=k;i--){element[i+1]=element[i];}element[k]=x;length++;return *this;}//实现Delete(Instantiate Delete)template<class T>SeqList<T>& SeqList<T>::Delete(int k){//如果不存在第k个元素,则引发异常OutOfBounds(An exception 'OutOfBounds'will be thrown if the k-th element doesn't exist)if(k<1||k>length) throw OutOfBounds(); //把第k+1,k+2,...元素向前移动一个位置(Move the element:k+1-th,k+2-th... forwards one position)for(int i=k;i<length;i++){element[i-1]=element[i];}length--;return *this;}#endif
//This is "AbstractList.h" header file #ifndef AbstractList_#define AbstractList_#include <iostream>using namespace std;template<class T>class AbstractList{public:virtual bool IsEmpty() const =0;virtual int Length() const =0;virtual bool Find(int k, T& x) const =0;virtual int Search(const T& x) const =0;virtual AbstractList<T>& Delete(int k) =0;virtual AbstractList<T>& Insert(int k,const T& x) =0;//virtual void Output(ostream& out) const =0;};#endif
//This is "Exception.h" header file#ifndef EXCEPTION_H#define EXCEPTION_H#include <iostream>#include <string>using namespace std;class OutOfBounds{public:OutOfBounds();void PrintError(const string error)const;private:string error;};class NoMem{public:NoMem();void PrintError(const string error)const;private:string error;};void OutOfBounds::PrintError(const string error) const{cout<<error<<endl;}OutOfBounds::OutOfBounds(){error="Out of the bounds of linear list!";}void NoMem::PrintError(const string error) const{cout<<error<<endl;}NoMem::NoMem(){error="No Member!";}#endif
- Sequential List(顺序表)
- 顺序表(Sequential List)
- JAVA顺序表(Sequential List)操作
- 顺序查找(Sequential Search)
- 顺序栈(Sequential Stack)
- 顺序队列(Sequential Queue)
- sequential list(template,virtual)
- 线性表的顺序存储结构(Sequential Mapping)
- 数据结构(Data Structure)(第二集)(顺序列表(Sequential List))(C语言)
- 数据结构之顺序查找(Sequential Search)
- VHDL顺序语句(Sequential Statements)
- db file sequential read-数据文件顺序读取
- 顺序表(sequence list)
- ISAM——索引顺序访问方法 Indexed Sequential Access
- 数据结构_线性表顺序sequence List
- 线性表(List)---顺序存储结构
- 线性表(List)---栈的顺序存储
- 线性表(List)---队列的顺序存储
- 关于Java IO流 你知道多少
- 影视评论
- break,continue,return等控制语句的使用
- vector 容器详细用法
- 在SrollView中嵌套GridView或ListView
- Sequential List(顺序表)
- Neither BindingResult nor plain target object for bean name 'user' available
- 关于android socket编程时,“Error:ShouldNotReachHere()”错误的原因
- 远程访问MySQL实现
- const用法详解
- 未能加载类型“FredCK.FCKeditorV2.FileBrowser.Config” 错误解决办法
- 14--4--2选择法排数字
- 几个C#日期、时间验证的正则表达式
- 黑马程序员之IO流