本篇博客將實現循環隊列的數組結構,實現功能有入隊、出隊、計算隊列長度、判斷隊列是否爲空、爲滿等。
隊列的初始狀態如下:
初始狀態時,front=rear=0,size=0。
入隊操作示意圖如下:
入隊時,在數組原來的rear位置插入數據,插入數據後將rear+1向後移動一位,同時將隊列長度size+1。
如上圖所示,當rear已經指向數組最後一個位置時,再進行入隊操作,rear將重新回到起始位置0.
出隊操作示意圖如下所示:
當要將a元素出隊時,原本指向a的front指針將移向下一個元素,同時將size-1。要注意的是這時的a其實還在數組中,只是不在有效區域,即實際存在於隊列中的數據是front到rear之間的數組數據,而這時的a不在front到rear之間。
隊列空滿示意圖如下:
如上圖所示隊列在空和滿時,front和rear指針都將重合,因此將很難通過front和rear指針位置判斷隊列是空還是滿。爲了方便判斷可以採取用實際隊列的長度size來進行判斷,即size=0時,隊列爲空;size=capacity(數組的最大容量)時,隊列爲滿。
具體實現代碼如下:
1.頭文件CircleQueue.h
#pragma once
#ifndef CIRCLEQUEUE_H
#define CIRCLEQUEUE_H
#define NumOfQueue 10
typedef int ElementType;
struct queue
{
int Capacity;
int size;
int front;
int rear;
ElementType* Array;
};
typedef struct queue* Queue;
Queue CreatQueue();//創建空隊列;
int IsEmpty(Queue Q);//判斷隊列是否爲空;
int IsFull(Queue Q);//判斷隊列是否爲滿;
void MakeEmpty(Queue Q);//置空
int LengthOfQueue(Queue Q);//計算隊列長度
void EnQueue(Queue Q, ElementType data);//入隊
void DeQueue(Queue Q);//出隊
void PrintQueue(Q);//打印隊列
#endif // !CIRCLEQUEUE_H
2.源文件CircleQueue.c
#include<stdio.h>
#include<malloc.h>
#include"CircleQueue.h"
Queue CreatQueue()//創建空隊列;
{
Queue Q = malloc(sizeof(struct queue));
if (Q == NULL)
{
printf("out of space!!!");
exit(1);
}
Q->Array = malloc(sizeof(ElementType) * NumOfQueue);
if (!Q->Array)
{
printf("out of space!!!");
exit(1);
}
Q->front = Q->rear = 0;
Q->size = 0;
Q->Capacity = NumOfQueue;
return Q;
}
int IsEmpty(Queue Q)//判斷隊列是否爲空;
{
return Q->size ==0 ;
}
int IsFull(Queue Q)//判斷隊列是否爲滿;
{
return Q->size == Q->Capacity;
}
void MakeEmpty(Queue Q)//置空
{
Q->front = Q->rear = 0;
Q->size = 0;
}
int LengthOfQueue(Queue Q)//計算隊列長度
{
return Q->size;
}
void EnQueue(Queue Q, ElementType data)//入隊
{
if (IsFull(Q))
{
printf("Enqueue Error:the queue is full !!!");
exit(1);
}
Q->Array[Q->rear++] = data;
if (Q->rear == Q->Capacity)
Q->rear = 0;
++Q->size;
}
void DeQueue(Queue Q)//出隊
{
if (IsEmpty(Q))
{
printf("Dequeue Error: the queue is empty !!!");
exit(1);
}
++Q->front;
if (Q->front == Q->Capacity)
Q->front = 0;
--Q->size;
}
void PrintQueue(Queue Q)//打印隊列
{
if (IsEmpty(Q))
{
printf("Print warning: the queue is empty !!!");
exit(1);
}
int i = Q->front;
if(Q->rear>Q->front)
for (; i < Q->rear; i++)
{
printf("%d ", Q->Array[i]);
}
else
for (; i < Q->front+Q->size; i++)
{
printf("%d ", Q->Array[i%Q->Capacity]);
}
printf("\n");
}
3.主程序main.c
/********************************************************************************************************
Function:循環隊列的數組實現
Date:2020/06/02
*********************************************************************************************************/
#include"CircleQueue.h"
int main()
{
Queue Q=CreatQueue();//創建一個空隊列
for (int i = 0; i < 10; i++)
{
EnQueue(Q, i);//入隊
}
PrintQueue(Q);//打印隊列
printf("the length of the queue is:%d\n", LengthOfQueue(Q));//打印隊列長度
for (int i = 0; i < 4; i++)
{
DeQueue(Q);//出隊
}
PrintQueue(Q);//打印隊列
printf("the length of the queue is:%d\n", LengthOfQueue(Q));//打印隊列長度
for (int i = 0; i < 3; i++)
{
EnQueue(Q, i+10);//入隊
}
PrintQueue(Q);//打印隊列
printf("the length of the queue is:%d\n", LengthOfQueue(Q));//打印隊列長度
MakeEmpty(Q);//置空
PrintQueue(Q);//打印隊列
printf("the length of the queue is:%d\n", LengthOfQueue(Q));//打印隊列長度
return 0;
}
4.運行結果