程序代码:/*********************************
Base Queue
2011-06-15 (year-month-day)
(c) xxc/dxc/cosdos
*********************************/
#include <stdio.h>
#include <stdlib.h>
typedef int Data;
typedef struct Node
{
Data data; // 自定义数据
struct Node *next;
} Node;
typedef struct Queue
{
Node *head;
Node *tail;
int count;
// Node* (*fun_createNode)(Data);
} Queue;
Node* createNode(Data d)
{
Node *p = (Node*)malloc(sizeof(Node));
if (NULL == p) {
fprintf(stderr, "Error: Memory Full!\n");
exit(1);
}
p->next = NULL;
p->data = d;
return p;
}
Queue* initQueue(Queue *l)
{
if (NULL == l)
return NULL;
l->head = l->tail = NULL;
//l->fun_createNode = createNode;
return l;
}
#define COUNT_NODE(L) isEmpty(L)
int isEmpty(Queue *l)
{
return l->count;
}
Node* inQueue(Queue *l, Node *new_node)
{
if (NULL == l || NULL == new_node)
return NULL;
new_node->next = NULL;
// 判断头是否存在,不存在说明是空队列。
//
// 空队列需要特殊对待。
//
// '.' 空队列时: tail == NULL 且 head == NULL
//
// .'. tail = head = new_node
//
// .'. 一个只有一个节点的队列。
//
if (NULL == l->head) {
l->head = new_node;
l->tail = new_node;
} else {
l->tail->next = new_node;
l->tail = new_node;
}
++(l->count);
return new_node;
}
/*
Node* inQueue(Queue *l, Data d)
{
if (NULL == l)
return NULL;
Node *new_node = createNode(d);
if (NULL == l->head) {
l->head = new_node;
l->tail = new_node;
} else {
l->tail->next = new_node;
l->tail = new_node;
}
++(l->count);
return new_node;
}
*/
Node* outQueue(Queue *l)
{
Node *p;
if (NULL == l || NULL == l->head)
return NULL;
p = l->head;
l->head = p->next; // l->head = l->head->next;
if (NULL == l->head)
l->tail = NULL;
--(l->count);
return p;
}
/*
Data outQueue(Queue *l)
{
Node *p;
Data d;
if (NULL == l || NULL == l->head)
return NULL;
p = l->head;
l->head = p->next; // l->head = l->head->next;
if (NULL == l->head)
l->tail = NULL;
--(l->count);
d = p->data;
free(p);
return d;
}
*/
void clearQueue(Queue *l)
{
Node *first;
while(l->head) {
first = l->head;
l->head = first->next; // l->head = l->head->next;
free(first);
// free(first->data); // 如果Node中使用一个数据指针。
}
l->count = 0;
l->tail = l->head;
}[ 本帖最后由 cosdos 于 2011-6-15 21:40 编辑 ]

—>〉Sun〈<—








