队列
来源:互联网 发布:苹果cms监控软件手机版 编辑:程序博客网 时间:2024/06/16 18:18
#include <iostream>#include <string.h>using namespace std;#define MAXSIZE 10template<class T>class queue{public:queue();queue(const queue& s);bool isEmpty();bool isFull();bool EnQueue(T data);bool DeQueue(T& data);void show();int length();private:T *buf;int front;int rear;};template<class T>void queue<T>::show(){for(int i = front;i<rear;i++){cout<<this->buf[i]<<" ";}cout<<endl;}template<class T>int queue<T>::length(){return (this->rear+1-this->front)%MAXSIZE;}template<class T>queue<T>::queue(){this->rear = 0;this->front = 0;this->buf = new T[MAXSIZE];memset(this->buf,0,sizeof(T)*MAXSIZE);}template<class T>queue<T>::queue(const queue& s){this->buf = new T[MAXSIZE];memset(this->buf,0,sizeof(T)*MAXSIZE);memcpy(this->buf,s.buf,sizeof(T)*MAXSIZE);this->front = s.front;this->rear = s.rear;}template<class T>bool queue<T>::isEmpty(){if(this->front == this->rear)return true;elsereturn false;}template<class T>bool queue<T>::isFull(){if((this->rear+1)%MAXSIZE == this->front)return true;elsereturn false;}template<class T>bool queue<T>::EnQueue(T data){if(this->isFull())return false;memcpy(&this->buf[this->rear],&data,sizeof(T));this->rear= (this->rear+1)%MAXSIZE;return true;}template<class T>bool queue<T>::DeQueue(T& data){if(this->isEmpty())return false;memcpy(&data,&this->buf[this->front],sizeof(T));this->front = (this->front+1)%MAXSIZE;return true;}int main(){queue<int> s;s.EnQueue(1);s.EnQueue(2);s.EnQueue(3);s.EnQueue(4);s.show();int data;s.DeQueue(data);cout<<data<<endl;s.show();return 0;}
阅读全文
0 0
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 【poj3070】 Fibonacci 【矩阵乘法】
- 欢迎使用CSDN-markdown编辑器
- [FAQ19857]采用Signature Scheme v2签名方式的APK预置进系统失败
- js键盘事件全面控制详解
- 将图片数据转化为LMDB数据
- 队列
- POJ1182 食物链(并查集)
- 接口的多继承和实现
- MDO3054示波器的使用
- Eclipse中用git解决冲突----避免每次重新拉代码
- css3选择器、背景、边框、渐变、阴影以及文本效果的介绍及实现
- Webpack学习总结
- solr优缺点
- POJ 2229 Sumsets