#include<assert.h>
#define MAXQSIZE 100
typedef int ElemType;
typedef struct queue
{
ElemType*data;
int front;
int rear;
}Queue,*PQueue;
void Init_Queue(PQueue pq);
bool Push(PQueue pq, ElemType val);
bool Pop(PQueue pq, ElemType* rtval);
bool Top(PQueue pq, ElemType* rtval);
int Get_length( PQueue pq);
bool IsEmpty(PQueue pq);
bool IsFull(PQueue pq);
void Clear(PQueue pq);
void Destory(PQueue pq);
void Show(PQueue pq);
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"queue.h"
void Init_Queue(PQueue pq)
{
assert(pq!= NULL);
pq->data = (ElemType*)malloc(sizeof(ElemType*) * MAXQSIZE);
assert(pq->data != NULL);
pq->front = 0;
pq->rear = 0;
}
bool Push(PQueue pq, ElemType val)
{
assert(pq != NULL);
if (IsFull(pq))
return false;
pq->data[pq->rear] = val;
pq->rear = (pq->rear + 1) % MAXQSIZE;
return true;
}
bool Pop(PQueue pq, ElemType* rtval)
{
assert(pq != NULL);
if (IsEmpty(pq))
return false;
*rtval = pq->data[pq->front];
pq->front = (pq->front + 1) % MAXQSIZE;
return true;
}
bool Top(PQueue pq, ElemType* rtval)
{
if (IsEmpty(pq))return false;
*rtval = pq->data[pq->front];
return true;
}
int Get_length(PQueue pq)
{
int length = (pq->rear - pq->front + MAXQSIZE) % MAXQSIZE;
return length;
}
bool IsEmpty(PQueue pq)
{
return pq->front == pq->rear;
}
bool IsFull(PQueue pq)
{
return (pq->rear + 1) % MAXQSIZE == pq->front;
}
void Clear(PQueue pq)
{
pq->rear = 0;
pq->front = 0;
}
void Destory(PQueue pq)
{
free(pq->data);
pq->data = NULL;
pq->front = pq->rear = 0;
}
void Show(PQueue pq)
{
for (int i=pq->front;i!=pq->rear;i=(i+1)%MAXQSIZE)
{
printf("%d ", pq->data[i]);
}
printf("\n");
}
int main()
{
Queue qu;
Init_Queue(&qu);
for (int i= 0; i < 10; i++)
{
Push(&qu, i + 1);
}
Show(&qu);
int tmp;
Top(&qu,&tmp);
printf("%d\n",tmp);
Show(&qu);
return 0;
}