[C++]使用vector描述线性表定义及基本操作

 

#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

 

 

#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

 

 

#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

 

posted @ 2019-08-30 11:51  Xu_Lin  阅读(470)  评论(0编辑  收藏  举报