#include <stdio.h>
#include <stdlib.h>struct node {
int data;
node *next;
};struct MyQueue
{
node *front;
node *rear;
};
MyQueue *CreatMyQueue(MyQueue *q)
{
MyQueue *q = (MyQueue *)malloc(sizeof(MyQueue));
q->front = NULL;
q->rear = NULL;
return q;
}
bool DestoryMyQueue(MyQueue *q)
{
while (q->front)
{
q->rear = q->front->next;
free(q->front);
q->front = q->rear;
}return true;
}
MyQueue *enqueue(MyQueue *q, int data)
{
node *p = (node *)malloc(sizeof(node));
p->data = data;
p->next = NULL;
if (q->rear == NULL)
{
q->front = p;
q->rear = p;
}
else
{
q->rear->next = p;
q->rear = p;
}
return q;
}
MyQueue *dequeue(MyQueue *q)
{
node *pnode = NULL;
pnode = q->front;
if (pnode == NULL)
{
printf("Empty Queue\n");
}
else
{
q->front = q->front->next;
if (q->front == NULL)
{
q->rear = NULL;
}
free(pnode);
}
return q;
}
int GetLength(MyQueue *q)
{
int len = 0;
node *pnode = q->front;
if (pnode != NULL)
{
len = 1;
}
while (pnode != q->rear)
{
pnode = pnode->next;
len++;
}
return len;
}
void PrintMyQueue(MyQueue *q)
{
node *pnode = q->front;
if (pnode == NULL)
{
printf("Empty Queue\n");
return;
}
while (pnode != NULL)
{
printf("data=%d\n", pnode->data);
pnode = pnode->next;
}
}
int main()
{
MyQueue *hp = NULL;
hp = CreatMyQueue(hp);
hp = enqueue(hp, 1);
hp = enqueue(hp, 2);
hp = enqueue(hp, 3);
hp = enqueue(hp, 4);
PrintMyQueue(hp);
int len = GetLength(hp);
printf("after enqueue len = %d\n", len);
hp = dequeue(hp);
hp = dequeue(hp);
len = GetLength(hp);
printf("after dequeue len = %d\n", len);
PrintMyQueue(hp);
DestoryMyQueue(hp);
PrintMyQueue(hp);
len = GetLength(hp);
printf("after destory len = %d\n", len);
return 0;
}