その場合は少量のデータで使用するための循環キューの配列は、あなたはすぐにチームとチーム内の要素を実現することができます。チームとチームは最初のうちの最初の原則に従う(FIFO)があります。次のような構造が構成されています。
typedef int datatype;
typedef struct _loopQueue {
datatype* data;
int front;
int rear;
}loopQueue;
図前記基本動作であるとして次の
サイクルチーム含まチーム、循環シフトlq->リア=(lq->後の終了後チーム + 1)%MAX_SIZEの
サイクルチーム記載されているチームのチームサイクリックシフトヘッド後lq->フロント=(LQを- >フロント+ 1)&MAX_SIZE
円形キュー空チームlq->フロント= = lq->リア 0; 場合//は同一の位置にポイントを初期化
円形キュー(lq->リア+ 1)%満杯である MAX_SIZE = lq->フロント
キューの長演算、2例:もしlq->リア> lq->フロント
キュー長lq->リアlq->フロント;場合lq-> rearfront、キュー長がMAX_SZIE-lq->フロントlq->リア)、即ちlq->リアlq->フロント+ MAX_SIZE、
上記2例(lq->リアlq->フロントの均一な長さ + MAX_SIZE)%のMAX_SIZE
以下を達成するための完全なコード:
#include<Windows.h>
#include<iostream>
using namespace std;
#define MAX_SZIE 5
typedef int datatype;
typedef struct _loopQueue {
datatype* data;
int front;
int rear;
}loopQueue;
bool initLoopQueue(loopQueue* &lq);
bool isFull(loopQueue* &lq);
int getLength(loopQueue* &lq);
void loopQueuePrint(loopQueue*& lq);
bool loopQueueEnter(loopQueue*& lq, datatype data);
bool loopQueueDelete(loopQueue*& lq, datatype &data);
int main() {
loopQueue* lq;
if(!initLoopQueue(lq)) {
cout << "初始化失败!" << endl;
exit(1);
}
for (int i = 0; i < 10; i++) {
if (loopQueueEnter(lq, i)) {
cout << "入队成功:"<<i<<endl;
}
else {
cout << "入队失败:" << i << endl;
}
}
cout << "队列的长度为:" << getLength(lq)<<endl;
int data;
for (int i = 0; i < 2; i++) {
if (loopQueueDelete(lq, data)) {
cout << "出队成功:" << data << endl;
}
else {
cout << "出队失败:" << endl;
}
}
for (int i = 10; i < 15; i++) {
if (loopQueueEnter(lq, i)) {
cout << "入队成功:" << i << endl;
}
else {
cout << "入队失败:" << i << endl;
}
}
cout << "队列的长度为:" << getLength(lq) << endl;
loopQueuePrint(lq);
system("pause");
return 0;
}
bool initLoopQueue(loopQueue* &lq) {
lq = new loopQueue;
if (!lq) return false;
lq->data = new datatype[MAX_SZIE];
memset(lq->data, 0, MAX_SZIE);
lq->front = lq->rear = 0;
return true;
}
bool isFull(loopQueue*& lq) {
if (!lq) return false;
if ((lq->rear + 1) % MAX_SZIE == lq->front)
return true;
else
return false;
}
int getLength(loopQueue*& lq) {
if (!lq) return false;
return (lq->rear - lq->front + MAX_SZIE) % MAX_SZIE;
}
void loopQueuePrint(loopQueue* &lq) {
if (!lq || lq->front == lq->rear) return;
int tmp = lq->front;
while (1) {
datatype num = lq->data[tmp];
cout << num << " ";
tmp=(tmp+1)% MAX_SZIE;
if (tmp == lq->rear) break;
}
}
bool loopQueueEnter(loopQueue*& lq, datatype data) {
if (!lq || isFull(lq)) return false;
lq->data[lq->rear] = data;
lq -> rear = (lq->rear + 1) % MAX_SZIE;
return true;
}
bool loopQueueDelete(loopQueue*& lq, datatype& data) {
if (!lq || lq->front == lq->rear) return false;
data = lq->data[lq->front];
lq->front = (lq->front + 1) % MAX_SZIE;
return true;
}