数据结构——队列的数组实现
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
typedef struct Queue
{
int Array[MaxSize];
int Front;
int Rear;
int Size;
}Queue;
Queue* CreateQueue()
{
Queue* q;
q = (Queue*)malloc(sizeof(Queue));
q->Front = 0;
q->Rear = 0;
q->Size = 0;
return q;
}
Queue* Enqueue(int X, Queue* q)
{
if (q->Rear + 1 == q->Front)
printf("该队列已满!");
else
{
q->Array[q->Rear] = X;
q->Rear++;
q->Size++;
}
return q;
}
Queue* Dequeue(Queue* q)
{
if (q->Size == 0)
printf("该队列为空!");
else
{
q->Size--;
q->Front++;
}
return q;
}
void Display(Queue* q)
{
int t;
t = q->Front;
if(q->Size==0)
printf("该队列为空!");
while (t != q->Rear)
{
printf("%d", q->Array[t]);
t++;
}
printf("\n");
}
void main()
{
Queue* q;
int i;
q = CreateQueue();
for (i = 0; i < 10; i++)
{
q = Enqueue(i, q);
Display(q);
}
q = Dequeue(q);
Display(q);
}