1#include <stdio.h>2#include <stdlib.h>3 4 #defineMAXSIZE 10//Define Queue Length5 6 Static intflag=0;//Defining flag bits7 8typedefstruct {9 int*Base;Ten intFront; One intRear; A}sqqueue;//Create a struct body - - intInitqueue (Sqqueue &Q) { theQ.Base=(int*)malloc(maxsize*sizeof(int)); - if(! Q.Base) - return 0; - Else{ +Q.front=q.rear=0; - return 1; + } A}//Create an empty list at - - intIsempety (Sqqueue &2) { - if(flag==0&&q.front==q.rear) { -printf"queue is empty \ n"); - return 0; in } - Else to return 1; + -}//determines whether the empty the * intIsfull (Sqqueue &p) { $ if(flag==1&&q.front==q.rear) {Panax Notoginsengprintf"queue is full \ n"); - return 0; the } + Else A return 1; the}//determine if it is full + - voidAddqueue (Sqqueue &Q) { $ if(Isfull (Q) = =1){ $ inte; -printf"Please enter an element: \ n"); -scanf"%d",&e); theQ.Base[q.rear]=e; -Q.rear= (q.rear+1)%MAXSIZE;Wuyiflag=1; the } -}//adding elements Wu - voidDeletequeue (Sqqueue &Q) { About if(Isempety (Q) = =1){ $ inte; -E=q.Base[Q.front]; -Q.front= (q.front+1)%MAXSIZE; -flag=0; Aprintf"%d\n", e); + } the}//Subtract elements - $ voidAction (Sqqueue &Q) { theprintf"1. queue \ n"); theprintf"2. Bring the team head element out of the team and return its value \ n"); theprintf"3. Exit \ n"); the intA; -scanf"%d",&a); in Switch(a) the { the Case 1: About the Addqueue (Q); the Break; the Case 2: + - Deletequeue (Q); the Break;Bayi Case 3: theExit0); the default: -printf"input is not valid, please re-enter \ n"); - the } the Action (Q); the}//Action Options the - intMain () { the sqqueue Q; the if(Initqueue (Q)) theprintf"created successfully. \ n");94 Else{ theprintf"creation failed. "); theSystem"Pause"); the return 0;98 } About - Action (Q);101 102System"Pause");103 Free(Q.Base);104 return 0; the 106 107}//Main function
Although implemented with a flag bit, an error occurs when swapping an int array to a char array, where doubts remain, pending resolution.
C Language Data structure: Implementing circular queues with flag bits