#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define MAXSIZE 100
typedef int ElemType;
typedef struct SqQueue
{
ElemType *
base;
//指向队列的存储空间
int rear, front;
//队头指针,队尾指针
}SQ;
void InitQueue(SQ *
Q);
void EnQueue(SQ *
Q, ElemType data);
ElemType DeQueue(SQ *
Q);
void PrintQueue(SQ *
Q);
int QueueEmpty(SQ *
Q);
void InitQueue(SQ *Q)
//循环队列的初始化
{
Q->
base = (ElemType*)
malloc(
sizeof(ElemType));
if (!Q->
base)
return;
Q->front = Q->rear =
0;
}
void EnQueue(SQ *Q, ElemType data)
//入队
{
if ((Q->rear +
1) % MAXSIZE == Q->front)
return;
Q->
base[Q->rear] =
data;
Q->rear = (Q->rear +
1) %
MAXSIZE;
}
ElemType DeQueue(SQ *Q)
//出队
{
if (QueueEmpty(Q))
return;
ElemType data = Q->
base[Q->
front];
Q->front=(Q->front +
1) %
MAXSIZE;
return data;
}
void PrintQueue(SQ *Q)
//打印循环队列
{
if (QueueEmpty(Q))
return;
int pos=Q->
front;
while (pos != Q->
rear)
{
printf("%d ",Q->
base[pos]);
pos++
;
}
}
int QueueEmpty(SQ *Q)
//判断队列是否为空
{
if (Q->front == Q->rear)
return 1;
return 0;
}
main()
{
SQ *sq= (SQ*)
malloc(
sizeof(SQ));
InitQueue(sq);
EnQueue(sq, 5);
EnQueue(sq, 6);
EnQueue(sq, 7);
EnQueue(sq, 8);
printf("出队列元素为%d\n", DeQueue(sq));
printf("队列元素为:");
PrintQueue(sq);
system("pause");
}
转载于:https://www.cnblogs.com/ljh-blog/p/10916313.html
相关资源:循环队列节点实现