循环队列的代码-c
#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)//循環(huán)隊列的初始化
{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)//打印循環(huán)隊列
{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");
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/ljh-blog/p/10916313.html
總結(jié)