#include <stdio.h>
#include "stdlib.h"
typedef struct MyQueueNode
{
int data;
struct MyQueueNode *front;
}MyQueueNode;
typedef struct MyQueue
{
int length;
MyQueueNode *head;
MyQueueNode *real;
}MyQueue;
MyQueue* MyQueue_create()
{
MyQueue *queue = (MyQueue *)malloc(sizeof(MyQueue));
queue->head = NULL;
queue->real = NULL;
queue->length = 0;
return queue;
}
int MyQueue_isEmpty(MyQueue *queue)
{
if(queue == NULL) return 1;
return queue->length == 0;
}
void MyQueue_enqueue(MyQueue *queue, int data)
{
if(queue == NULL) return;
MyQueueNode *node = (MyQueueNode *)malloc(sizeof(MyQueueNode));
node->data = data;
node->front = NULL;
if(queue->head == NULL)
{
queue->head = node;
}
else
{
queue->real->front = node;
}
queue->real = node;
queue->length ++;
}
int MyQueue_dequeue(MyQueue *queue)
{
if(queue == NULL) return 0;
MyQueueNode *topNode = queue->head;
queue->head = topNode->front;
queue->length --;
int data = topNode->data;
if(topNode)
{
free(topNode);
topNode = NULL;
}
if(queue->length == 0)
{
queue->head = NULL;
queue->real = NULL;
}
return data;
}
void MyQueue_myprintln(MyQueue *queue)
{
if(queue == NULL || queue->length == 0) return;
MyQueueNode *p = queue->head;
while (p) {
printf("%d ", p->data);
p = p->front;
}
}
void main()
{
MyQueue *linkQueue = MyQueue_create();
MyQueue_enqueue(linkQueue, 10);
MyQueue_enqueue(linkQueue, 11);
MyQueue_enqueue(linkQueue, 15);
MyQueue_enqueue(linkQueue, 17);
MyQueue_enqueue(linkQueue, 18);
MyQueue_myprintln(linkQueue);
int data = MyQueue_dequeue(linkQueue);
MyQueue_myprintln(linkQueue);
}