There are many queues in the life of the shadow, such as playing rice queue, buy train tickets queue problems, can be said with time-related issues, generally involved in the queue problem; from life, you can abstract the concept of the queue, the queue is a can achieve "FIFO" storage structure. Queues are divided into chain queues and static queues, and static queues are usually implemented in arrays, but the queue must be a circular queue at this time, otherwise it will cause huge memory waste; Chained queues are implemented using linked lists. Here is the circular queue, first we must understand the following issues
First, the basic knowledge of the cyclic queue
1. The loop queue requires several parameters to determine
Loop queue requires 2 parameters, front and rear
2. Meaning of each parameter of the cyclic queue
(1) The front and rear values are zero when the queue is initialized;
(2) When the queue is not empty, front points to the first element of the queue, rear points to the next position of the last element of the queue;
(3) When the queue is empty, the value of front and rear is equal, but not necessarily zero;
3. The loop team is included in the team's pseudo algorithm
(1) The position where the value exists rear;
(2) rear= (rear+1)%maxsize, where maxsize represents the length of the array;
Program code:
if (Isfullqueue (Q)) return 0 ; Else { Q->pbase[q->rear]=val; Q->rear= (q->rear+1)%q->maxsize; } return 1;
4. The Loop team lists the team's pseudo algorithm
(1) Save the value of the team first;
(2) front= (front+1)%maxsize, where maxsize represents the length of the array;
Program code:
if (Isemptyqueue (Q)) { return0; } Else { *val=q->pbase[q->Front]; Q->pbase[q->front]=0; Q->front= (q->front+1)%q->maxsize; } return 1;
5. How to determine if the loop queue is empty
if (front==rear)
Queue empty;
Else
Queue is not empty
Program code:
if (q->front==q->rear) // determines whether the empty return 1 ; Else return 0;
6. How to determine if the loop queue is full
This problem is more complex, assuming that the array has a storage space of 7, at this time has been stored 1,a,5,7,22,90 six elements, if you add an element to the array, then rear=front; At this time, the queue is full and the queue empty judgment condition front=rear the same, In this way we can not judge whether the queue is empty or full;
There are two ways to solve this problem: one is to add a parameter to record the number of current elements in the array, and the second is to use less one storage space, that is, the last memory space of the array, and when (rear+1)%maxsiz=front, the queue is full.
Program code:
if (q->front== (q->rear+1)%q->maxsize) // determine if the loop list is full, leaving a reserved space return 1 ; Else return 0;
Appendix:
Queue.h File Code:
#ifndef _queue_self_h_#define_queue_self_h_#include<stdint.h>typedefstructqueue{uint16_t*pbase; int16_t Front; //point to queue the first element subscriptint16_t Rear;//Subscript to the next element of the last element of the queueint16_t maxsize;//Maximum storage space for the loop queue}queue,*pqueue;uint8_t createqueue (pqueue q,int16_t maxsize); uint8_t isfullqueue (Pqueue q); uint8_t isemptyqueue (PQUEUE q) ; uint8_t Sendtobackqueue (Pqueue Q, uint16_t val); uint8_t Receivefromfrontqueue (Pqueue q, uint16_t*val);#endif/* _queue_self_h_ */
QUEUE.C File Code:
#include"queue_self.h"#include"stdlib.h"uint8_t createqueue (pqueue q,int16_t maxsize) {Q->pbase= (uint16_t *)malloc(sizeof(uint16_t) *maxsize); if(null==q->pbase) Exit (-1);//Exit ProgramQ->front=0;//Initialize ParametersQ->rear=0; Q->maxsize=maxsize; return 1;} uint8_t isfullqueue (Pqueue Q) {if(q->front== (q->rear+1)%q->maxsize)//determine if the loop list is full, leaving a reserved space return 1; Else return 0;} uint8_t isemptyqueue (Pqueue Q) {if(q->front==q->rear)//determines whether the empty return 1; Else return 0;} uint8_t Sendtobackqueue (Pqueue Q, uint16_t val) {if(Isfullqueue (Q))return 0; Else{Q->pbase[q->rear]=Val; Q->rear= (q->rear+1)%q->maxsize; } return 1;} uint8_t Receivefromfrontqueue (Pqueue Q, uint16_t*val) { if(Isemptyqueue (Q)) {return 0; } Else { *val=q->pbase[q->Front]; Q->pbase[q->front]=0; Q->front= (q->front+1)%q->maxsize; } return 1;}
C language implementation of cyclic queue