码迷,mamicode.com
首页 > 编程语言 > 详细

C++__循环队列(练习)

时间:2017-05-09 00:59:44      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:int   getc   else   enqueue   generate   creat   return   ios   sig   

循环队列

 

queue.h

#ifndef QUEUE_H_
#define QUEUE_H_

#define SIZE 10

typedef int data_type;

enum QUEUE_OP {
    QUEUE_ERR = -1, QUEUE_OK, QUEUE_EMPTY
};

class QUEUE {
private:
    data_type *data;
    int count;
    int iFront;
    int iRear;
public:
    QUEUE();
    ~QUEUE();

    int getCount() const;
    data_type getData(unsigned int i) const;
    data_type *getData() const;
    int getFront() const;
    int getRear() const;
    void setCount(int count);
    void setData(data_type data, unsigned int i);
    void setData(data_type *data);
    void setFront(int front);
    void setRear(int rear);

//    QUEUE *Create();
//    void Destroy(QUEUE *pQueue);
    int EnQueue(QUEUE *pQueue, data_type tData);
    int DeQueue(QUEUE *pQueue, data_type *pData);
    int IsEmpty(QUEUE *pQueue);
    void operator delete(void *pQueue);
};

#endif /* QUEUE_H_ */

 

queue.cpp

#include "QUEUE.h"
#include <iostream>
using namespace std;

QUEUE::QUEUE() {
    // TODO Auto-generated constructor stub
    cout<<"queue"<<endl;
    this->setCount(0);
    this->setFront(0);
    this->setRear(0);
    int *data = new data_type[SIZE];
    if (!data) {
        cout << "new data[SIZE] error" << endl;
        this->setData(NULL);
    } else {
        this->setData(new data_type[SIZE]);
    }
}

QUEUE::~QUEUE() {
    // TODO Auto-generated destructor stub
    cout<<"~queue"<<endl;
}

int QUEUE::getCount() const {
    return count;
}

data_type QUEUE::getData(unsigned int i) const {
    return data[i];
}

data_type *QUEUE::getData() const {
    return data;
}

int QUEUE::getFront() const {
    return iFront;
}

int QUEUE::getRear() const {
    return iRear;
}

void QUEUE::setCount(int count) {
    this->count = count;
}

void QUEUE::setData(data_type data, unsigned int i) {
    this->data[i] = data;
}

void QUEUE::setData(data_type *data) {
    this->data = data;
}

void QUEUE::setFront(int front) {
    iFront = front;
}

void QUEUE::setRear(int rear) {
    iRear = rear;
}

int QUEUE::EnQueue(QUEUE *pQueue, data_type tData) {
    if (!pQueue)
        return QUEUE_ERR;

    if (SIZE == pQueue->getCount()) {
        cout << "queue en over" << endl;
        return QUEUE_ERR;
    }
    pQueue->setData(tData, pQueue->getRear());
    pQueue->setRear(pQueue->getRear() + 1);
    if(SIZE == pQueue->getRear())
        pQueue->setRear(0);
    pQueue->setCount(pQueue->getCount() + 1);

    return QUEUE_OK;
}

int QUEUE::DeQueue(QUEUE *pQueue, data_type *pData) {
    if ((!pQueue) || (!pData))
        return QUEUE_ERR;

    if (0 == pQueue->getCount()) {
        cout << "queue de over" << endl;
        return QUEUE_ERR;
    }
    *pData = pQueue->getData(pQueue->getFront());
    pQueue->setFront(pQueue->getFront() + 1);
    if(SIZE == pQueue->getFront())
        pQueue->setFront(0);
    pQueue->setCount(pQueue->getCount() - 1);

    return QUEUE_OK;
}

int QUEUE::IsEmpty(QUEUE *pQueue) {
    if(!pQueue)
        return QUEUE_ERR;

    if(0 == pQueue->getCount())
        return QUEUE_EMPTY;

    return QUEUE_OK;
}

void QUEUE::operator delete(void *pQueue){
    if(!pQueue)
        return;

    data_type *Tmp = ((QUEUE *)pQueue)->getData();
    free(Tmp);
    free(pQueue);

    return;
}

 

main.cpp

#include "QUEUE/QUEUE.h"
#include <iostream>
using namespace std;

void function() {
    QUEUE *pQueue = new QUEUE;
    if (!pQueue) {
        cout << "new queue error" << endl;
        return;
    }
    int i = 5;
    data_type data = 0;
    while (i--) {
        pQueue->EnQueue(pQueue, i);
        pQueue->DeQueue(pQueue, &data);
        cout << data << "  ";
    }
    if (QUEUE_OK == pQueue->IsEmpty(pQueue))
        cout << "queue ok" << endl;
    /*
     i = 9;
     while (i--) {
     pQueue->DeQueue(pQueue, &data);
     cout << data << "  ";
     }
     cout << endl;
     if (QUEUE_EMPTY == pQueue->IsEmpty(pQueue))
     cout << "queue empty" << endl;
     */
    delete pQueue;
}

int main() {
    function();
    return 0;
}

 

C++__循环队列(练习)

标签:int   getc   else   enqueue   generate   creat   return   ios   sig   

原文地址:http://www.cnblogs.com/peformer/p/6828299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!