Queue is a simple first-out structure, all kinds of things that need to queue, can open a queue to complete.
Using a linked list or array, you can implement the queue, but the biggest difference is that the expansion of the array is difficult, but the list is easier, but the list of resources consumption slightly more.
The different data structures lead to the implementation of the queue is not the same, the last time the list has been implemented, just simple packaging can be used, here, we introduce a simple array to simulate the way the queue:
This queue is constructed from an array of fixed lengths and holds two int numbers, which are responsible for recording the subscript index of the array.
Let's write the queue below, or C, and I'll continue with the basics of C.
Queue implementation
Review the previous knowledge, C defines the basic structure and the macro definition, we also first make the necessary definition of the queue
/* queue.h */#ifndef QUEUE_H#define QUEUE_H#include "malloc.h"typedefcharbool;typedefint QueueElementType;#define QueueSize 200typedefstruct _queue{ int head, tail; queue;#endif // QUEUE_H
Note here, we set the queue length ahead of time, and it may not be appropriate to know how long the queue should be, but later we'll show you how to calculate the maximum length that the queue needs.
There is still a definition of four functions:
/* 队列的构造函数 */queue/* 从结尾压入元素 */void QueuePush(queue* q, QueueElementType data);/* 从头部弹出元素 */QueueElementType QueuePop(queue* q);/* 判断队列是否为空 */bool QueueIsEmpty(queue* q);
Simple constructors and judgment functions are given here, and note that the condition of initialization is 0:
queue* QueueCreate() { queue* q = (queuemalloc(sizeof(queue)); 0; 0; return q;}bool QueueIsEmpty(queue* q) { return q->head == q->tail;}
The push-in Popup is also simple, but it is important to note that in order to reduce memory consumption, we want this queue to be a circular queue.
(PS. One of the Tsoi in Tangshan, there is a classic funny quote, "the queue is first-out, the loop queue is in and out." "Hey, off the topic, everyone ignores the disregard." )
As for what is the loop queue, let's take a look at the following picture:
Well, through the modulo operation to let the queue cycle movement, very convenient design, but still have to pay attention, never let tail chase head, then, the queue overflow.
So, our action function push and pop are like this:
void QueuePush(queue* q, QueueElementType data) { q->data_array[ q->tail ] = data; q->tail = ++(q->tail) % QueueSize;}QueueElementType QueuePop(queue* q) { ifreturn (QueueElementType)NULL; QueueElementType data = q->data_array[ q->head ]; q->head = ++(q->head) % QueueSize; return data;}
The use of queues is, of course, very simple, with only four functions to be manipulated:
#include <stdio.h>#include "queue.h"queue* q = NULL;int main() { q = QueueCreate(); 10); 20); 30); printf("%d\n", QueuePop(q)); printf("%d\n", QueuePop(q)); printf("%d\n", QueuePop(q)); return0;}
The complete code is described in the appendix, which is no longer more specific.
Application of queues
Say the way the queue is written, then you should describe what the queue is for. Wide search problem, is to let me first know the problem of the queue, here is to give you a lost child home problem = =, the place where the X is drawn indicates that it is impassable, to find a workable path for him.
Open a queue, record the number of steps and the current position, and then according to the map structure, each time you look at the position of the new pop-up of the queue, respectively, to four directions to search, if you have already searched for, do not find, the whole search process is like water, so you can find a feasible path.
How about this is the search problem in the classic breadth-first traversal, is it also very simple?
West wind data Structure Tutorial (2)--queue