The strongest data structure in history----easy to handle the queue and the simulation implementation of the queue

1. The concept and structure of queue

Queue: A special linear table that only allows data insertion operations at one end and deletion data operations at the other end. The queue has a First In First Out (First In First Out)

Enqueue: The end that performs the insertion operation is called the tail of the queue

Dequeue: The end that performs the delete operation is called the head of the queue

image-20220326172844626

2. Implementation of the queue

The queue can be implemented in the structure of an array and a linked list. It is better to use the structure of a linked list, because if the structure of an array is used, the efficiency of dequeuing and outputting data at the head of the array will be relatively low, because the data needs to be overwritten from the back to the front.

Queue diagram structure:
insert image description here

Note: Because the implementation of the queue and the implementation of the linked list are a lot the same! Because the queue here is implemented by the linked list, the insertion of the queue is the tail insertion of the linked list, and the deletion of the queue is the deletion of the head of the linked list. No more splicing here! I hope you can simulate and implement it yourself. If you don't understand, you can refer to the code of Wanwan. Of course, the code of Wanwan is only for reference, and there is still room for optimization, such as adding a sentinel position and the like!

Queue.h header file:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int QDataType;
typedef struct QueueNode
{
	QDataType data;
	struct QueueNode* next;
}QNode;
typedef struct Queue
{
	QNode* head;
	QNode* tail;
}Queue;
void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);
void QueuePush(Queue*pq,QDataType x);
void QueuePop(Queue*pq);
size_t QueueSize(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
bool QueueEmpty(Queue* pq);

Queue.c source file

#include"Queue.h"
void QueueInit(Queue* pq)//队列的初始化
{
	assert(pq);
	pq->head =pq->tail = NULL;
}
QNode* BuyQNode(QDataType x)//开辟一个节点
{
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		printf("fail malloc\n");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
void QueuePush(Queue* pq, QDataType x)//入队
{
	assert(pq);
	QNode* newnode = BuyQNode(x);
	if (pq->tail == NULL)//没有节点的时候
	{
		assert(pq->head == NULL);
		pq->head= pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	
}
void QueuePop(Queue* pq)//出队
{
	assert(pq);
	assert(pq->tail&&pq->head);
	if (pq->head->next==NULL)//处理只有一个节点的时候
	{
		free(pq->tail);
		pq->tail = pq->head = NULL;
	}
	else//有多个节点
	{
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
}
size_t QueueSize(Queue* pq)//求队列的元素数目
{
	assert(pq);
	size_t size = 0;
	QNode* cur = pq->head;
	while (cur!= pq->tail->next)
	{
		size++;
		cur = cur->next;
	}
	return size;
}
QDataType QueueFront(Queue* pq)//求队列首节点存储的元素
{
	assert(pq);
	assert(pq->head);
	return pq->head->data;
}
QDataType QueueBack(Queue* pq)//求队列尾节点存储的元素
{
	assert(pq);
	assert(pq->tail);
	return pq->tail->data;
}
void QueueDestory(Queue* pq)//销毁队列
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}
bool QueueEmpty(Queue* pq)//判断队列是否为空
{
	assert(pq);
	return pq->head==NULL&&pq->tail==NULL;
}

Guess you like

Origin blog.csdn.net/m0_57304511/article/details/123986798