#include<stdio.h> #include<stdlib.h> #include<string.h> #define MaxSize 100 typedef struct Queue { int front; int rear; int *queue; }Queue,SqQueue; int InitQueue(SqQueue *Q) { Q->queue = (int *)malloc(MaxSize*sizeof(int)); if (!Q->queue) { exit(1); } Q->front = Q->rear = 0; return 0; } int QueueLength(SqQueue *Q) { return(Q->rear - Q->front + MaxSize) % MaxSize; } int EnterQueue(SqQueue *Q,int e) { if ((Q->queue[Q->rear + 1])%MaxSize == Q->queue[Q->front]) { printf("队列已满!"); exit(1); } Q->queue[Q->rear] = e; Q->rear = (Q->rear+1) % MaxSize; return 0; } int DeleteQueue(SqQueue *Q,int *E) { int e; if (Q->front == Q->rear) { printf("队列为空!"); } *E= Q->queue[Q->front]; Q->front = (Q->front + 1) % MaxSize; return 0; } int main() { int S,D,F; int i = 1, e, Len, data; SqQueue * Que; Que = (SqQueue *)malloc(sizeof(Queue)); InitQueue(Que); printf("是否想往队列中加点东东嘞?\n"); printf("是输入1,否输入0\n"); scanf_s("%d", &S); while (S=1) { printf("请输入第%d个数据:", i++); scanf_s("%d", &e); EnterQueue(Que, e); printf("是否想要继续输入到队列之中?是选择1,否选择0\n"); scanf_s("%d", &D); if (D == 0) { break; } } printf("看看现在的队列有多长吧!\n"); Len = QueueLength(Que); printf("现在的队列的长度为:%d\n", Len); printf("是否想要删除队列之中的一些数据呢?选择1或者0\n"); scanf_s("%d",& F); if ( F=1) { DeleteQueue(Que,&data); printf("删除的数据为:%d\n", data); } printf("看看现在的队列有多长吧!\n"); Len = QueueLength(Que); printf("现在的队列的长度为:%d\n", Len); }
原文:http://www.cnblogs.com/code-wangjun/p/4372847.html