#ifndef QUEUE_H_INCLUDED
#define QUEUE_H_INCLUDED
struct QueueRecord;
typedef struct QueueRecord * Queue;
int IsEmpty(Queue);
int IsFull(Queue Q);
Queue CreateQueue(int MaxElements);
void DisposeQueue(Queue Q);
void MakeEmpty(Queue Q);
int Front(Queue Q);
void Dequeue(Queue Q);
#endif // QUEUE_H_INCLUDED
#define MinQueueSize 5
struct QueueRecord
{
int CapaCity;
int Front;
int Rear;
int Size;
int *Array;
};
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
int IsEmpty(Queue Q)
{
return Q->Size==0;
}
Queue CreateQueue(int MaxElements)//创建队列数组
{
Queue Q;
if(MaxElements<MinQueueSize)
{
printf("Queue size is too small");
}
Q=malloc(sizeof(struct QueueRecord));
if(Q==NULL)
{
printf("Out of space");
}
Q->Array=malloc(sizeof(int)*MaxElements);//开辟指定的空间
if(Q->Array==NULL)
{
printf("Out of space");
}
Q->CapaCity=MaxElements;
MakeEmpty(Q);
return Q;
}
void MakeEmpty(Queue Q)
{
Q->Size=0;
Q->Front=0;
Q->Rear=0;
}
static int Succ(int Value ,Queue Q)
{
if(++Value==Q->CapaCity)
{
Value=0;
}
return Value;
}
void EnQueue(int X,Queue Q)
{
if(IsFull(Q))
{
printf("Full queue");
}
else
Q->Size++;
Q->Rear=Succ(Q->Rear,Q);
Q->Array[Q->Rear]=X;
}
int IsFull(Queue Q)
{
return ((Q->Rear)+1)%(Q->CapaCity)==Q->Front;
}
void Dequeue(Queue Q)
{
if(IsEmpty(Q))
{
printf("Empty Queue");
}
else
Q->Rear--;
Q->Size--;
}
void PrintQueue(Queue Q)
{
int i;
printf("The elemet in the queue from front to Rear is :\n");
for(i=Q->Front+1;i<=Q->Size;i=(i+1%(Q->CapaCity)))
{
printf("%d,",Q->Array[i]);
}
printf("\n");
}
int Front(Queue Q)
{
return Front;
}
void DisposeQueue(Queue Q)
{
free(Q->Array);
free(Q);
}
int main()
{
int m;
int temp;
int data=1;
int i=1;
printf("输入想要输入的节点数:");
scanf("%d",&m);
Queue A;
A=CreateQueue(m);
printf("输入第%d个节点数 :",i++);
scanf("%d",&data);
EnQueue(data,A);
printf("输入第%d个节点数 :",i++);
scanf("%d",&data);
EnQueue(data,A);
printf("输入第%d个节点数 :",i++);
scanf("%d",&data);
EnQueue(data,A);
PrintQueue(A);
Dequeue(A);
PrintQueue(A);
return 0;
}
运行结果: