Title details :

6-1 Circular queue out of line in line (15 branch )

Use an array to represent the circular queue , Please write an algorithm to initialize the circular queue , Entry and exit operations .

When entering : The first row inputs the data space capacity of the queue , The second line is entered in sequence 5 Element values to be inserted , The third line is entered in sequence 5 Element values to be inserted .
Output time : The first and last lines output the cyclic queue element values and their subscripts ( Element value ( subscript )), If the queue is empty or full on the way , Corresponding prompt shall be given .

Function interface definition :
void InitQ(SqQueue &Q,int N); void AddQ(SqQueue &Q, int x ); Status
DeleteQ(SqQueue &Q,int &e);
Interface parameters : Q Circular queue , N  Is the space capacity of the queue array , x It's a team element , e The value used to receive the dequeued element

Sample referee test procedure :
#include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR 0 typedef
int Status; typedef struct { int *base; int front; // Team leader int rear; // Team tail
}SqQueue; void InitQ(SqQueue &Q,int N); void AddQ(SqQueue &Q, int x ); Status
DeleteQ(SqQueue &Q,int &e); void printQ(SqQueue Q); // Output queue elements and their subscript positions int N; int
main() { int x,e,i; SqQueue Q; scanf("%d",&N); // Enter the size of the circular queue space InitQ(Q,N);
for(i=0;i<5;i++){ scanf("%d",&x); AddQ(Q,x); } printQ(Q); // Elements in the output queue
for(i=0;i<5;i++){ if(DeleteQ(Q,e)==OK) printf("%d is out.\n",e); }
for(i=0;i<5;i++){ scanf("%d",&x); AddQ(Q,x); } printQ(Q); return 0; } void
printQ(SqQueue Q){ int i; i=Q.front; while(i!=Q.rear){ printf("%d(%d)
",Q.base[i],i); i=(i+1)%N; } printf("\n"); } /* Please fill in the answer here */
sample input 1:
6 1 2 3 4 5 1 2 3 4 5
sample output 1:
1(0) 2(1) 3(2) 4(3) 5(4) 1 is out. 2 is out. 3 is out. 4 is out. 5 is out.
1(5) 2(0) 3(1) 4(2) 5(3)
sample input 2:
5 1 2 3 4 5 1 2 3 4 5
sample output 2:
Queue Full 1(0) 2(1) 3(2) 4(3) 1 is out. 2 is out. 3 is out. 4 is out. Queue
Empty Queue Full 1(4) 2(0) 3(1) 4(2)
Answer code :
void InitQ(SqQueue &Q,int N) { Q.base = (int*)malloc(N*sizeof(int)); Q.front =
Q.rear = 0; } void AddQ(SqQueue &Q, int x ) { if((Q.rear+1)%N == Q.front) {
printf("Queue Full\n"); } else { Q.base[Q.rear] = x; Q.rear = (Q.rear+1)%N; } }
Status DeleteQ(SqQueue &Q,int &e) { if(Q.front==Q.rear) { printf("Queue
Empty\n"); return ERROR; } else { e = Q.base[Q.front]; Q.front = (Q.front+1)%N;
return OK; } }
  Input case test :

Submit results :

 

Technology