前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用vector描述线性表

用vector描述线性表

作者头像
青木
发布2018-05-28 15:42:52
5100
发布2018-05-28 15:42:52
举报

vectorList.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档