The basic function and queue detailed code language C (with detailed comments)

The concept and structure of the queue

Queue: only one end of the insertion operation data, delete data operation at the other end of the special linear table , queue having the FIFO rule FIFO (First In First Out)
queues: insertion operation is called the tail end of
the queue: end delete operation is referred to as head-of-
fact, another way is to queue queuing problem in our civilized life, no matter what the general always follow first come first served,
is the first to the (head) to get to the resources, not later quasi-jump the queue, can only wait in line in the final surface (the tail).

as the picture shows:
Queuing Concepts FIG.

Queue implementation:

Queue structure can also achieve the chain array and using the linked list structure to achieve some of the better , because using the array structure, the data array of the queue head, the following data will be 11 moves, the efficiency will be relatively low.
The following list queue structure with
written procedures recommended multi-file mode, not only conducive to reading and viewing, but also for the future also help the company enter
the header file:
1. Chain queue structure and representation
statement 2. Basic functions

#pragma once

#include <stdio.h>
#include <malloc.h>

typedef int QuDataType;
// 链式结构:表示队列
typedef struct QListNode
{
	struct QListNode* _next;
	QuDataType _data;
}QueueNode;

// 队列的结构
typedef struct Queue
{
	QueueNode* _front;
	QueueNode* _rear;
}Queue;

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

// 队尾入队列
void QueuePush(Queue* q, QuDataType data);

// 队头出队列
void QueuePop(Queue* q);

// 获取队列头部元素
QuDataType QueueFront(Queue* q);

// 获取队列队尾元素
QuDataType QueueBack(Queue* q);

// 获取队列中有效元素个数
int QueueSize(Queue* q);

// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* q);

// 销毁队列
void QueueDestroy(Queue* q);

Function definition file
definitions of the basic functions of the realization of
concrete in it should be noted that the comment in

#include "Queue.h"
//一个数据入队列必须要先创建节点
QueueNode * BuyQueueNode(QuDataType x)    //创建节点并初始化此节点
{
	QueueNode * cur = (QueueNode *)malloc(sizeof(QueueNode));
	cur->_data = x;
	cur->_next = NULL;
	return cur;
}
void QueueInit(Queue* q) //初始化队列结构
{
	q->_front = NULL;
	q->_rear = NULL;
}
void QueuePush(Queue* q, QuDataType x)  //队列尾部入数据
{
	QueueNode * cur = BuyQueueNode(x);  //先把创建好的节点传过来
	if (q->_front == NULL)             //若是队列本身为空,队列里就只有这一个节点,又为队列头又为队列尾
	{
		q->_front = q->_rear = cur; 
	}
	else
	{
		q->_rear->_next = cur; //否则,链表尾插操作
		q->_rear = cur;
	}
}
void QueuePop(Queue* q)     //队列头部出数据
{
	if (q->_front == NULL)    //本身队列为空,不做操作
	{
		return;
    }
	QueueNode* tmp = q->_front->_next;   //先保留下一个节点,防止断链
	free(q->_front);
	q->_front = tmp;   //更新对列头部
}
QuDataType QueueFront(Queue* q)   //获取队列首部元素
{
	return q->_front->_data;
}
QuDataType QueueBack(Queue* q)//获取队列尾部元素
{
	return q->_rear->_data;
}
int QueueEmpty(Queue* q)   //判断队列是否为空
{
	return q->_front == NULL;   //为空,返回1
}
int QueueSize(Queue* q)  //获取队列中的元素个数
{
	QueueNode * cur;
	int count = 0;
	for (cur = q->_front; cur; cur = cur->_next)//循环遍历,计数即可
	{
		count++;
	}
	return count;
}
void QueueDestory(Queue* q)  //销毁队列
{
	if (q->_front == NULL)
	{
		return;
	}
	while (q->_front)
	{
		QueuePop(q);//对每一个元素迭代出队即可
	}
}

Simple test function implemented

#include "Queue.h"

int main()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);

	printf("%d\n", QueueFront(&q)); //1
	QueuePop(&q);//出队列
	printf("%d\n", QueueFront(&q));//2   此时队首为2

	printf("%d\n", QueueBack(&q));//4  队尾为4

	return 0;
}
Published 29 original articles · won praise 8 · views 1998

Guess you like

Origin blog.csdn.net/qq_44785014/article/details/104089691