手记

C++环形队列类模板 = =#

首先感谢 james_yuan 老师,C++一路跟着学过来!

环形队列类模板的实现如下(附带测试代码)

  • 环形队列类模板
#ifndef CIRCULARQUEUE_H_INCLUDED
#define CIRCULARQUEUE_H_INCLUDED
#include <iostream>
#include <string.h>

using namespace std;

/**
 *  环形队列类模板
 *
 */
template <class T>
class CircularQueue{
public:
    CircularQueue(int capacity);
    virtual ~CircularQueue();
    //清队
    void clearQueue();
    //入队
    bool enQueue(T element);
    //出队
    bool deQueue(T &element);   //传入一个T的引用,方便接队头,而不是返回队头,这样函数返回布尔,调用完毕后,引用拿到队头
    //判空
    const bool isEmpty();
    //判满
    const bool isFull();
    //队长
    const int length();
    //列队
    void printQueue(void(*pFunc)(T));    //适配所有模板类的打印,传入一个对应类型的打印函数指针
private:
    //队列数组指针
    T *m_pQueue;
    //队列容量
    int m_iCapacity;
    //队头
    int m_iHead;
    //队尾
    int m_iTail;
    //队长
    int m_iLength;
};

template <class T>
CircularQueue<T>::CircularQueue(int capacity){
    m_iCapacity = capacity;
    clearQueue();
    if ((m_pQueue = new T[m_iCapacity]) == NULL) {
        throw string("Queue Initialization Failed!");
    }
}

template <class T>
CircularQueue<T>::~CircularQueue(){
    delete []m_pQueue;
    m_pQueue = NULL;
}

template <class T>
void CircularQueue<T>::clearQueue(){
    m_iHead = 0;
    m_iTail = 0;
    m_iLength = 0;
}

template <class T>
bool CircularQueue<T>::enQueue(T element){
    if (isFull()) {
        return false;
    }
    m_pQueue[m_iTail] = element;
    m_iLength++;
    m_iTail++;
    m_iTail = m_iTail % m_iCapacity;
    return true;
}

template <class T>
bool CircularQueue<T>::deQueue(T &element){
    if (isEmpty()) {
        return false;
    }
    //传入一个T的引用,方便接收队头,而不是返队头,这样函数返回布尔,调用完毕后,引用拿到队头
    element = m_pQueue[m_iHead];
    m_iLength--;
    m_iHead++;
    m_iHead = m_iHead % m_iCapacity;
    return true;
}

template <class T>
const bool CircularQueue<T>::isEmpty(){
    return m_iLength == 0 ? true : false;
}

template <class T>
const bool CircularQueue<T>::isFull(){
    return m_iCapacity == m_iLength ? true : false;
}

template <class T>
const int CircularQueue<T>::length(){
    return m_iLength;
}

template <class T>
void CircularQueue<T>::printQueue(void(*pFunc)(T)){
    for (int i = m_iHead; i < m_iHead + m_iLength; i++) {
        pFunc(m_pQueue[i % m_iCapacity]);
    }
}

#endif // CIRCULARQUEUE_H_INCLUDED
  • Coordinate测试类
#ifndef COORDINATE_H_INCLUDED
#define COORDINATE_H_INCLUDED

class Coordinate{
public:
    Coordinate();
    Coordinate(double x, double y);
    virtual ~Coordinate();
    double getX();
    double getY();
private:
    double m_iX;
    double m_iY;
};

Coordinate::Coordinate(){

}

Coordinate::Coordinate(double x, double y){
    m_iX = x;
    m_iY = y;
}

Coordinate::~Coordinate(){

}

double Coordinate::getX(){
    return m_iX;
}

double Coordinate::getY(){
    return m_iY;
}

#endif // COORDINATE_H_INCLUDED
  • 环形队列类模板测试
#include <iostream>
#include <string.h>
#include "CircularQueue.h"
#include "Coordinate.h"

using namespace std;

//打印整型数据,作为函数指针传递给队列类
void printInt(int o){
    cout << o << endl;
}

//打印Coordinate数据,作为函数指针传递给队列类
void printCoor(Coordinate o){
    cout << "(" << o.getX() << "," << o.getY() << ")" << endl;
}

int main()
{
    try{
        //整型环形队列模板类
        cout << "***********int*********" << endl;
        //入队测试
        CircularQueue<int> cQueueInt = CircularQueue<int>(3);
        cQueueInt.enQueue(10);
        cQueueInt.enQueue(20);
        cQueueInt.enQueue(30);
        cQueueInt.printQueue(&printInt);
        cout << "length : " << cQueueInt.length() << endl;

        cout << "-----------------------" << endl;
        //出队测试
        int e1;
        cQueueInt.deQueue(e1);
        cout << "i'm out : " << e1 << endl;
        int e2;
        cQueueInt.deQueue(e2);
        cout << "i'm out : " << e2 << endl;

        cout << "-----------------------" << endl;
        cQueueInt.printQueue(&printInt);
        cout << "length : " << cQueueInt.length() << endl;

        cout << "-----------------------" << endl;
        //队满测试
        cQueueInt.enQueue(40);
        cQueueInt.enQueue(50);
        cQueueInt.enQueue(60);     //超过长度插入失败
        cQueueInt.printQueue(&printInt);
        cout << "length : " << cQueueInt.length() << endl;

        //Coordinate型环形队列模板类
        cout << endl << "***********Coordinate*********" << endl;
        //入队测试
        CircularQueue<Coordinate> cQueueCoor = CircularQueue<Coordinate>(3);
        Coordinate c1 = Coordinate(1,2);
        Coordinate c2 = Coordinate(3,4);
        Coordinate c3 = Coordinate(5,6);
        cQueueCoor.enQueue(c1);
        cQueueCoor.enQueue(c2);
        cQueueCoor.enQueue(c3);
        cQueueCoor.printQueue(&printCoor);
        cout << "length : " << cQueueCoor.length() << endl;

        cout << "-----------------------" << endl;
        //出队测试
        cout << "we out:" << endl;
        Coordinate co1;
        cQueueCoor.deQueue(co1);
        printCoor(co1);

        Coordinate co2;
        cQueueCoor.deQueue(co2);
        printCoor(co2);

        cout << "-----------------------" << endl;
        cQueueCoor.printQueue(&printCoor);
        cout << "length : " << cQueueCoor.length() << endl;

        cout << "-----------------------" << endl;
        //队满测试
        Coordinate c4 = Coordinate(7,8);
        Coordinate c5 = Coordinate(9,10);
        Coordinate c6 = Coordinate(10,11);  //队满,不能入队
        cQueueCoor.enQueue(c4);
        cQueueCoor.enQueue(c5);
        cQueueCoor.enQueue(c6);
        cQueueCoor.printQueue(&printCoor);
        cout << "length : " << cQueueCoor.length() << endl;

        return 0;

    } catch (string &e) {  //捕获可预知异常
        cout << e << endl;
        return 2;
    } catch (...) { //捕获其他所有未知异常
        return 1;
    }
}
  • 运行结果
19人推荐
随时随地看视频
慕课网APP

热门评论

有你Q吗。加你..............

查看全部评论