用vector描述线性表

vectorList.h

#ifndef VECTORLIST_H
#define VECTORLIST_H
#include<iostream>
#include"linearlist.h"
#include<vector>
#include<myexceptions.h>
using namespace std;

template<class T>
class vectorList : public linearList<T>
{
public:
    //构造函数
    vectorList(int initialCapacity = 0);

    //复制构造函数
    vectorList(const vectorList<T>&);

    //析构函数
    ~vectorList()
    {
        delete element;
    }


    /*
     * 类linearList中抽象方法的实现
    */
    //判断是否表空
    bool empty() const
    {
        return element->empty();
    }

    //返回表内元素个数
    int size() const
    {
        return (int)element->size();
    }

    //返回索引为theIndex的元素
    T& get(int theIndex) const;

    //返回元素theElement的索引
    int indexOf(const T &theElement) const;

    //删除索引为theIndex的元素
    void erase(int theIndex);

    //在索引为theIndex的位置插入元素theElement
    void insert(int theIndex, const T &theElement);

    //将线性表元素放入输出流
    void output(ostream &out) const;


    /*
     * 增加的方法
    */
    int capacity() const
    {
        return (int)element->capacity();
    }

    /*
     * 线性表的起始和结束位置的迭代器
    */
    typedef typename vector<T>::iterator iterator;
    iterator begin(){return element->begin();}
    iterator end(){return element->end();}
protected:
    void checkIndex(int theIndex) const;
    vector<T>* element;//存储线性表元素的向量
};

/*
 * 构造函数和复制构造函数的实现
*/
template<class T>
vectorList<T>::vectorList(int initialCapacity)
{
    //构造函数
    if(initialCapacity < 1)
    {
        ostringstream s;
        s<<"Initial capacity = "<<initialCapacity<<"Must be > 0";
    throw illegalParameterValue(s.str);
    }
    element = new vector<T>;//创建向量为0的空向量
    element->reserve(initialCapacity);//vector的容量从0增加到initialCapacity

}

template<class T>
vectorList<T>::vectorList(const vectorList<T> &theList)
{
    //复制构造函数
    element = new vector<T>(*theList.element);
}


//删除元素
template<class T>
void vectorList<T>::erase(int theIndex)
{
    checkIndex(theIndex);
    element->erase(begin()+theIndex);
}

//插入元素
template<class T>
void vectorList<T>::insert(int theIndex, const T &theElement)
{
    if(theIndex < 0 || theIndex > size())
    {
        //无效索引
        ostringstream s;
        s<<"index = "<<theIndex <<" size = "<<size();
        throw illegalIndex(s.str());
    }
    element->insert(element->begin()+theIndex,theElement);
}

#endif // VECTORLIST_H

myexceptions.h

#ifndef MYEXCEPTIONS_H
#define MYEXCEPTIONS_H

#include <string>

using namespace std;

// illegal parameter value
class illegalParameterValue
{
   public:
      illegalParameterValue(string theMessage = "Illegal parameter value")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// illegal input data
class illegalInputData
{
   public:
      illegalInputData(string theMessage = "Illegal data input")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// illegal index
class illegalIndex
{
   public:
      illegalIndex(string theMessage = "Illegal index")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// matrix index out of bounds
class matrixIndexOutOfBounds
{
   public:
      matrixIndexOutOfBounds
            (string theMessage = "Matrix index out of bounds")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// matrix size mismatch
class matrixSizeMismatch
{
   public:
      matrixSizeMismatch(string theMessage =
                   "The size of the two matrics doesn't match")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// stack is empty
class stackEmpty
{
   public:
      stackEmpty(string theMessage =
                   "Invalid operation on empty stack")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// queue is empty
class queueEmpty
{
   public:
      queueEmpty(string theMessage =
                   "Invalid operation on empty queue")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// hash table is full
class hashTableFull
{
   public:
      hashTableFull(string theMessage =
                   "The hash table is full")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// edge weight undefined
class undefinedEdgeWeight
{
   public:
      undefinedEdgeWeight(string theMessage =
                   "No edge weights defined")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// method undefined
class undefinedMethod
{
   public:
      undefinedMethod(string theMessage =
                   "This method is undefined")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};
#endif // MYEXCEPTIONS_H

linearList.h

#ifndef LINEARLIST_H
#define LINEARLIST_H

// LINEARLIST_H
// abstract class linearList
// abstract data type specification for linear list data structure
// all methods are pure virtual functions

#include <iostream>

using namespace std;

template<class T>
class linearList
{
   public:
      virtual ~linearList() {};
      virtual bool empty() const = 0;
                  // return true iff list is empty
      virtual int size() const = 0;
                  // return number of elements in list
      virtual T& get(int theIndex) const = 0;
                  // return element whose index is theIndex
      virtual int indexOf(const T& theElement) const = 0;
                  // return index of first occurence of theElement
      virtual void erase(int theIndex) = 0;
                  // remove the element whose index is theIndex
      virtual void insert(int theIndex, const T& theElement) = 0;
                  // insert theElement so that its index is theIndex
      virtual void output(ostream& out) const = 0;
                  // insert list into stream out
};
#endif



转载于:https://my.oschina.net/u/1771419/blog/1793053

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值