#define QMaxSize 10
#include "stdio.h"
typedef struct{
QElemType data[QMaxSize];
int front, rear; //队头指针和队尾指针
}SqQueue;
/* 初始化 */
void InitQueue(SqQueue &Q)
{
Q.rear = Q.front = 0;
}
/* 判断队空 */
bool isEmpty(SqQueue &Q)
{
if( Q.rear == Q.front )
return true;
else
return false;
}
/* 判断队满 */
bool isFull(SqQueue &Q)
{
if( ( Q.rear + 1 ) % QMaxSize == Q.front )
return true;
else
return false;
}
/* 入队 */
bool EnQueue(SqQueue &Q, int x)
{
if( ( Q.rear + 1 ) % QMaxSize == Q.front )
return false;
Q.data[Q.rear] = x;
Q.rear = ( Q.rear + 1 ) % QMaxSize; //队尾指针加一取模
return true;
}
/* 出队 */
bool DeQueue(SqQueue &Q, int &x)
{
if( Q.rear == Q.front )
return false;
x = Q.data[Q.front];
Q.front = ( Q.front + 1 ) % QMaxSize;
return true;
}
void Print(SqQueue Q)
{
if( isEmpty(Q) )
{
printf("the queue is empty !\n");
return;
}
int i = Q.front;
while( i != Q.rear )
{
printf("%d ", Q.data[i]);
i = ( i + 1 ) % QMaxSize;
}
}
顺序循环队列的基本操作(2017.7.26)
最新推荐文章于 2022-08-14 20:30:26 发布