#include "stdafx.h"
#include<iostream>
using namespace std;
#define MAXQSIZE 100
typedef int QElemType;
typedef enum Status {
success, fail, fatal, rangeerror, overflow
}Status;
typedef struct {
QElemType *base;//初始化动态分配存储空间
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue &q) {
q.base = (QElemType*)malloc(MAXQSIZE * sizeof(QElemType));
if (q.base == NULL) exit(OVERFLOW);
q.rear = q.front = 0;
return success;
}
int QueueLength(SqQueue q) {
return (q.rear - q.front+ MAXQSIZE)% MAXQSIZE;
}
Status EnQueue(SqQueue &q, QElemType elem) {
if ((q.rear + 1) % MAXQSIZE == q.front) return overflow;
q.base[q.rear] = elem;
q.rear = (q.rear + 1) % MAXQSIZE;
return success;
}
Status DeQueue(SqQueue &q, QElemType &elem) {
if (q.rear == q.front) return fail;
elem = q.base[q.front];
q.front = (q.front + 1) % MAXQSIZE;
return success;
}
void PrintQueue(SqQueue q) {
for(int i=q.front;i<q.rear;i++)
{
cout << q.base[i] << " ";
}
cout << endl;
}
int main() {
SqQueue q;
InitQueue(q);
int arr[] = { 2,5,3,7 };
int len = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < len; i++) {
EnQueue(q, arr[i]);
}
PrintQueue(q);
QElemType elem;
DeQueue(q, elem);
cout << "删除的元素为:" << elem <<endl;
system("pause");
return 0;
}