队列
// duilie.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include<iostream> #include <stdlib.h> using namespace std; struct QueueRecord; typedef struct QueueRecord *Queue; int IsEmpty(Queue Q); int IsFull(Queue Q); Queue CreateQueue(int MaxElement); void DisposeQueue(Queue Q); void MakeEmpty(Queue Q); void Enqueue(ElementType x, Queue Q); ElementType Dequeue(Queue Q); #define MinQueueSize (5) typedef int ElementType; struct QueueRecord { int Capacity; int Front; int Rear; int Size; ElementType *Array; }; Queue CreateQueue(int MaxElement) { Queue Q; if (MaxElement < MinQueueSize) cout << "too small"; Q = (Queue)malloc(sizeof(QueueRecord)); if (Q == NULL) cout << "out of space"; Q->Array= (ElementType *)malloc(sizeof(ElementType)*MaxElement); if (Q->Array == NULL) cout << "out of space"; Q->Capacity = MaxElement; MakeEmpty(Q); return Q; } void DisposeQueue(Queue Q) { if (Q != NULL) { free(Q->Array); free(Q); } } int IsEmpty(Queue Q) { return Q->Size == 0; } void MakeEmpty(Queue Q) { Q->Size = 0; Q->Front = 1; Q->Rear = 0; } static int Succ(int Value, Queue Q) { if (++Value == Q->Capacity) Value = 0; return Value; } void Enqueue(ElementType x, Queue Q) { if (IsFull(Q)) cout << "full"; else { Q->Size ++; Q->Rear = Succ(Q->Rear,Q); Q->Array[Q->Rear] = x; } } int IsFull(Queue Q) { return (Q->Rear) - (Q->Front) == -1; } ElementType Dequeue(Queue Q) { ElementType x; if (IsEmpty(Q)) { cout << "Empty"; return 0; } else { x = Q->Array[Q->Front]; Q->Size--; Q->Front = Succ(Q->Front, Q); return x; } } void print(Queue Q) { for (int i = 0; i <Q->Size; i++) cout << Q->Array[i] << " "; cout << endl; } int main() { Queue Q = CreateQueue(10); Enqueue(10, Q); Enqueue(5, Q); print(Q); ElementType e = Dequeue(Q); print(Q); while (1); return 0; }