首页 > 代码库 > C++__循环队列(练习)
C++__循环队列(练习)
循环队列
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 = http://www.mamicode.com/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 =http://www.mamicode.com/ 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 = http://www.mamicode.com/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++__循环队列(练习)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。