Wangdao postgraduate entrance examination data structure--4.1. Sequential queue

Table of contents

foreword

1. Definition of sequential queue

2. The structure of the sequential queue

3. Operation of sequential queue

3.1 Define sequential queue

3.2 Initialization

3.3 Joining the team

3.4 Dequeue

3.5 Traverse to find table length

3.6 Empty, destroy the queue

4. Complete code


foreword

Date: 2023.7.25

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

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

6. Empty, destroy

1. Definition of sequential queue

        First of all, let's take a look at what is a queue?

        A queue is a first-in-first-out (FIFO) linear list that only allows insertion at one end of the list and deletion at the other end . This is consistent with the queuing in our daily life, the element that enters the queue first leaves first. The structure diagram of the queue is as follows:

       After understanding the queue, the sequential queue is very simple, and the queue represented by the sequential storage structure is simply called the sequential queue. Similar to the sequential stack, in the sequential storage structure of the queue, in addition to using a group of storage units with continuous addresses to sequentially store the elements from the head of the queue to the tail of the queue, two pointers front and rear need to be attached to indicate the elements at the head of the queue respectively and the position of the tail element of the queue.

        For the convenience of description in C language, here we agree: when initializing an empty queue, let front=rear=0, whenever a new queue tail element is inserted, the "tail pointer is incremented by 1"; whenever the queue head is deleted element, "the head pointer is incremented by 1" . Therefore, in a non-empty queue, the head pointer always points to the head element of the queue, and the tail pointer always points to the next position of the tail element of the queue. After understanding the sequential queue, we can find that the sequential queue has a big disadvantage, which is a false full state. In order to solve this problem, we can transform it into a circular queue (the circular queue will be introduced next time).


2. The structure of the sequential queue

3. Operation of sequential queue

3.1 Define a sequential 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 >= MaxSize)
		return false;
	//如果还有存储空间,将数据入队
	Q->data[Q->rear] = x;
    Q->rear++;
    return true;
}

3.4 Dequeue

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

3.5 Traverse to find table length

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

//5.求队长
//获取队列元素个数
int Length(Queue *Q){
	//将尾指针位置减去头指针的位置就是队列中元素的个数
	return (Q->rear - Q->front);
}

3.6 Empty, destroy the queue


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

//5.求队长
//获取队列元素个数
int Length(Queue *Q){
	//将尾指针位置减去头指针的位置就是队列中元素的个数
	return (Q->rear - Q->front);
}

//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<=8; ++i){
		EnQueue(&Q, i);
	}
	ShowQueue(&Q);
    int x;
	DeQueue(&Q,&x);
    printf("%d\n",x);
	EnQueue(&Q,10);
	ShowQueue(&Q);
}

Guess you like

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