Look at my code.
#include <iostream>
using namespace std;
#define MAXSIZE 50
typedef int QueueElementType; // type of variable
// Circular Queue
// The Circular Queue's difinition of the structure
typedef struct{
QueueElementType element[MAXSIZE]; // element space of queue
int front; // the head pointer
int rear; // the tail pointer
}SeqQueue;
// the basic operation of the queue includes initialize、incoming、out of queue and so on
// initialize the Circular Queue
void InitQueue(SeqQueue * Q)
{ // initialize Q into an empty circular queue
Q->front = Q->rear = 0;
}
// entry operation of the circular queue
void EnterQueue(SeqQueue * Q,QueueElementType x)
{ // enter the element x into the queue
if((Q->rear+1)%MAXSIZE==Q->front) //the head pointer is caught up with by the tail pointer plus 1 ,revealing that the queue is full
{
}
Q->element[Q->rear] = x;
Q->rear = (Q->rear+1)%MAXSIZE; // reset the tail pointer
}
// delete operation of the queue
void DeleteQueue(SeqQueue * Q,QueueElementType * x)
{ // delete the head element from the queue,save the element in x
if(Q->front == Q->rear)
{
}
*x = Q->element[Q->front];
Q->front = (Q->front+1)%MAXSIZE; // reset the head pointer
}
int main()
{
SeqQueue Q;
cout << " execute initialize..."<<endl;
InitQueue(&Q);
cout << " execute entery..."<<endl;
EnterQueue(&Q,2);
cout << " input 2 "<<endl;
EnterQueue(&Q,4);
cout << " input 4 "<<endl;
EnterQueue(&Q,5);
cout << " input 5"<<endl;
cout <<endl;
cout << " execute delete..."<<endl;
int x;
DeleteQueue(&Q,&x);
cout << " the head element's value of the queue : "<< x <<endl;
DeleteQueue(&Q,&x);
cout << " the head element's value of the queue : "<< x <<endl;
}
Running result: