Queue (queue chain sequence and circular queue)

1. The basic concept of the queue:

Queue is a special linear form, linear form and delete operations to allow insertion in any position, but only allows insertion queues (queues) at one end while the other end of the delete operation (output queue).
One end of insertion, is referred to as the tail end, a delete operation is referred to as the first team. The queue is not empty queue when the data element referred to.
Team head and the tail of each queue head pointer (queue head pointer), and the tail pointer (queue tail pointer) points.
So is a FIFO queue table (first in first out), also known as FIFO table.

Here Insert Picture Description

2 sets of operations:

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

/*判断队列是否非空,非空返回1,空返回0*/
int QueueNotEmpty(q);

/*元素x入队列*/
void QueueAppend(q,x);

/*出队列,元素保存在x中,成功返回1,失败返回0*/
int QueuePop(q, x);

/*取队头元素,保存在x中,成功返回1,失败返回0*/
int QueueGet(q, x);

3. Realization

3.1 the queues

With a contiguous storage space to store data from a queue, the queues referred to such a queue.
But there is order in the queue "false spillover" phenomenon, that is, when the queue is full after the tail pointer to the last element space, it indicates that the queue is full. One element out of the queue, the queue at this time should not have full status for the team, but the team is still the tail pointer points to the last element of space, it indicates that the queue is full. Solve the "fake" Overflowing the queue can be recycled with the order.

Cyclic queue order 3.2

Order to resolve a false circular queue can overflow, but there is an empty team, team full of judgment conditions are rear = = front of the case
to resolve the order of empty circular queue team, team achieve full state judge has three forms:

  • Less one storage unit
    to the tail pointer is equal to 1 plus rear head pointer team to team full front
    case team full: (rear + 1)% QUEUE_MAXSIZE = = front Empty Team: front = = rear
  • Setting a flag
    set flag tag, tag is set whenever a successful enqueue, dequeue successful tag is set to 0.
    In this case the full force: rear = = front && tag = = 0 Empty Team: rear = = front && tag = = 1
  • Setting a counter
    arranged a counter COUNT, plus an enqueue, dequeue minus 1.
    At this point the team full count> 0 && front = = rear air fleet count = = 0

In fact, the counter is an upgraded version of a flag, may be between the count when the flag bit is also used
thus giving a below less storage space and is provided two kinds of counter code.

A less storage space

Code:

//#pragma once				//作为头文件时加上这行
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define MAXSIZE 50
#define DataType char
typedef struct {
	DataType queue[MAXSIZE];
	int rear;
	int front;
}SeqQueue;

void QueueInit(SeqQueue* q);
int QueueNotEmpty(SeqQueue* q);
int QueueAppend(SeqQueue* q, DataType x);
int QueuePop(SeqQueue* q, DataType *x);
int QueueGet(SeqQueue* q, DataType *x);

void QueueInit(SeqQueue* q)
{
	q->rear = 0;
	q->front = 0;
}

int QueueNotEmpty(SeqQueue* q)
{
	if (q->rear != q->front)return 1;
	else return 0;
}

int QueueAppend(SeqQueue* q, DataType x)
{
	if ((q->rear + 1) % MAXSIZE == q->front) {
		printf("队列已满,无法插入\n");
		return 0;
	}
	q->queue[q->rear] = x;
	q->rear = (q->rear + 1) % MAXSIZE;
	return 1;
}

int QueuePop(SeqQueue* q, char *x)
{
	if (QueueNotEmpty(q)) {
		*x = q->queue[q->front];
		q->front = (q->front + 1) % MAXSIZE;
		return 1;
	}
	printf("队列中元素为空,无元素出队列\n");
	return 0;
}

int QueueGet(SeqQueue* q, char *x)
{
	if (QueueNotEmpty(q)) {
		*x = q->queue[q->front];
		return 1;
	}
	printf("队列中元素为空,无元素可取\n");
	return 0;
}

Set a counter (flag)

Code:

/#pragma once				//作为头文件时加上这行
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define DataType char
#define QUEUE_MAXSIZE 30
typedef struct {
	DataType queue[QUEUE_MAXSIZE];
	int rear;
	int front;
	int count;
}SeqQueue;
void QueueInit(SeqQueue* q);
int QueueNotEmpty(SeqQueue* q);
int QueueAppend(SeqQueue* q, DataType x);
int QueuePop(SeqQueue* q, DataType *x);
int QueueGet(SeqQueue* q, DataType *x);

void QueueInit(SeqQueue* q)
{
	q->rear = 0;
	q->front = 0;
	q->count = 0;
}
int QueueNotEmpty(SeqQueue* q)
{
	if (q->count != 0)return 1;
	else return 0;
}
int QueueAppend(SeqQueue* q, DataType x)
{
	if (q->count == QUEUE_MAXSIZE) {
		printf("队列已满,无法插入\n");
		return 0;
	}
	q->queue[q->rear] = x;
	q->count++;
	q->rear = (q->rear + 1) % QUEUE_MAXSIZE;
	return 1;
}

int QueuePop(SeqQueue* q, DataType *x)
{
	if (QueueNotEmpty(q)) {
		*x = q->queue[q->front];
		q->count--;
		q->front = (q->front + 1) % QUEUE_MAXSIZE;
		return 1;
	}
	printf("队列中元素为空,无元素出队列\n");
	return 0;
}

int QueueGet(SeqQueue* q, DataType *x)
{
	if (QueueNotEmpty(q)) {
		*x = q->queue[q->front];
		return 1;
	}
	printf("队列中元素为空,无元素可取\n");
	return 0;
}

Test function:

void test()
{
	char c;
	SeqQueue q;
	QueueInit(&q);	//初始化队列
	//入队列
	QueueAppend(&q, 'T');
	QueueAppend(&q, 'h');
	QueueAppend(&q, 'i');
	QueueAppend(&q, 's');
	QueueAppend(&q, ' ');
	QueueAppend(&q, 'a');
	QueueAppend(&q, ' ');
	QueueAppend(&q, 't');
	QueueAppend(&q, 'e');
	QueueAppend(&q, 's');
	QueueAppend(&q, 't');
	//出队列,并输出。
	while (QueueNotEmpty(&q))
	{
		QueueGet(&q, &c);
		printf("%c ", c);
		QueuePop(&q, &c);
	}
}

operation result:
Here Insert Picture Description

3.3 queue chain

Code:

//#pragma once				//作为头文件时加上这行
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define DataType char

/*链式队列中结点的结构体定义*/
typedef struct queue_node{
	DataType data;
	struct queue_node* next;
}queue_node;

/*链式队列结构体*/
typedef struct {
	queue_node *front;	//队头指针
	queue_node *rear;	//队尾指针
}LinkedQueue;

/*初始化链式队列*/
void QueueInit(LinkedQueue* q);
/*判断队列是否非空,非空返回1,空返回0*/
int QueueNotEmpty(LinkedQueue* q);
/*元素x入队列*/
void QueueAppend(LinkedQueue* q, DataType x);
/*出队列,元素保存在x中,成功返回1,失败返回0*/
int QueuePop(LinkedQueue* q, DataType *x);
/*取队头元素,保存在x中,成功返回1,失败返回0*/
int QueueGet(LinkedQueue* q, DataType *x);
/*撤销队列结点的内存*/
void QueueDestroy(LinkedQueue* q);

void QueueInit(LinkedQueue* q)
{
	q->front = NULL;
	q->rear = NULL;
}

int QueueNotEmpty(LinkedQueue* q)
{
	if (q->front != NULL)return 1;
	else return 0;
}

void QueueAppend(LinkedQueue* q, DataType x)
{
	queue_node *p = (queue_node *)malloc(sizeof(queue_node));
	p->data = x;
	p->next = NULL;
	if (q->rear != NULL)q->rear->next = p;	//队列原来非空时,队尾加新结点
	q->rear = p;	//修改队尾指针
	if (q->front == NULL)q->front = p;		//队列原来为空是,修改队头指针
}

int QueuePop(LinkedQueue* q, DataType *x)
{
	queue_node *p = NULL;
	if (q->front==NULL) {
		printf("队列中元素为空,无元素出队列\n");
		return 0;
	}
	*x = q->front->data;
	p = q->front;
	q->front = p->next;	//出队列结点脱离队列
	if (q->front == NULL)q->rear = NULL;	//删除最后一个结点时要置队尾指针为空
	free(p);
	return 1;
}

int QueueGet(LinkedQueue* q, DataType *x)
{
	if (q->front == NULL) {
		printf("队列中元素为空,无元素可取\n");
		return 0;
	}
	*x = q->front->data;
	return 1;
}

void QueueDestroy(LinkedQueue* q)
{
	queue_node *p1 = NULL, *p2 = NULL;
	p1 = q->front;
	while (p1 != NULL)
	{
		p2 = p1;
		p1 = p1->next;
		free(p2);
	}
}

Test function:

void test()
{
	char c;
	LinkedQueue q;
	QueueInit(&q);	//初始化队列
	//入队列
	QueueAppend(&q, 'T');
	QueueAppend(&q, 'h');
	QueueAppend(&q, 'i');
	QueueAppend(&q, 's');
	QueueAppend(&q, ' ');
	QueueAppend(&q, 'a');
	QueueAppend(&q, ' ');
	QueueAppend(&q, 't');
	QueueAppend(&q, 'e');
	QueueAppend(&q, 's');
	QueueAppend(&q, 't');
	//出队列,并输出。
	while (QueueNotEmpty(&q))
	{
		QueueGet(&q, &c);
		printf("%c ", c);
		QueuePop(&q, &c);
	}
}

operation result:
Here Insert Picture Description

Published 24 original articles · won praise 11 · views 5368

Guess you like

Origin blog.csdn.net/weixin_44339734/article/details/89265475