队列的数组实现及其基础操作

#include
#include
typedef int ElementType ; 

struct QueueRecord
{
int Capacity;
int Front;
int Rear;
int Size;
ElementType *Array;
};
typedef struct QueueRecord *Queue; 


int IsFull(Queue Q) //判满 
{
return (Q->Size == Q->Capacity);





int IsEmpty(Queue Q) //判空 
{
return Q->Size ==  0;
}




Queue CreatQueue(int size) //创建队列大小为size 
{                                                                                               //在这里我要多说几句,创建的时候一定要传入值和在主函

Queue Q;                                                                        //函数里面加    Q = , 博主因为这个错误花费了好长时间,

                                                                                               //或者传入 &Q ,不能直接传入Q,因为传入的是Q的复制品,

                                                                                                //并不会改变原来Q的值

Q = (Queue)malloc(sizeof(struct QueueRecord));
Q->Array = (ElementType*)malloc(sizeof(ElementType) * size);

Q->Capacity = size;
Q->Front = 1;
Q->Rear = 0;
Q->Size = 0;

return Q;
}


int Succ(int Value,Queue Q) //实现循环队列 
{
if( ++Value  == Q->Capacity)
Value = 0;

return Value;



void EnQueue(Queue Q,ElementType X) //入队 
{
if(IsFull( Q))
printf("Queue is Full\n");
else 
{

Q->Rear = Succ(Q->Rear,Q);
Q->Array[Q->Rear] = X;
Q->Size++;
}




ElementType TopQueue(Queue Q) //取队头元素 
{
if(IsEmpty(Q))
printf("Queue is Empty\n");
else 
{
return Q->Array[Q->Front];
}

}


void PopQueue(Queue Q) //出对 
{
if(IsEmpty(Q))
printf("Queue is Empty\n");
else
{
Q->Size--;
Q->Front = Succ(Q->Front,Q);
}



void Print(ElementType X) //打印X 
{
printf("%d",X);
}


void PPT(Queue Q) //打印 Q中的信息 
{
printf("Q->Capacity = %d\n",Q->Capacity);
printf("Q->Front = %d\n",Q->Front);
printf("Q->Rear = %d\n",Q->Rear);
printf("Q->Size = %d\n\n",Q->Size);
}




int main(void)
{
ElementType t;
Queue Q = NULL;

Q = CreatQueue(3);

EnQueue(Q,45);


EnQueue(Q,98);

PopQueue(Q);

PopQueue(Q);




t = TopQueue(Q);
Print(t);

return 0;
}









你可能感兴趣的:(C语言基本算法)