#pragma once
#include <iostream>
using namespace std;
class AQueue {
private:
int maxSize;
int front;
int rear;
int *listArray;
public:
AQueue(int size) {
maxSize = size + 1;
front = 1;
rear = 0;
listArray = new int[maxSize];
}
~AQueue() {
delete[] listArray;
}
void clear() {
rear = 0;
front = 1;
}
void enqueue(const int& it) {
if ((rear + 2) % maxSize == front)cout << " the queue is full" << endl;
else{
rear = (rear + 1) % maxSize;
listArray[rear] = it;
}
}
int length()const {
return ((rear + maxSize) - front + 1) % maxSize;
}
int dequeue() {
if (length() == 0)cout << " the queue is empty" << endl;
else {
int it = listArray[front];
front = (front + 1) % maxSize;
return it;
}
}
int frontValue()const {
return listArray[front];
}
};
转载于:https://my.oschina.net/u/3772904/blog/1615808