静态循环队列的操作 详细请参考以下代码:
#include <stdio.h> #define MAX_SIZE 100 typedef struct { int front; int rear; int data[MAX_SIZE]; }Queue; // 循环队列 void initQueue(Queue * sq); void DestoryQueue (Queue * sq); //销毁队列 void EnQueue (Queue * sq,int e); //进队列 void DeQueue (Queue * sq); //出队列 void GetHead (Queue * sq); //取队头元素值 bool QueueEmpty (Queue sq); //判队列空否 void DisQueue(Queue * sq); // 遍历队列中元素 void main () { int size = 0; char choice = '\0'; Queue sQueue = {0,}; initQueue(&sQueue); while (choice != 'q') { printf("******************************\n"); printf("--- 1 元素进队 --- \n"); printf("--- 2 队头出队 --- \n"); printf("--- 3 遍历队中元素 --- \n"); printf("--- 4 判队列空否 --- \n"); printf("--- 5 取队头元素值 --- \n"); printf("--- 6 销毁队列 --- \n"); printf("--- q 退出 --- \n"); printf("******************************\n"); scanf("\n%c",&choice); switch (choice) { case '1': printf("输入进栈元素个数:"); scanf("%d",&size); for (int i = 0; i < size; i++) EnQueue (&sQueue,i+1) ; //进队列 break; case '2': DeQueue(&sQueue); break; case '3': DisQueue(&sQueue); break; case '4': if (!QueueEmpty(sQueue)) printf("队列不为空\n"); break; case '5': GetHead(&sQueue); break; case '6': DestoryQueue(&sQueue); break; case 'q': return; } } } /*初始化队列*/ void initQueue(Queue * sq) { sq->front = 0; sq->rear = 0; for (int i = 0; i < MAX_SIZE; i++) sq->data[i] = 0; } void DestoryQueue (Queue * sq) //销毁队列 { initQueue(sq); return; } void EnQueue (Queue * sq,int e) //进队列 { if ((sq->rear+1)% MAX_SIZE != sq->front) // 判断是否队满 { sq->data[sq->rear] = e; sq->rear = (sq->rear+1)%MAX_SIZE; // 队尾进队 } } void DeQueue (Queue * sq) //出队列 { if (!QueueEmpty(*sq)) // 队列不空时出队 sq->front = (sq->front+1)%MAX_SIZE; else return; } void GetHead (Queue * sq) //取队头元素值 { if (!QueueEmpty(*sq)) printf("队头元素为 %d\n",sq->data[sq->front]); } bool QueueEmpty (Queue sq) //判队列空否 { if (sq.front != sq.rear) return false; else { printf("队列为空,请先进队\n"); return true; } } void DisQueue(Queue * sq) // 遍历队列中元素 { if (QueueEmpty(*sq)) return; printf("遍历队列中的元素\n"); for (int i = sq->front; i < sq->rear; i++) { printf("%d\t",sq->data[i]); } printf("\n"); }原文链接:https://www.f2er.com/datastructure/383096.html