C++ 泛型 编写的 数据结构 队列
2023-09-11 14:19:20 时间
平时编程里经常需要用到数据结构,比如 栈和队列 等, 为了避免每次用到都需要重新编写的麻烦现将 C++ 编写的 数据结构 队列 记录下来,以备后用。
将 数据结构 队列 用头文件的形式写成,方便调用。
#ifndef QUEUE_CLASS #define QUEUE_CLASS #include<iostream> #include<cstdlib> using namespace std; const int MaxQSize=50; template <class T> class Queue { private: int front, rear, count; T qlist[MaxQSize]; public: Queue(void); void QInsert(const T &item); T QDelete(void); void ClearQueue(void); T QFront(void) const; int QLength(void) const; int QEmpty(void) const; int QFull(void) const; }; //默认构造函数 template <class T> Queue<T>::Queue(void):front(0), rear(0), count(0) {} template <class T> void Queue<T>::QInsert(const T &item) { if(count==MaxQSize) { cerr<<"Queue overflow!"<<endl; exit(1); } count++; qlist[rear]=item; rear=(rear+1)%MaxQSize; } template <class T> T Queue<T>::QDelete(void) { T temp; if(count==0) { cerr<<"Deleting from an empty queue!"<<endl; exit(1); } count--; temp=qlist[front]; front=(front+1)%MaxQSize; return temp; } template <class T> T Queue<T>::QFront(void) const { return qlist[front]; } template <class T> int Queue<T>::QLength(void) const { return count; } template <class T> int Queue<T>::QEmpty(void) const { return count==0; } template <class T> int Queue<T>::QFull(void) const { return count==MaxQSize; } template <class T> void Queue<T>::ClearQueue(void) { front=0; rear=0; count=0; } #endif
具体的调用形式:
运行结果:
相关文章
- C++ 强制让目标进程执行自己的 ShellCode
- C++ 删除字符串中的子串
- 托管C++线程锁实现 c++11线程池
- 固定尺寸内存块的缓冲队列类及C++实现源代码
- C++端口扫描简单入门
- 【c++】:“无敌的适配器来咯“栈和队列模拟实现以及优先级队列的模拟实现。
- 循环队列实现(C++) Ring Buffer
- 《C++并发编程实战》——1.3 在C++中使用并发和多线程
- 【C++】优先级队列priority_queue/仿函数(函数对象)
- 纪念逝去的岁月——C++实现一个队列(使用类模板)
- 186、【栈与队列】leetcode ——503. 下一个更大元素 II(C++版本)
- 87、【栈与队列】leetcode ——347. 前 K 个高频元素:优先队列(小根堆)+Hash表(C++版本)
- 83、【栈与队列】leetcode ——20. 有效的括号(C++版本)
- 81、【栈与队列】leetcode ——232. 用栈实现队列(C++版本)
- 46、【树和二叉树】并查集——合并集合 (C/C++版)
- C++创建类的对象(类的初始化)的方法