#include <stdio.h>
#include <windows.h>
//定义一个顺序队列
#define QUEUESIZE 100
typedef struct Squeue {
int queue[QUEUESIZE];
int front;
int rear;
} SeQueue;
//顺序队列的初始化
void InitQueue(SeQueue* S) {
S->front = S->rear = 0;
}
//判断队列是否为空
bool IsEmpty(SeQueue S) {
if (S.front == S.rear) {
return 1;
}
return 0;
}
//入队操作
bool InQueue(SeQueue* S, int e) {
//判断队列是否为满
if (S->rear >= QUEUESIZE) {
return 0;
}
S->queue[S->rear] = e;
++S->rear;
return 1;
}
//出队操作
bool OutQueue(SeQueue* S, int* e) {
//判断队列是否为空
if (IsEmpty(*S)) {
return 0;
}
*e = S->queue[S->front];
++S->front;
return 1;
}
//销毁队列
void DestoryQueue(SeQueue *Q) {
Q->front=Q->rear=0;
}
//获取队头元素
int GetEle(SeQueue Q) {
if(Q.front==Q.rear) {
return 0;
}
int value = Q.queue[Q.front];
return value;
}
//打印队列
void Display(SeQueue s1) {
if(s1.front==s1.rear) {
printf("队列为空!\n");
return ;
}
while (s1.front != s1.rear) {
printf("Q[%d]=%d\n", s1.front, s1.queue[s1.front]);
s1.front = s1.front + 1;
}
}
int main() {
SeQueue s1;
InitQueue(&s1);
for (int i = 0; i < 10; ++i) {
if (InQueue(&s1, i + 1));
}
Display(s1);
printf("\n");
printf("当前队列的头元素为%d\n",GetEle(s1));
//出队
int e =0;
for (int i = 0; i < 5; ++i) {
if (OutQueue(&s1, &e)) {
printf("Q[%d]=%d\n", i,e);
}
}
printf("\n");
//获取队列头元素
printf("\n");
printf("\n");
//打印队列
Display(s1);
printf("\n");
printf("当前队列的头元素为%d\n",GetEle(s1));
printf("\n");
//销毁队列
printf("\n");
DestoryQueue(&s1);
Display(s1);
printf("当前队列的头元素为%d\n",GetEle(s1));
return 0;
}
顺序队列
最新推荐文章于 2023-10-21 12:13:24 发布