MyBatis-Plus學習(一)
阿新 • • 發佈:2021-07-20
迴圈佇列
迴圈佇列
可以解決順序佇列的偽上溢位問題
程式碼
#include<iostream> #include<algorithm> #include<cstring> using namespace std; #define MaxSize 10 typedef struct{ int data[MaxSize]; int front,rear; }SqQueue; //初始化 void InitQueue(SqQueue &Q){ Q.rear = Q.front = 0; } //判斷隊空 bool QueueEmpty(SqQueue &Q){ if (Q.rear == Q.front) return true; else return false; } //入隊 bool EnQueue(SqQueue &Q, int x){ if ((Q.rear + 1) % MaxSize == Q.front) return false; Q.data[Q.rear] = x; Q.rear = (Q.rear + 1) % MaxSize; return true; } //出隊 bool DeQueue(SqQueue &Q, int &x){ if (Q.rear == Q.front) return false; x = Q.data[Q.front]; Q.front = (Q.front + 1) % MaxSize; return true; } //讀隊首元素 bool GetHead(SqQueue &Q,int &x){ if (Q.rear == Q.front) return false; x = Q.data[Q.front]; return true; } int main(){ SqQueue Q; InitQueue(Q); int n; cin >> n; for (int i = 0; i < n; i ++) { int x; cin >> x; EnQueue(Q,x); } cout << "Q.front: "<<Q.front << " " << "Q.rear: " << Q.rear << endl; for (int i = 0; i < n; i ++) { int x; DeQueue(Q,x); cout << x << " "; } cout << endl << "Q.front: "<<Q.front << " " << "Q.rear: " << Q.rear << endl; return 0; } /* 4 1 0 2 4 /*