Detailed explanation of the chain implementation of queue (Queue)----linear structure

Yesterday, I shared the chain implementation of the stack , and today I will share the chain implementation of the queue with you.

Queue is also an important restricted As the name suggests, queue is an abstract summary of phenomena in life.

For example, when we are queuing in our daily life, we usually enter the team from the end of the team , and then wait for the people at the head of the team to gradually leave the team , that is, everyone can only enter the team from the rear of the team and exit from the front of the team. Queue , only allows single entry and single exit , this is the basic feature of the queue data structure.

We call this feature of the queue FIFO (first in, first out) .

Corresponding to it is the feature of stack LIFO (last in first out) , please see the previous article for details.

The operation of entering the queue from the rear of the queue is called -----> entering the queue .
The operation of dequeuing from the front of the queue is called -----> dequeue .

Since the operation of the queue is relatively simple, I will not go into details here. If you have any doubts, please leave a comment~

The chain implementation code of the queue is directly given below :

Initialize the queue (Queue)

//数据结构---->初始化链式队列 
int initQueue(LinkQueue &queue)
{
    
    
	queue = (QueueNode *)malloc(sizeof(QueueNode));			//使用malloc函数动态为队列开辟空间 
	queue->front = NULL;				//队列头指针初始化指向NULL 
	queue->rear = NULL;					//队列尾指针初始化指向NULL 
	
	return OK;				//返回成功信息 
}

Enqueue operation (enterQueue)

Insert elements at the end of the queue, that is, enter the queue


//数据结构---->向队尾进行插入元素操作,即入队操作 
int enterQueue(LinkQueue &queue, Student student)
{
    
    
	Student *student1 = (Student *)malloc(sizeof(Student));			//动态开辟队列结点元素 
	
	student1->id = student.id; 
	student1->age = student.age;			//为待插入结点添加信息 
	
	
	if(queue->front == NULL && queue->rear == NULL)			//若队列为空队列 
	{
    
    
		queue->front = student1;			//队头指针front指向该元素 
		queue->rear = student1;				//队尾指针rear指向该元素 
	}
	else						//若队列不为空队列 
	{
    
    
		queue->rear->next = student1;		//将该元素从队列尾部插入队列 
		queue->rear = student1;				//队尾指针rear后挪,指向该元素 
	}
	
	queue->rear->next = NULL;		//队尾的下一待加入元素设置为NULL 
	
	return OK;				///返回 
}

Dequeue operation (quitQueue)

Remove and dequeue the elements at the head of the queue, that is, the dequeue operation


//数据结构---->将队列头部的元素进行去除,即出队操作 
int quitQueue(LinkQueue &queue, Student &student)
{
    
    
	Student *q = queue->front;			//定义指针q暂时保存队头元素 
	
	if(queue->front == NULL && queue->rear == NULL)		//若队列为NULL 
		return ERROR;					//返回出队失败信息 
	
	if(queue->front == queue->rear)		//若队列仅有一个元素 
	{
    
    
		queue->front = NULL; 
		queue->rear = NULL;				//直接队头、队尾指针设置为NULL,恢复空队列 
	}
	else			//若队列中的元素大于 1 (length>1) 
	{
    
    
		queue->front = queue->front->next; 		//队列的队头直接指向队列头的下一元素 
	}
	
	free(q);		//释放原队列头部 
	return OK;		//返回 
}

Get the head element of the queue (getHead)

//数据结构---->获取队列头元素 
int getHead(LinkQueue &queue, Student &student)
{
    
    
	if(queue->front != NULL)			//若队列头部存在元素 
	{
    
    
		student = *queue->front;		//保存下该元素的信息 
		return OK;					//返回结果 
	}
	else
		return ERROR;				//否则返回失败信息 
}

Output all queues from head to tail

//数据结构---->从队列头部至队列尾部输出队列 
void queueTraverse(LinkQueue &queue)
{
    
    
	int i = 0;			//定义统计量 
	Student *student = queue->front;		//定义遍历队列变量student 
	
	printf("队列头部至队列尾部所有元素的信息如下:\n");
	while(student != NULL)					//循环从头遍历至尾 
	{
    
    
		printf("id = %d, age = %d.\n", student->id, student->age);		//输出结点信息 
		i++;
		student = student->next;			//遍历指针后挪队列下一结点 
	}
	printf("共有 %d 条数据.\n\n", i);		//输出数据条数 
	
	return;			//返回 
}

Destroy and empty the queue, determine the empty queue and find the length of the queue

//数据结构---->销毁队列 
int destroyQueue(LinkQueue &queue)
{
    
    
	clearQueue(queue);		//首先将队列清空 
	free(queue);			//释放队列的地址空间 
	queue = NULL;			//队列指针设置为NULL 
	return OK;		//返回 
}


//数据结构---->清空队列元素 
int clearQueue(LinkQueue &queue)
{
    
    
	Student student;		//定义临时变量 
	while(queue->front != NULL && queue->rear != NULL)		//若队列仍有元素 
		quitQueue(queue, student);						//不断进行出队操作 
	return OK;				//返回 
}


//数据结构---->判断队列是否为空 
int queueEmpty(LinkQueue &queue)
{
    
    
	if(queue->front == NULL && queue->rear == NULL)
		return TRUE;			//若队列为NULL,返回true 
	else
		return FALSE;			//否则返回false 
}


//数据结构---->测试队列的长度 
int queueLength(LinkQueue &queue)
{
    
    
	int i = 0;			//定义计数器i并初始化为 0 
	Student *student = queue->front;			//定义指针student指向队列头元素 
	while(student != NULL)				//循环累计
	{
    
    
		i++;			//计数器累加 
		student = student->next;		//student指针不断向队尾进行移动 
	}
	return i;			//返回计数结果 
}

Well, this is the basic operation of the queue, as well as more complex queues such as double-ended queues, circular queues , etc., I will make up for it when I have time to study it later. If you have any questions, please leave a comment, come on~

Guess you like

Origin blog.csdn.net/weixin_43479947/article/details/113487086