in the loop queue, the tail pointer may be smaller than the team head pointer.
when the cursor is in the queue, the tail pointer is added one.
When the queue is full, the tail pointer is equal to the head pointer, and the queue null condition is the same.
In order to distinguish the team from the team empty, in the loop queue, less a storage unit, that is, the maximum storage space is max_qsize in the loop queue, can only hold max_qsize-1 elements. In this way, the queue empty condition is either the tail pointer equals the head pointer, the queue full condition changes (the tail pointer + 1) to the max_qsize is equal to the opponent pointer.
/* Sequential storage structure for c3-3.h queue (circular queue) */
#define MAX_QSIZE 5/* Maximum Queue Length +1 */
typedef struct
{
Qelemtype *base; /* Initialize dynamically allocated storage space */
int front; /* head pointer, if the queue is not empty, point to the queue header element */
int rear; /* tail pointer, if the queue is not empty, point to the next position of the tail element of the queue */
}sqqueue;
/* Basic operation of bo3-3.c loop queue (storage structure defined by C3-3.h) (9) */
void Initqueue (Sqqueue *q)
{/* Constructs an empty queue Q */
(*q). base= (Qelemtype *) malloc (max_qsize*sizeof (Qelemtype));
if (! ( *Q). Base)/* Storage Allocation failed */
Exit (OVERFLOW);
(*q). front= (*q). rear=0;
}
void Destroyqueue (Sqqueue *q)
{/* Destroy queue Q,q no longer exists */
if ((*q). Base)
Free ((*q). Base);
(*q). Base=null;
(*q). front= (*q). rear=0;
}
void Clearqueue (Sqqueue *q)
{/* Clear Q to empty queue */
(*q). front= (*q). rear=0;
}
Status queueempty (Sqqueue Q)
{/* Returns true if Queue Q is an empty queue; otherwise false */
if (q.front==q.rear)/* Queue empty flag */
return TRUE;
Else
return FALSE;
}
int Queuelength (Sqqueue Q)
{/* Returns the number of elements of Q, which is the length of the queue */
Return (q.rear-q.front+max_qsize)%max_qsize;
}
Status GetHead (sqqueue q,qelemtype *e)
{/* If the queue is not empty, use E to return the team header element of Q and return OK; otherwise return error */
if (q.front==q.rear)/* Queue Empty */
return ERROR;
*e=q.base[q.front];
return OK;
}
Status EnQueue (Sqqueue *q,qelemtype e)
{/* Insert new tail element with element e as Q */
if ((*q) rear+1)%max_qsize== (*Q)./* Queue full *///Logical position is adjacent, wash experience
return ERROR;
(*q). base[(*q). rear]=e;
(*q). rear= ((*q). rear+1)%max_qsize;
return OK;
}
Status DeQueue (sqqueue *q,qelemtype *e)
{/* If the queue is not empty, delete the team header element of Q, return its value with E, and return OK; return error */
if ((*q). front== (*Q). Rear)/* Queue Empty */
return ERROR;
*e= (*Q). base[(*Q). Front];
(*q). Front= ((*q). front+1)%max_qsize; Note is added one, the position of the queue Head plus 1, carefully experience.
return OK;
}
void Queuetraverse (Sqqueue q,void (*VI) (Qelemtype))
{/* Call Function VI () for each element in queue Q from Team head to end of team) */
int i;
I=q.front;
while (I!=q.rear)
{
VI (Q.base[i]);
I= (i+1)%max_qsize;
}
printf ("\ n");
}
/* main3-3.c loop queue Check BO3-3.C's main program */
#include "C1.h"
typedef int QELEMTYPE;
#include "C3-3.h"
#include "bo3-3.c"
void print (Qelemtype i)
{
printf ("%d", I);
}
int main ()
{
Status J;
int i=0,l;
Qelemtype D;
Sqqueue Q;
Initqueue (&Q);
printf ("After the queue is initialized, the queue is empty.) %u (1: Empty 0: NO) \ n ", Queueempty (Q));
printf ("Please enter an integer queue element (no more than%d), 1 is an advance Terminator:", max_qsize-1);
Do
{
scanf ("%d", &d);
if (d==-1)
Break
i++;
EnQueue (&Q,D);
}while (i<max_qsize-1);
printf ("Queue Length:%d\n", Queuelength (Q));
printf ("Now queue empty No.") %u (1: Empty 0: NO) \ n ", Queueempty (Q));
printf ("%d consecutive times by team head Delete element, tail insert element: \ n", max_qsize);
for (l=1;l<=max_qsize;l++)
{
DeQueue (&Q,&D);
printf ("The deleted element is%d, please enter the element to be inserted:", d);
scanf ("%d", &d);
EnQueue (&Q,D);
}
L=queuelength (Q);
printf ("Now the elements in the queue are: \ n");
Queuetraverse (Q,print);
printf ("A total of%d elements were inserted at the end of the team \ n", i+max_qsize);
if (l-2>0)
printf ("Now removed%d elements by team head: \ n", l-2);
while (Queuelength (Q) >2)
{
DeQueue (&Q,&D);
printf ("Deleted element value is%d\n", d);
}
J=gethead (Q,&D);
if (j)
printf ("Now the team head element is:%d\n", D);
Clearqueue (&Q);
printf ("Empty queue, no queue.") %u (1: Empty 0: NO) \ n ", Queueempty (Q));
Destroyqueue (&Q);
}