/* * April 17, 2015 09:07:17 * Objective: To implement the cyclic queue (sequential storage structure), I would like to implement the queue before the implementation of the queued order storage, but this algorithm has a great disadvantage * first will cause false overflow phenomenon *//* * Solve the problem of queue, is how to judge Whether the queue is full or empty * The first method: * We are all real = front when the team is full and empty; so we take the sacrifice of a storage unit so that when NULL, the judgment is: real = = Front * Team full operation is: (real+1)%maxs ize = = Front * The second method is to add a variable to represent the number of elements in the queue, size * Here I use the first method. */# include <stdio.h># define maxSize 50struct sqqueue{int *data;int Front;//head pointer int rear;//team tail pointer};//initialize void Initqu Eue (Sqqueue *queue) {queue->data = new Int[maxsize];queue->front = queue->rear = 0;//Initialize team header and tail pointer}//determine if the queue is empty bool IsEmpty (Sqqueue *queue) {if (Queue->front = = Queue->rear) return True;elsereturn false;} Determines whether the queue is full of bool Isfill (Sqqueue *queue) {if (Queue->front = = (queue->rear+1)% maxSize) return True;elsereturn false; into the queue void Enterqueue (Sqqueue *queue,int value) {if (Isfill (queue)) {printf ("The queue is already full, no additional numbers can be inserted!\n");} Else{queue->data[queue->rear] = value;//Assign the tail pointer queue->rear = (queue->rear+1)% MaxSize;//Add 1 modulo to the tail pointer}}// Out of queue void Outofqueue (Sqqueue *queue,iNT *value) {if (IsEmpty (queue)) {printf ("Queue is empty!, cannot out queue. \ n");} Else{*value = Queue->data[queue->front];queue->front = (queue->front+1)% MaxSize;}} Output queue void PrintQueue (Sqqueue *queue) {if (IsEmpty (queue)) printf ("Queue is empty!. \ n"); else{printf ("The element of the queue is: \ n"); if ( Queue->rear < Queue->front) {for (int i = Queue->front;i <maxsize;i++) {printf ("%4d", Queue->data[i]); if ((i+1)% = = 0) printf ("\ n");} for (int j = 0;j < queue->rear;j++) {printf ("%4d", Queue->data[i]), if ((i+1)% = = 0) printf ("\ n");}} else{for (int i = Queue->front;i < queue->rear;i++) {printf ("%4d", Queue->data[i]), if ((i+1)% = = 0) printf (" \ n ");} printf ("\ n");}}} int main () {Sqqueue queue;initqueue (&queue); for (int i = 1;i < 20;i++) Enterqueue (&queue,i);p Rintqueue (& queue); int value; Outofqueue (&queue,&value);p rintf ("the element that is out of the queue is:%d\n", value);p rintqueue (&queue); return 0;}
Queue sequential Storage (circular queue)