/**
* @file GM_Queue.h
* @brief 采用链表实现,链表的头部为队首, 链表的尾部为队尾, Enqueue在队尾进行操作, Dequeue在队首进行操作
* @author Don Hao
* @date 2011-8-22 22:32:57
* @version
* <pre><b>copyright: </b></pre>
* <pre><b>email: </b>hao.limin@gmail.com</pre>
* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>
* <pre><b>All rights reserved.</b></pre>
* <pre><b>modification:</b></pre>
* <pre>Write modifications here.</pre>
*/
#ifndef _GM_QUEUE_H
#define _GM_QUEUE_H
#include <stdlib.h>
#ifdef __cplusplus
extern"C"
{
#endif /**< __cplusplus */
/**
* @brief GM_Queue_Enqueue
*
* 入队.
* @param[in] value
* @return int
*/
int GM_Queue_Enqueue(int value);
/**
* @brief GM_Queue_Dequeue
*
* 出队.
* @param[out] value
* @return int
*/
int GM_Queue_Dequeue(int* value);
/**
* @brief GM_Queue_Clear
*
* 清队.
*/
void GM_Queue_Clear();
/**
* @brief GM_Queue_Length
*
* 队长.
* @return int
*/
int GM_Queue_Length();
#ifdef __cplusplus
}
#endif /**< __cplusplus */
#endif /**< _GM_QUEUE_H */
/**
* @file GM_Queue.c
* @brief
* @author Don Hao
* @date 2011-8-22 22:32:51
* @version
* <pre><b>copyright: </b></pre>
* <pre><b>email: </b>hao.limin@gmail.com</pre>
* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>
* <pre><b>All rights reserved.</b></pre>
* <pre><b>modification:</b></pre>
* <pre>Write modifications here.</pre>
*/
#include "GM_Queue.h"
#include <stdio.h>
typedef struct Queue
{
int value;
struct Queue* next;
}Queue_Struct;
static Queue_Struct* head = NULL;
static Queue_Struct* tail = NULL;
static int count = 0;
int GM_Queue_Enqueue( int value )
{
Queue_Struct* tmp = (Queue_Struct*)malloc(sizeof(Queue_Struct));
if (NULL == tmp)
{
return -1;
}
tmp->value = value;
tmp->next = NULL;
if (NULL == tail)
{
head = tmp;
}
else
{
tail->next = tmp;
}
tail = tmp;
++count;
return 1;
}
int GM_Queue_Dequeue( int* value )
{
Queue_Struct* tmp = NULL;
if ((NULL == head) || (NULL == value))
{
return -1;
}
*value = head->value;
tmp = head;
if (head == tail)
{
head = NULL;
tail = NULL;
}
else
{
head = head->next;
}
free(tmp);
tmp = NULL;
--count;
return 1;
}
void GM_Queue_Clear()
{
int i = 0;
int value = 0;
while (count > 0)
{
GM_Queue_Dequeue(&value);
}
}
int GM_Queue_Length()
{
return count;
}
void main()
{
int i = 0;
int rt = -1;
int value = 0;
for (i = 0; i < 10; ++i)
{
rt = GM_Queue_Enqueue(i);
printf("ENQUEUE rt=%d: value=%d\n", rt, i);
}
printf("COUNT=%d\n", GM_Queue_Length());
for (i = 0; i < 10; ++i)
{
rt = GM_Queue_Dequeue(&value);
printf("DEQUEUE rt=%d: value=%d\n", rt, value);
}
rt = GM_Queue_Dequeue(&value);
printf("DEQUEUE rt=%d: value=%d\n", rt, value);
for (i = 0; i < 10; ++i)
{
rt = GM_Queue_Enqueue(i);
printf("ENQUEUE rt=%d: value=%d\n", rt, i);
}
GM_Queue_Clear();
printf("COUNT=%d\n", GM_Queue_Length());
}