#include<stdio.h>
#include<stdlib.h>
#ifndef _Queue_h
struct QueueRecord;
typedef struct QueueRecord *Queue;
typedef int ElementType;
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 Front(Queue Q);
void Dequeue(Queue Q);
#endif
struct QueueRecord
{
int Capacity;//容量
int Front;//队首
int Rear;//队尾
int Size;//队列中有几个元素
ElementType *Array;//队列数组
};
int IsEmpty(Queue Q)
{
return Q->Size==0;
}
int IsFull(Queue Q)
{
return Q->Size==Q->Capacity;
}
Queue CreateQueue(int MaxElement)
{
Queue Q;
Q=(struct QueueRecord*)malloc(sizeof(struct QueueRecord));
if(Q==NULL)printf("Out of space!\n");
Q->Array=(ElementType*)malloc(sizeof(ElementType)*MaxElement);
if(Q->Array==NULL)printf("Out of space!\n");
Q->Capacity=MaxElement;
MakeEmpty(Q);
return Q;
}
void DisposeQueue(Queue Q)
{
if(Q!=NULL)
{
free(Q->Array);
free(Q);
}
}
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))printf("Full Queue!\n");
else{
Q->Size++;
Q->Rear=Succ(Q->Rear,Q);
Q->Array[Q->Rear]=X;
}
}
ElementType Front(Queue Q)
{
if(IsEmpty(Q))
{
printf("Empty Queue!\n");
return 0;
}
return Q->Array[Q->Front];
}
void Dequeue(Queue Q)
{
if(IsEmpty(Q))printf("Empty Queue!\n");
else
Q->Front++;
}
int main()
{
system("pause");
return 0;
}
数据结构——队列
最新推荐文章于 2024-07-09 19:36:25 发布