Circular Queue

Queue class and operator:

#include "queue.h"
#include <iostream>

using namespace std;

template <typename T>
Queue<T>::Queue(int queueCapacity){
    i_cQueueCapacity = queueCapacity;
    i_pQueue = new T[i_cQueueCapacity];//perilous int stack
    ClearQueue();
}
//destory the queue
template <typename T>
Queue<T>::~Queue(){
    delete []i_pQueue;
    i_pQueue = NULL;
}

//clear queue & do not free the memory & do not change capacity
template <typename T>
void Queue<T>::ClearQueue(){
    i_iHead = 0;
    i_iTail = 0;
    i_nQueueLen = 0;
}

//do not judge that head&tail is 0
template <typename T>
bool Queue<T>::QueueEmpty(){
    if(0 == i_nQueueLen){
        return true;
    }else{
    return false;
    }
}

template <typename T>
bool Queue<T>::QueueFull(){
    if(i_nQueueLen == i_cQueueCapacity){
        return true;
    }else{
        return false;
    }
}

//the length of queue
template <typename T>
int Queue<T>::QueueLength(){
    return i_nQueueLen;
}

template <typename T>
bool Queue<T>::EnterQueue(T element){
    if(QueueFull()){
        return false;
    }else{
        i_pQueue[i_iTail] = element;
        i_iTail++;
        i_iTail = i_iTail % i_cQueueCapacity;//circulation
        i_nQueueLen++;
        return true;
    }
}

template <typename T>
bool Queue<T>::DeQueue(T &element){
    if(QueueEmpty()){
        return false;
    }else{
        element = i_pQueue[i_iHead];
        i_iHead++;
        i_iHead = i_iHead % i_cQueueCapacity;//circulation
        i_nQueueLen--;
        return true;
    }
}

template <typename T>
void Queue<T>::QueueTraverse(){
    for(int i = i_iHead ; i < i_nQueueLen + i_iHead ; i++){
        i_pQueue.printInfo();
        //cout << i_pQueue[i % i_cQueueCapacity];
        cout << endl;
    }
}

 

 

.h file:

#ifndef _queue_h_
#define _queue_h_

template <typename T>
class Queue{
public:
    Queue(int queueCapacity);//constructor
    virtual ~Queue();//destructor

    void ClearQueue();//clear the queue
    bool QueueEmpty();//judge queue if null
    bool QueueFull();//
    int QueueLength();//return the lenght of queue
    bool EnterQueue(T element );//enter the element
    bool DeQueue(T &element);//dequeue
    void QueueTraverse();//traverse the queue
private:
    T *i_pQueue;//the point of queue
    int i_nQueueLen;//the length of queue
    int i_cQueueCapacity;//queue capacity
    int i_iHead;//the head of queue
    int i_iTail;//the tail of queue
};
#endif

 

posted @ 2017-09-01 16:42  JadeofMoon  阅读(109)  评论(0编辑  收藏  举报