Use static arrays to implement the C language of cyclic queue
# Include
# Include
# Define Data_Type int # define Queue_Len 5 // you can use either of the following two methods to determine whether the team is full: Mark the team, for example, size. // Another is to waste a piece of space, when taking up the N-1, even if full. Typedef struct Queue {Data_Type data [Queue_Len]; int front; // the first element of the team header, int rear; // The team end element // int size; record QUEUE size} QUEUE, * QQUEUE; void create (QQUEUE); bool isFull (QQUEUE); bool isEmpty (QQUEUE); bool add (QQUEUE, Data_Type ); data_Type out (QQUEUE); void traverse (QQUEUE); int main (void) {QUEUE queue; create (& queue); add (& queue, 1); add (& queue, 2); add (& queue, 3); add (& queue, 4); out (& queue); add (& queue, 5); out (& queue ); add (& qu Eue, 6); out (& queue); add (& queue, 7); traverse (& queue);} bool isFull (QQUEUE qQuere) {if (qQuere-> rear + 1) % Queue_Len = qQuere-> front) {return true;} else {return false;} bool isEmpty (QQUEUE qQueue) {if (qQueue-> front = qQueue-> rear) {return true;} else {return false ;}} void create (QQUEUE qQueue) {qQueue-> front = qQueue-> rear = 0; return;} void traverse (QQUEUE qQueue) {int I = qQueue-> front; while (I! = QQueue-> rear) {// here is a comparison. Why do I need to obtain the remainder in the output? // First, front represents the first element of the Team, which must not be output. // If you give him more than 1, you can normally output the first line. // But there will be a problem at the end of the team. If the subscript is the subscript at the end of the team, + 1 will exceed the length of the array. // if the remainder is obtained, the normal printf (% d, qQueue-> data [(I + 1) % Queue_Len]); I ++; I = I % Queue_Len; // find the next legal element first for a better understanding, find and output //, so you can directly output I, because I is already the next element to be found. // I ++; // I = I % Queue_Len; // printf (% d, qQueue-> data [I]) ;}} bool add (QQUEUE qQueue, data_Type val) {if (isFull (qQueue) {return false;} else {qQueue-> rear = (qQueue-> rear + 1) % Queue_Len; qQueue-> data [qQueue-> rear] = val; return true ;}} Data_Type out (QQUEUE qQueue) {if (isEmpty (qQueue) {exit (-1 );} else {Data_Type val = qQueue-> data [qQueue-> front + 1]; qQueue-> front = (qQueue-> front + 1) % Queue_Len; return val ;}}