头文件:
#pragma once #include <iostream> #include <assert.h> using namespace std; template<class Type> class CQueue { public: CQueue(size_t sz = INIT_SZ); ~CQueue(); public: bool full()const; bool empty()const; void show()const; bool push(const Type &x); bool pop(); void gettop(Type &x); int length()const; void clear(); void destory(); void quit_system(Type &x); private: enum{ INIT_SZ = 8 }; Type *base; int capacity; int head; int tail; }; template<class Type> CQueue<Type>::CQueue(size_t sz = INIT_SZ) { capacity = sz > INIT_SZ ? sz : INIT_SZ; base = new Type[capacity]; assert(base != NULL); head = 0; tail = 0; } template<class Type> CQueue<Type>::~CQueue() { destory(); } template<class Type> bool CQueue<Type>::full()const { // 浪费一个空间 if ((tail + 1) % capacity == head) return true; else return false; } template<class Type> bool CQueue<Type>::empty()const { return (tail == head); } // 显示 template<class Type> void CQueue<Type>::show()const { if (tail == head) { cout << "the queue is empty!" << endl; return; } for (int i = head; i != tail; i = (i + 1) % capacity) { cout << base[i] << endl; } } // 入队 template<class Type> bool CQueue<Type>::push(const Type &x) { if (full()) { cout << "the queue is full,can not enter!" << endl; return false; } else { base[tail] = x; tail = (tail + 1) % capacity;// 让队列循环起来 return true; } } // 出队 template<class Type> bool CQueue<Type>::pop() { if (empty()) { cout << "the queue is empty,can not pop!" << endl; return false; } else { head = (head + 1) % capacity; return true; } } // 获得队头 template<class Type> void CQueue<Type>::gettop(Type &x) { x = base[head]; } // 队列的长度 template<class Type> int CQueue<Type>::length()const { if ((tail - head) > 0) return (tail - head); else return(head - tail); } // 清空队列 template<class Type> void CQueue<Type>::clear() { head = tail = 0; } // 摧毁队列 template<class Type> void CQueue<Type>::destory() { delete[]base; base = NULL; capacity = head = tail = 0; } // 退出系统 template<class Type> void CQueue<Type>::quit_system(Type &x) { x = 0; }
主函数:
#include "CQueue.h" int main() { CQueue<int> myqueue; int input = 1; int value; while (input) { cout << "****************************************************" << endl; cout << "* [1] show [2] push *" << endl; cout << "* [3] pop [4] gettop *" << endl; cout << "* [5] length [6] clear *" << endl; cout << "* [7] destory [8] quit_syntem *" << endl; cout << "****************************************************" << endl; cout << "please choose:"; cin >> input; switch (input) { case 1: myqueue.show(); break; case 2: cout << "please enter the number:"; while (cin >> value,value != -1) { myqueue.push(value); } break; case 3: myqueue.pop(); break; case 4: myqueue.gettop(value); cout << value << endl; break; case 5: cout << myqueue.length() << endl; break; case 6: myqueue.clear(); break; case 7: myqueue.destory(); break; case 8: myqueue.quit_system(input); break; default: break; } } return 0; }
清空:
获得队首元素:
求长度:
出队:
入队:
退出系统:
原文链接:https://www.f2er.com/datastructure/382648.html