1019

#include <iostream>
#include <cstdlib>
using namespace std;
typedef int ElemType;
#define MAX_SIZE 100
#define INIT_SIZE 100
#define ERROR 0
#define OK 1
typedef struct {
ElemType *base;
int front;
int rear;
}SqQueue;

bool isFull(SqQueue & q){
return (q.rear+1)%MAX_SIZE ==q.front;
}
bool isEmpty(SqQueue & q){
return (q.rear==q.front) ;
}
bool initQueue(SqQueue & q){
q.base=(ElemType*)malloc(INIT_SIZE*sizeof(ElemType));
if(!q.base) exit(ERROR);
q.front=q.rear=0;
return OK;
}

bool put(SqQueue & q,ElemType e){
if(isFull(q)) return ERROR;
q.base[q.rear]=e;
q.rear=(q.rear+1)%MAX_SIZE;
return OK;
}

bool pop(SqQueue & q,ElemType &e){
if(isEmpty(q)) return ERROR;
e=q.base[q.front];
q.front=(q.front+1)%MAX_SIZE;
return OK;
}
int sizeOfQueue(SqQueue & q){
return (q.rear-q.front+MAX_SIZE)%MAX_SIZE;
}
int main(){
SqQueue sq;
initQueue(sq);
int k;
while(1){
cout<<"请输入操作代码\n";
cout<<"1 插入,2 弹出队首,3获取队列长度,4.退出\n";
cin>>k;
switch(k){
case 1 :
int m;cout<<"请输入要插入的数字\n"; cin>>m;put(sq,m);continue;
case 2: int e ;pop(sq,e); cout<<"队首"<<e<<"已弹出\n"; continue;
case 3: cout<<"元素个数为"<<sizeOfQueue(sq)<<"\n";continue;
case 4: return 0;
}
}
return 0;
}

posted @ 2021-10-19 16:47  pursuit_purity  阅读(75)  评论(0)    收藏  举报