#include <stdio.h>
#include <malloc.h>
typedef int T;
typedef struct QNode
{
T data;
QNode* next;
}*Node;
typedef struct
{
Node front, rear;
int count;
}*LinkQuene;
bool InitQuene(LinkQuene q)
{
q->front = q->rear = (Node)malloc(sizeof(Node));
q->front->next = NULL;
q->count = 0;
return true;
}
bool DetoryQuene(LinkQuene Q)
{
while (Q->front)
{
Q->rear = Q->front->next;
free(Q->front);
Q->front = Q->rear;
}
Q->count = 0;
return true;
}
bool ClearQuene(LinkQuene Q)
{
Node p, q;
Q->rear = Q->front;
p = Q->front;
Q->front->next = NULL;
while (p)
{
q = p->next;
free(p);
p = q;
}
Q->count = true;
return true;
}
bool Is_Empty(LinkQuene Q)
{
return Q->front == Q->rear;
}
int GetLength(LinkQuene q)
{
return q->count;
}
T GetHead(LinkQuene q)
{
return q->front->next->data;
}
bool Push(LinkQuene q, T val)
{
if (!q)
return false;
Node node = (Node)malloc(sizeof (Node));
node->data = val;
node->next = NULL;
q->rear->next = node;
q->rear = node;
q->count++;
return true;
}
T Pop(LinkQuene Q)
{
if (!Q)
return false;
Node p;
T tmp;
p = Q->front->next;
tmp = p->data;
Q->front->next = p->next;
if (p == Q->rear)
Q->rear = Q->front;
Q->count--;
return true;
}
void Show(LinkQuene Q)
{
Node p;
p = Q->front->next;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
LinkQuene Q = (LinkQuene)malloc(sizeof(LinkQuene));
InitQuene(Q);
for (int i = 1; i <= 5; i++)
{
Push(Q, i);
}
Show(Q);
printf("Head: %d\n", GetHead(Q));
printf("Len: %d\n", GetLength(Q));
printf("Pop: %d\n", Pop(Q));
return 0;
}