队列

来源:互联网 发布:苹果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;}

原创粉丝点击