#include "link_queue.h"
/********************************
创建
入列
出列
显示所有数据
是否为空
释放队列
********************************/
//创建1
void create_link_queue_1(pLinkQueue *h)
{
(*h) = (pLinkQueue)malloc(sizeof(tLinkQueue));
if(NULL == (*header))
{
printf("create_link_queue malloc header err\r\n");
return ;
}
(*h)->len = 0;
(*h)->header = NULL;
(*h)->tail = NULL;
}
//创建2
void create_link_queue_2(pLinkQueue h)
{
if(NULL == h)
{
return ;
}
h->len = 0;
h->header = NULL;
h->tail = NULL;
}
//创建3
pLinkQueue create_link_queue_3(void)
{
pLinkQueue new_h;
new_h = (pLinkQueue)malloc(sizeof(tLinkQueue));
if(NULL == new_h)
{
printf("create_link_queue3 malloc header err\r\n");
return;
}
new_h->len = 0;
new_h->header = NULL;
new_h->tail = NULL;
return new_h;
}
//是否为空
int is_empty_link_queue(pLinkQueue h)
{
if((NULL == h) || (0 == (h->len)) || (NULL == h->header))
{
return 1;
}
return 0;
}
//入列:注意 队列只有尾插
int into_link_queue(pLinkQueue h,QUEUE_DATA_TYPE data)
{
pQueueNode new_node;
if(NULL == h)
{
return 1;
}
new_node = (pQueueNode)malloc(sizeof(tQueueNode));
if(NULL == new_node)
{
printf("into_link_queue malloc err\r\n");
return 1;
}
new_node->data = data;
new_node->next = NULL;
h->tail->next = new_node;
h->tail = new_node;
if(NULL == h->header)
{
h->header = new_node;
}
h->len++;
return 0;
}
//出列
int out_link_queue(pLinkQueue h,QUEUE_DATA_TYPE *data)
{
pQueueNode outNode;
if(is_empty_link_queue(h))
{
printf("队列为空\r\n");
return 1;
}
outNode = h->header;
*data = outNode->data;
h->header = outNode->next;
h->len--;
if(outNode == h->tail)
{
h->tail = NULL;
}
free(outNode);
return 0;
}
//显示所有数据
int display_all_link_queue_node(pLinkQueue h)
{
pQueueNode tmpNode;
if(is_empty_link_queue(h))
{
printf("队列为空\r\n");
return 1;
}
for((tmpNode = h->header); (NULL != tmpNode); (tmpNode = tmpNode->next))
{
printf("%d ",tmpNode->data);
}
return 0;
}
//释放链表
int free_link_queue(pLinkQueue h)
{
pQueueNode freeNode;
if(is_empty_link_queue(h))
{
printf("队列为空\r\n");
return 1;
}
while(NULL != (h->header))
{
freeNode = h->header;
h->header = h->header->next;
free(freeNode);
}
free(h);
return 0;
}
#ifndef LINK_QUEUE_H_INCLUDED
#define LINK_QUEUE_H_INCLUDED
#ifndef NULL
#define NULL ((void *)0)
#endif // NULL
#define QUEUE_DATA_TYPE int
typedef struct _QUEUE_NODE
{
QUEUE_DATA_TYPE data;
struct _QUEUE_NODE *next;
}tQueueNode,*pQueueNode;
typedef struct _LINK_QUEUE
{
int len;
pQueueNode header;
pQueueNode tail;
}tLinkQueue,*pLinkQueue;
extern void create_link_queue_1(pLinkQueue *h);
extern void create_link_queue_2(pLinkQueue h);
extern pLinkQueue create_link_queue_3(void);
extern int is_empty_link_queue(pLinkQueue h);
extern int into_link_queue(pLinkQueue h,QUEUE_DATA_TYPE data);
extern int out_link_queue(pLinkQueue h,QUEUE_DATA_TYPE *data);
extern int display_all_link_queue_node(pLinkQueue h);
extern int free_link_queue(pLinkQueue h);
#endif // LINK_QUEUE_H_INCLUDED
如有错误,欢迎指正