[Data structure—implementation of queue]

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.

Article directory


Preface

世上有两种耀眼的光芒,一种是正在升起的太阳,一种是正在努力学习编程的你!一个爱学编程的人。各位看官,我衷心的希望这篇博客能对你们有所帮助,同时也希望各位看官能对我的文章给与点评,希望我们能够携手共同促进进步,在编程的道路上越走越远!


提示:以下是本篇文章正文内容,下面案例可供参考

1. Queue

1.1 Concept and structure of queue

Queue:A special linear table that only allows data insertion operations at one end, deletion data operations at the other end, The queue hasFirst In First OutFIFO (First In First Out)

Enqueue:The end where the insertion operation is performed is called the tail of the queue

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

2. Implementation of Queue

Queue (first in, first out) has three implementation solutions:Array, one-way linked list, two-way linked list
Array:The queue is put in at the tail and out at the head, but inserting at the end of the array is okay, but deleting the head requires moving the data, so it is very inconvenient
Singly linked list:It is convenient to insert the queue at the end of a singly linked list, and it is also convenient to delete the head

2.1 Implementation of header file—Queue.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

typedef int QDataType;

typedef struct QueueNode
{
	QDataType val;
	struct QueueNode* next;
}QNode;



//尾入(*单向链表,我们要找尾,进行尾插,所以我们需要把头节点和尾节点的指针传进来,
//但是要进行头删,得频繁改变第一个节点得地址,所以我们得用二级指针,这样就更麻烦了)
//void QueuePush(QNode* phead,QNode* ptail, QDataType x);
头出
//void QueuePop(QNode* phead);


typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;

//尾入(我们把第一个节点和尾节点放入一个结构体中,
//然后可以改变结构体成员,就可以实现第一个节点地址的频繁的更换)
void QueuePush(Queue* pq, QDataType x);
//头出
void QueuePop(Queue* pq);

//初始化
void QueueInit(Queue* pq);
//销毁
void QueueDestroy(Queue* pq);

//取队头的数据
QDataType QueueFront(Queue* pq);
//取队尾的数据
QDataType QueueBack(Queue* pq);

//获取队列中有效元素个数
int QueueSize(Queue* pq);
//检测队列是否为空,如果为空返回非零结果,如果非空返回0 
bool QueueEmpty(Queue* pq);

2.2 Implementation of source file—Queue.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "queue.h"

//尾入(我们把第一个节点和尾节点放入一个结构体中,
//然后可以改变结构体成员,就可以实现第一个节点地址的频繁的更换)
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}

	newnode->val = x;
	newnode->next = NULL;

	if (pq->ptail == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	pq->size++;
}
//头出
void QueuePop(Queue* pq)
{
	assert(pq);
	//如果只剩一个节点的时候,phead往后走,此时ptail就是野指针
	assert(pq->phead);

	QNode* del = pq->phead;
	pq->phead = pq->phead->next;
	free(del);
	del = NULL;

	if (pq->phead == NULL)
	{
		pq->ptail = NULL;
	}
	pq->size--;
}

//初始化
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

//销毁
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->phead;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->phead = pq->ptail = NULL;
}

//取队头的数据
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->phead);
	return pq->phead->val;
}
//取队尾的数据
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->ptail);
	return pq->ptail->val;
}

//获取队列中有效元素个数
int QueueSize(Queue* pq)
{
	assert(pq);
	return pq->size;
}
//检测队列是否为空,如果为空返回非零结果,如果非空返回0 
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	
	return pq->phead == NULL;
}

2.3 Test of source file—test.c

#include "queue.h"

int main()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	QueuePush(&q, 5);
	while (!QueueEmpty(&q))
	{
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}


	QueueDestroy(&q);
	return 0;
}

3. Display of actual data of the test queue

1. How to enter and exit the queue: insert data at the end of the queue and delete data at the end

2. The relationship between dequeuing and enqueuing: one-to-one

3.1 Ingress and egress of normal queue

3.2 There is dequeueing while entering the queue


Summarize

Okay, this blog ends here. If you have a better point of view, please leave a message in time. I will watch it carefully and learn from it.
If you don’t accumulate steps, you can’t reach a thousand miles; if you don’t accumulate small streams, you can’t become a river or sea.

Guess you like

Origin blog.csdn.net/2301_79585944/article/details/134982457