The loop queue is stored in sequential order (array), the basic idea is to store data through an array, two subscript front and rear respectively point to Team head and tail
Because of the false overflow phenomenon: the use of circular queue, and because the loop queue can not determine whether the queue is empty or full, so the loss of an element is the cost of empty to separate the queue is empty or full
Unlike the chain queue, it is:
The team head pointer (subscript) of the loop queue is not the head node, but the element that points directly to the current team header
The tail pointer (subscript) of the loop queue does not point to the last element, but to the next subscript of the last element
When the loop queue is empty, the tail and the team head subscript are pointing to ah a[0]
Definition of circular queues
An array to hold the data, two subscript to hold the team head and the tail subscript
typedef char DATATYPE;TYPEDEF struct queue{datatype elem[maxsize];int front;int rear;} Seqqueue;
Initialization of the cyclic queue
Team head subscript and tail subscript are initialized to 0
Initqueue (seqqueue* Q) {q->front=0; q->rear=0;}
Queued operation
First judge whether it is full: condition: (q->rear+1)%maxsize==q->front
Above is an example of sacrificing a space
BOOL Enterqueue (seqqueue* Q,datatype x) {if ((q->rear+1)%maxsize==q->front) {return false;} else{q->elem[q->rear]=x; Q->rear= (q->rear+1)%maxsize;return true;}}
Out-of-action
First determine whether it is empty: condition: q->front==q->rear
BOOL Deletequeue (seqqueue* q,datatype* x) {if (Q->front==q->rear) {return false;} else{*x=q->elem[q->front]; Q->front= (q->front+1)%maxsize;return true;}}
Queue of data structures (three)--cyclic queue