#include <stdio.h> #include <stdlib.h> #include <iostream> #include <string> #include <fstream> #include <vector> #include <list> #include <limits.h> #include <algorithm> /*队列数组实现*/ struct Queue{ int Capacity;//容量 int Size;//实际大小 int Front;//前指针 int Rear;//后指针 int* data;//数组 }; //1 is Empty int IsEmpty(Queue* q) { return q->Size == 0; } //1 is Full int IsFull(Queue* q) { return q->Capacity == q->Size; } //入队 void Enqueue(Queue* q, int val) { if (IsFull(q)) { printf("Queue is full!\n"); return; } q->Rear = (q->Rear + 1) % q->Capacity; q->data[q->Rear] = val; q->Size++; } //出队 int Dequeue(Queue* q) { if (IsEmpty(q)) { printf("Queue is Empty!\n"); return NULL; } int tmp = q->data[q->Front]; q->Front = (q->Front + 1) % q->Capacity; q->Size--; return tmp; } //打印队列 void PrintQueue(Queue* q) { if (IsEmpty(q)) { printf("Queue is Empty!\n"); return; } int i = 0, j = q->Front; while (i < q->Size) { printf("%d\n", q->data[j++%q->Capacity]); i++; } printf("\n"); } //创建队列 Queue* CreateQueue(int MaxCapacity) { Queue* q = (Queue*)malloc(sizeof(Queue)); q->Capacity = MaxCapacity; q->data = (int*)malloc(sizeof(int)*MaxCapacity); q->Front = 0; q->Rear = -1; q->Size = 0; return q; } int main(int argc,char** argv) { Queue *q=CreateQueue(5); Enqueue(q, 0); Enqueue(q, 1); Enqueue(q, 2); Enqueue(q, 3); Enqueue(q, 4); PrintQueue(q); Dequeue(q); Dequeue(q); PrintQueue(q); //Enqueue(q, 6); Enqueue(q, 7); Enqueue(q, 8); Enqueue(q, 9); PrintQueue(q); return 0; }