标签:eem type col class stdio.h empty lib bsp main
#include <stdio.h> #include <stdlib.h> //Real capacity is CircularQueueMaxSize -1 #define CircularQueueMaxSize 1000 typedef int ElementType; struct CircularQueue { ElementType QueueData[CircularQueueMaxSize]; int Front; int Rear; }; int CircularQueueIsEmpty(struct CircularQueue *Queue) { return (Queue -> Front == Queue -> Rear); } int CircularQueueIsFull(struct CircularQueue *Queue) { return ((Queue -> Rear + 1) % CircularQueueMaxSize == Queue -> Front); } struct CircularQueue *CircularQueueInit() { struct CircularQueue *Queue; Queue = malloc(sizeof(struct CircularQueue)); Queue -> Front = Queue -> Rear = 0; return Queue; } //if Queue is full,return 1 int CircularQueueEnqueue(struct CircularQueue *Queue,ElementType ToBeEnqueue) { if(CircularQueueIsFull(Queue)) { return 1; } else { Queue -> Rear = (Queue -> Rear + 1) % CircularQueueMaxSize; Queue -> QueueData[Queue -> Rear] = ToBeEnqueue; } return 0; } //if Queue is empty,return 1 ElementType CircularQueueTop(struct CircularQueue *Queue) { if(CircularQueueIsEmpty(Queue)) { return 1; } else { return Queue -> QueueData[(Queue -> Front + 1) % CircularQueueMaxSize]; } return 0; } //if Queue is empty,return 1 int CircularQueueDequeue(struct CircularQueue *Queue) { if(CircularQueueIsEmpty(Queue)) { return 1; } else { Queue -> Front = (Queue -> Front + 1) % CircularQueueMaxSize; return 0; } } int MakeCircularQueueEmpty(struct CircularQueue *Queue) { Queue -> Front = Queue -> Rear = 0; return 0; } int CircularQueueDelete(struct CircularQueue *Queue) { free(Queue); Queue = NULL; return 0; } int main() { int test; int i; struct CircularQueue *Queue; Queue = CircularQueueInit(); MakeCircularQueueEmpty(Queue); CircularQueueDelete(Queue); Queue = CircularQueueInit(); test = CircularQueueTop(Queue); CircularQueueDequeue(Queue); CircularQueueEnqueue(Queue,39); test = CircularQueueTop(Queue); CircularQueueDequeue(Queue); printf("%d\n",test); for(i = 1;i < 2000;i ++) { CircularQueueEnqueue(Queue,i); } for(i = 1;i < 500;i ++) { CircularQueueDequeue(Queue); } test = CircularQueueTop(Queue); CircularQueueDequeue(Queue); printf("%d\n",test); return 0; }
标签:eem type col class stdio.h empty lib bsp main
原文地址:https://www.cnblogs.com/Asurudo/p/9427305.html