Wangdao postgraduate entrance examination data structure--4.2 circular queue

Table of contents

foreword 

1. Definition of circular queue

2. The structure of the circular queue

3. Operation of circular queue

3.1 Define the circular queue

3.2 Initialization

3.3 Joining the team

3.4 Dequeue

3.5 traverse, find the length of the table

3.6 Empty and destroy

4. Complete code


foreword 

Date: 2023.7.25

Book: 2024 Data Structure PubMed Review Guide (Wang Dao PubMed Series)

Content: Realize the basic realization of sequential queue, the main functions are as follows:
1. Data structure of circular queue
2. Entering the queue
3. Exiting the queue
4. Traversing
5. Asking for the captain

6. Empty, destroy

1. Definition of circular queue


         Sequential queues are prone to false full states during use. In order to solve this problem, a more ingenious method has been developed, which is to fabricate sequential queues as a ring-shaped space, called a circular queue.

        The relationship between the pointer and the queue elements in the circular queue remains unchanged, and we can easily realize the circular movement of the pointer only by using the modulo operation. But there is a problem in the circular queue. In the circular queue, it is impossible to judge whether the queue space is "empty" or "full" only by the fact that the head pointer front is equal to the tail pointer rear. There are two ways to deal with it: one is to set another flag to indicate Distinguish whether the queue is "empty" or "full"; the second is to use one less element space, and agree that "the queue head pointer is at the next position of the queue tail pointer (the next position of the ring)" as the queue is "full". Status flags. Method 2 is used here to solve this problem.

2. The structure of the circular queue


3. Operation of circular queue

3.1 Define the circular queue

#define MaxSize 50
typedef int ElemType;
typedef struct Queue{
	ElemType data[MaxSize]; //指向队列的存储空间
	int       front; //指向队头
	int       rear;  //指向队尾
}Queue;

3.2 Initialization

//1.初始化
void InitQueue(Queue *Q){
	Q->front = Q->rear = 0;
}

3.3 Joining the team

//入队操作
bool EnQueue(Queue *Q, ElemType x){
	//判断循环队列是否已满
	if(((Q->rear+1)%MaxSize) == Q->front)
		return false;
	//如果还有存储空间,将数据入队
	Q->data[Q->rear] = x;
    Q->rear = (Q->rear+1)%MaxSize;
    return true;
}

3.4 Dequeue

//3.出栈
//出队
bool DeQueue(Queue *Q,ElemType *x){
	//判断队列中的元素是否为空
	if(Q->front == Q->rear)
		return false;
	//如果队列中的元素不为空,进行出队操作
    *x=Q->data[Q->front];
	Q->front = (Q->front+1)%MaxSize;
    return true;
}

3.5 traverse, find the length of the table

//4.遍历队列
//打印顺序队列中的元素
bool ShowQueue(Queue *Q){
	//遍历队头到队尾中的每个元素,并将其打印输出
	for(int i=Q->front; i<Q->rear; ){
		printf("%d ",Q->data[i]);
        i = (i+1)%MaxSize;
	}
	printf("\n");
    return true;
}

//5.求队长
//获取队列元素个数
int Length(Queue *Q){
	//将尾指针位置减去头指针的位置就是队列中元素的个数
	//计算尾指针位置与头指针位置的差距
	int len= Q->rear - Q->front;
	//如果为正数,那么len就是队列的长度;如果为负数,那么MAXSIZE+len才是队列的长度
	len = (len>0) ? len : MaxSize+len;
	return len;
}

3.6 Empty and destroy


//6.清空,销毁队列
//清空队列
bool ClearQueue(Queue *Q){
	//将队头指针和队尾指针都重置为0
	Q->front = Q->rear = 0;
    return true;
}
//销毁队列
void DestroyQueue(Queue *Q){
	//释放队列的存储空间
	free(Q);
	//将队列空间的位置指针置空
	Q = NULL;
}

4. Complete code

//循环队列
#include <stdio.h>
#include <stdlib.h>

#define bool char
#define true 1
#define false 0


#define MaxSize 50
typedef int ElemType;
typedef struct Queue{
	ElemType data[MaxSize]; //指向队列的存储空间
	int       front; //指向队头
	int       rear;  //指向队尾
}Queue;

//1.初始化
void InitQueue(Queue *Q){
	Q->front = Q->rear = 0;
}

//2.入队
//入队操作
bool EnQueue(Queue *Q, ElemType x){
	//判断循环队列是否已满
	if(((Q->rear+1)%MaxSize) == Q->front)
		return false;
	//如果还有存储空间,将数据入队
	Q->data[Q->rear] = x;
    Q->rear = (Q->rear+1)%MaxSize;
    return true;
}
//3.出栈
//出队
bool DeQueue(Queue *Q,ElemType *x){
	//判断队列中的元素是否为空
	if(Q->front == Q->rear)
		return false;
	//如果队列中的元素不为空,进行出队操作
    *x=Q->data[Q->front];
	Q->front = (Q->front+1)%MaxSize;
    return true;
}
//4.遍历队列
//打印顺序队列中的元素
bool ShowQueue(Queue *Q){
	//遍历队头到队尾中的每个元素,并将其打印输出
	for(int i=Q->front; i<Q->rear; ){
		printf("%d ",Q->data[i]);
        i = (i+1)%MaxSize;
	}
	printf("\n");
    return true;
}

//5.求队长
//获取队列元素个数
int Length(Queue *Q){
	//将尾指针位置减去头指针的位置就是队列中元素的个数
	//计算尾指针位置与头指针位置的差距
	int len= Q->rear - Q->front;
	//如果为正数,那么len就是队列的长度;如果为负数,那么MAXSIZE+len才是队列的长度
	len = (len>0) ? len : MaxSize+len;
	return len;
}

//6.清空,销毁队列
//清空队列
bool ClearQueue(Queue *Q){
	//将队头指针和队尾指针都重置为0
	Q->front = Q->rear = 0;
    return true;
}
//销毁队列
void DestroyQueue(Queue *Q){
	//释放队列的存储空间
	free(Q);
	//将队列空间的位置指针置空
	Q = NULL;
}



int main(){
	Queue Q;
	InitQueue(&Q);

	for(int i=1; i<=15; ++i){
		EnQueue(&Q, i);
	}
	ShowQueue(&Q);
    int x;
	DeQueue(&Q,&x);
    printf("%d\n",x);
	EnQueue(&Q,100);
	ShowQueue(&Q);
    printf("%d\n",Length(&Q));
}

 

Guess you like

Origin blog.csdn.net/qq_58602552/article/details/131923867