describe
Create a circular queue with 4 elements in the queue. It can implement queue initialization, queue entry, exit queue, and queue length calculation.
The circular queue data type is defined as follows:
typedef struct
{
int data[Max];
int front;
int rear;
}SqQueue;
Part of the code has been given, please complete it. Please do not include the given code when submitting.
intmain() { SqQueue q; char ss[10]; int x, sta, l; InitQueue(&q); while(scanf("%s", ss)!=EOF) { if(strcmp(ss, "enter")==0) { scanf("%d", &x); sta= EnQueue(&q,x); if(sta==0) printf("FULL\n"); } else if(strcmp(ss, "length")==0) { l=QueueLength(q); printf("%d\n", l); } else { sta = DeQueue(&q,&x); if(sta==0) printf("EMPTY\n"); else printf("%d\n", x); } } return 0; }
enter
The input data consists of the following commands:
(1) enter x: x enters the queue
(2) del: dequeue
(3) length: find the length of the queue
Each command occupies one line and ends with EOF.
output
When the enter operation is performed, the element is enqueued, and if the queue is full, FULL is output.
When del is executed, the elements dequeued are output. If the queue is empty, EMPTY is output.
when. When length is executed, the length of the queue is output.
sample input
enter 3
enter 15
length
del
del
del
Sample output
2
3
15
EMPTY
#include <stdio.h> #include <string.h> #define Max 5 typedef struct { int data[Max]; int front; int rear; } sqqueue; int InitQueue(SqQueue *q) { q->front=0; q->rear=0; return 1; } int QueueLength(SqQueue q) { return (q.rear-q.front+Max)%Max; } int EnQueue(SqQueue *q,int x) { if((q->rear+1)%Max==q->front) return 0; else { q->data[q->rear]=x; q->rear=(q->rear+1)%Max; return 1; } } int DeQueue(SqQueue *q,int *x) { if((q)->front==(q)->rear) return 0; else { *x=q->data[q->front]; q->front=(q->front+1)%Max; return *x; } }