1、结构体定义
2、初始化
3、判空
4、入队
5、出队
6、完整程序
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 5
typedef int ElemType;
typedef struct Queue{
ElemType data[MaxSize];
int front, rear;
}SqQueue;
void InitQueue(SqQueue& Q) {
Q.front = Q.rear = 0;
}
bool isEmpty(SqQueue Q) {
if (Q.front == Q.rear) {
return true;
}
return false;
}
bool EnQueue(SqQueue& Q,ElemType x) {
if ((Q.rear + 1) % MaxSize == Q.front) {
return false;
}
Q.data[Q.rear++] = x;
Q.rear = Q.rear % MaxSize;
return true;
}
bool DeQueue(SqQueue& Q,ElemType& e) {
if (isEmpty(Q)) {
return false;
}
e = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
void PrintQueue(SqQueue Q) {
if (isEmpty(Q)) {
printf("队空\n");
}
for (int i = Q.front; i < Q.rear; i++) {
printf(" %d ",Q.data[i]);
}
printf("\n");
}
int main() {
SqQueue Q;
InitQueue(Q);
EnQueue(Q,2);
EnQueue(Q,3);
EnQueue(Q,4);
EnQueue(Q,5);
PrintQueue(Q);
ElemType temp;
DeQueue(Q,temp);
PrintQueue(Q);
printf(" %d \n",temp);
return 0;
}
|