C++实现循环队列

这篇文章主要为大家详细介绍了C++实现循环队列,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现循环队列的具体代码,供大家参考,具体内容如下

circularQueue.h

 #pragma once #pragma once #ifndef CIRCULARQUEUE_H #define CIRCULARQUEUE_H #include #include using std::cout; using std::cin; using std::endl; using std::ostream; template class cirQueue; template class cirQueue { public: cirQueue(int sz); ~cirQueue(); void push(const T& elem);//进队 void pop(T& elem);//出队 bool empty();//查看队列是否为空 int getSize();//返回队列中元素的个数 void clearQueue();//清空队列中的元素 void print();//打印队列中的元素 int getfront() { return front; } int getrear() { return rear; } bool getTop(T& elem);//读取队列首个元素 template friend ostream& operator<<(ostream& os, cirQueue& queue); private: bool _full()const;//判断队列是否已满 int maxsize;//队列最大的空间 T* element;//存放于队列中的元素数组 int front;//模拟队头指针 int rear;//模拟队尾指针 }; template cirQueue::cirQueue(int sz) { maxsize = sz; element = new T[maxsize]; if (element == nullptr) cout << "内存分配失败" << endl; front = 0; rear = 0; } template cirQueue::~cirQueue() { if (element != nullptr) delete element; } //进队 template void cirQueue::push(const T& elem) {//需要保证队尾指针位置与首个元素相差一个位置 if (rear > (maxsize - 1)) rear -= maxsize ; if (front > (maxsize - 1)) front -= maxsize ; if (!_full()) {//队列未满的情况 element[rear++] = elem;//队尾向后移动一位 //++rear; } else { cout << "队列已满,不能插入!" << endl; return; } } //出队 template void cirQueue::pop(T& elem) { if (rear > (maxsize - 1)) rear -= (maxsize - 1); if (front > (maxsize - 1)) front -= (maxsize - 1); if (!empty()) {//队列未空的情况 elem = element[front++];//队头向后移动一位 element[front - 1] = 0;//置零 } else { cout << "队列已空!" << endl; return; } } //查看队列是否为空 template bool cirQueue::empty() { if (front == rear)//待定 return true; return false; } //返回队列中元素的个数 template int cirQueue::getSize() { int num = 0; if (front <= rear) return rear - front; else return maxsize - front + rear + 1; } //清空队列中的元素 template void cirQueue::clearQueue() { if (!empty()) { int Index = 0; while (front  void cirQueue::print() {//与clearQueue函数原理一致,将front替换为Index if (!empty()) { int Index = front; while (Index  bool cirQueue::getTop(T& elem) { if (!empty()) { elem = element[front]; return true; } return false; } template ostream& operator<<(ostream& os, cirQueue& queue) { os << "队列中的元素数量为:" << queue.getSize() << endl; return os; } //判断队列是否已满 template bool cirQueue::_full()const { if (front - rear == 1 || front - rear == -maxsize + 1) return true; return false; } #endif // !CIRCULARQUEUE_H

main.cpp

 #include"CircularQueue.h" int main() { cirQueue cq(20); int a = 0; for (int i = 0; i <19; i++) { cq.push(i); } cq.print(); cout << cq; for (int i = 0; i <20; i++) { cq.pop(a); } cout << cq;//此时front=rear=19 cout << cq.getfront() << "  " << cq.getrear() << endl; //for (int i = 19; i <25; i++) //{ // cq.push(i); //} cq.push(19); cq.print(); cout << cq.getfront() << "  " << cq.getrear() << endl; cout << endl << endl; cq.push(20); cq.getTop(a); cout << a << endl; cq.print(); cout << cq.getfront() << "  " << cq.getrear() << endl; return 1; }

以上就是C++实现循环队列的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言