循环队列的增删改查
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 5
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
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 EnQueue(SqQueue &Q, ElemType e)
{
if ((Q.rear + 1) % MaxSize == Q.front)
return false;
Q.data[Q.rear] = e;
Q.rear = (Q.rear + 1) % MaxSize;
return true;
}
bool DeQueue(SqQueue &Q, ElemType &e)
{
if (Q.front==Q.rear)
{
return false;
}
e = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
int main()
{
SqQueue Q;
bool ret;
ElemType e;
InitQueue(Q);
ret = isEmpty(Q);
if (ret)
printf("队列为空\n");
else
printf("队列不为空\n");
EnQueue(Q, 3);
EnQueue(Q, 4);
EnQueue(Q, 5);
ret = EnQueue(Q, 6);
ret = EnQueue(Q, 7);
if (ret)
{
printf("入队成功\n");
}
else
{
printf("入队失败\n");
}
ret = DeQueue(Q, e);
if (ret)
{
printf("出队成功,元素值%d\n", e);
}
else
{
printf("出队失败\n");
}
return 0;
}
|