#ifndef SEQUEUE_H #define SEQUEUE_H #include<iostream> using namespace std; class SeQueue{ public: SeQueue(int size=defaultMaxsize){ if(size>0){ maxsize=size; data=new int[maxsize]; for(int i=0;i<maxsize;++i){ data[i]=0; } rear=front=-1; } } //判断队列是否为空 bool Empty_SeQueue(SeQueue *s); //出队 bool Pop_SeQueue(SeQueue *s,int *x); //入队 int Push_SeQueue(SeQueue *s,int x); private: //默认长度 const static int defaultMaxsize=10; //队列元素 int *data; //最大长度 int maxsize; //队首和队尾元素 int rear,front; }; bool SeQueue::Empty_SeQueue(SeQueue *s){ if(s->rear==-1 && s->front==-1){ return true; }else{ return false; } } bool SeQueue::Pop_SeQueue(SeQueue *s,int *x){ if(s->Empty_SeQueue(s)){ return false; } *x=s->data[++s->front]; return true; } int SeQueue::Push_SeQueue(SeQueue *s,int x){ s->data[++s->rear]=x; return 1; } #endif
#include<iostream> #include"SeQueue.h" using namespace std; int main(){ int data[10]={7,1,2,3,4,5,6,7,8,9},a; int *p=&a; SeQueue *s=new SeQueue(); for(int i=0;i<=5;++i){ s->Push_SeQueue(s,data[i]); } if(s->Empty_SeQueue(s)){ cout<<"入队失败"<<endl; }else{ cout<<"入队成功"<<endl; } s->Pop_SeQueue(s,p); cout<<"出队的元素是:"<<*p<<endl; return 0; }