#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; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。