赞
踩
#include <stdio.h> #include <stdlib.h> typedef int ElemType; #define MaxSize 50 typedef struct { ElemType data[MaxSize]; int front; //队头指针 int rear; //队尾指针 } SqQueue; //循环队列初始化 SqQueue* InitQueue(SqQueue *Q) { Q->front = Q->rear = 0; return Q; } //判断队列是否为空 ElemType isEmpty(SqQueue *Q) { if (Q->rear == Q->front) return 0; else return 1; } //入队 int EnQueue(SqQueue *Q, ElemType x) { if ((Q->rear + 1) % MaxSize == Q->front) { //队列满报错 return 0; } Q->data[Q->rear] = x; Q->rear = (Q->rear + 1) % MaxSize; return 1; } //出队 void DeQueue(SqQueue *Q) { Q->front = (Q->front + 1) % MaxSize; } //遍历 void PrintQueue(SqQueue *Q) { for (int i = Q->front; i < Q->rear; i++) { printf("%d", Q->data[i]); } } int main() { SqQueue Q; //初始化 SqQueue *Q1 = InitQueue(&Q); //入队 EnQueue(Q1, 1); EnQueue(Q1, 2); EnQueue(Q1, 3); EnQueue(Q1, 4); //打印 PrintQueue(Q1); printf("\n"); //出队 DeQueue(Q1); //打印 PrintQueue(Q1); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。