Data structure - implementation of queue (C language version)

Preface

        A queue is a special linear table that only allows data to be inserted at one end, and a special linear table that deletes data at the other end. The queue has the characteristics of first-in-first-out (FIFO), and one end of the insertion operation It is called the tail of the queue, and the end where the deletion operation is performed is called the head of the queue.

1. Characteristics of the queue

        End of queue: The element is queued at the end of the queue. Insert operation.

        Head of the team: The element is paired at the head of the team. delete operation.

As shown in the picture:

2. Implementation of the queue

         The queue can be implemented with 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 will be relatively low, and the data needs to be moved. Therefore, the method of a linked list is used here. To implement the queue.

//queue.h

#include<stdlib.h>
#include<assert.h>
#include<stdio.h>
#include<stdbool.h>
typedef int QDataType;
typedef struct QueueNode
{
	struct QueueNode* _next;
	QDataType _data;
}QueueNode;
typedef struct  Queue//队列的结构
{
	QueueNode* _head;//头指针
	QueueNode* _tail;//尾指针
}Queue;

void QueueInit(Queue* qu);//初始化队列

void QueueDestory(Queue* qu);//摧毁队列

void QueuePush(Queue* qu,QDataType data);//入队

void QueuePop(Queue* qu);//出队

QDataType QueueFront(Queue* qu);//返回队头元素
QDataType QueueBack(Queue* qu);//返回队尾元素

size_t QueueSize(Queue* qu);//队列长度

bool QueueEmpty(Queue* qu);//判断队列是否为空

//queue.c

void QueueInit(Queue* qu)//初始化队列
{
	qu->_head = qu->_tail = NULL;
}
void QueueDestory(Queue* qu)//摧毁队列
{
	//确保指针有效
	assert(qu);
	QueueNode* cur = qu->_head;
	while (cur)
	{
		QueueNode* next = cur->_next;
		free(cur);
        cur = cur ->next;//迭代继续向后走
	}
}
void QueuePush(Queue* qu,QDataType data)//入队
{
	if (qu->_head == NULL)//第一次入队,头结点为空
	{
		qu->_head = (QueueNode*)malloc(sizeof(QueueNode));
		qu->_tail = qu->_head;
		qu->_head->_next = NULL;
		qu->_head->_data = data;
	}
	else
	{
		//尾部入数据
		QueueNode* cur = qu->_tail;
		QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
		cur->_next = newNode;
		newNode->_next = NULL;
		qu->_tail = newNode;
		newNode->_data = data;
	}
}
void QueuePop(Queue* qu)//出队
{
	//队头出数据
	QueueNode* head = qu->_head;
	qu->_head = head->_next;
	free(head);
}
QDataType QueueFront(Queue* qu)//返回队头元素
{
	return qu->_head->_data;
}
QDataType QueueBack(Queue* qu)//返回队尾元素
{
	return qu->_tail->_data;
}
size_t QueueSize(Queue* qu)//队列长度
{
	assert(qu);//确保指针存在
	QueueNode* cur = qu->_head;
	size_t size = 0;
	while (cur)
	{
		++size;
		cur = cur->_next;
	}
	return size;
}
bool QueueEmpty(Queue* qu)//判断队列是否为空
{
	return !qu->_head;
}

3. Test part

        

void TestQueue()
{
	Queue qu;
	QueueInit(&qu);
	QueuePush(&qu, 1);
	QueuePush(&qu, 2);
	QueuePush(&qu, 3);
	QueuePush(&qu, 4);
	QueuePush(&qu, 5);
	QueuePush(&qu, 6);
	QueuePush(&qu, 7);
	QueuePush(&qu, 8);
	while (!QueueEmpty(&qu))
	{
		printf("%d ", QueueFront(&qu));
		QueuePop(&qu);
	}
	QueueDestory(&qu);
}

Guess you like

Origin blog.csdn.net/m0_68641696/article/details/132258483