資料結構---迴圈佇列(順序表實現)
阿新 • • 發佈:2018-12-31
一、理論知識
待續
二、程式碼實現
1.佇列結構體定義
typedef struct lQueue
{
int* pBase;
int size;
int length;
int front;
int rear;
}lQueue,*plQueue;
2.初始化佇列
void InitlQueue(plQueue q, int size) { q->pBase = (int*)malloc(sizeof(int)*size); q->size = size; q->length = 0; q->front = 0; q->rear = 0; }
3.判斷佇列是否已滿
bool isFull(plQueue q)
{
return q->length == q->size;
}
4.判斷佇列是否為空
bool isEmpty(plQueue q)
{
return q->length == 0;
}
5.壓佇列
void PushQueue(plQueue q,int data) { if (!isFull(q)) { q->pBase[q->rear++] = data; q->length++; if (q->rear == q->size) q->rear = 0; } else { cout << "佇列已滿"<< endl; } }
6.出佇列
void PopQueue(plQueue q)
{
if (!isEmpty(q))
{
q->front++;
q->length--;
if (q->front == q->size)
q->front = 0;
}
else
{
cout << "佇列為空!"<< endl;
}
}
7.列印佇列
void ShowQueue(plQueue q) { if (!isEmpty(q)) { for (int i = q->front; i != q->rear; ++i) { cout << q->pBase[i]<<"->"; if (i == q->size) i = -1; } } else { cout << "順序表為空!"<< endl; } }