/*
Author:avalon qq:1243128
Date: 05-10-04 21:18
Description:泛型队列
*/
#ifndef AVALON_QUEUE_H
#define AVALON_QUEUE_H
#ifndef AVA_BOOL
#define AVA_BOOL
#define TRUE 1
#define FALSE 0
typedef int BOOL;
#endif
typedef struct Link * LHandle;
extern LHandle InitQueue(size_t type);
/*构造*/
extern BOOL QueueEmpty(LHandle q);
/*empty?*/
extern int QueueLength(LHandle q);
/*length*/
extern BOOL GetHead(LHandle q, void * elem);
/*若队列不为空,则COPY队头元素,并返回TRUE*/
extern BOOL EnQueue(LHandle q,void * elem);
/*插入元素elem为新的队尾元素*/
extern BOOL DeQueue(LHandle q,void * elem);
/*如elem不为空,则将删除后的头结点值赋值给elem,返回TRUE*/
extern BOOL ClearQueue(LHandle q);
/*清空*/
extern BOOL DestroyQueue(LHandle * q);
/*销毁*/
#endif
//////////////////////
///////////////////
/*
Author:avalon qq:1243128
Date: 05-10-04 21:18
Description:泛型队列
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#ifndef AVA_BOOL
#define AVA_BOOL
#define TRUE 1
#define FALSE 0
typedef int BOOL;
#endif
typedef struct Node{
void * data;/*数据指针*/
struct Node * next;/*下一结点*/
}Node , *NHandle;
typedef struct Link{
NHandle front;/*头指针*/
NHandle rear ;/*尾指针*/
int size ;/*长度*/
size_t type ;
}Link , *LHandle;
/*
内部函数
*/
static NHandle AllocNode(LHandle S,void * elem)
{/*分配结点*/
NHandle node_temp = (NHandle)malloc(sizeof(Node));
void * data_temp ;
assert(NULL !=node_temp);
assert(NULL !=S);
assert(NULL !=elem);
data_temp = malloc(S->type);
assert(NULL !=data_temp);
memcpy(data_temp,elem,S->type);/*copy data*/
node_temp->data = data_temp; /*member data 赋值*/
return node_temp;
}
static BOOL FreeNode(NHandle * node)
{/*释放结点*/
free( (*node)->data);
free( *node);
return TRUE;
}
/*
外部函数
*/
extern LHandle InitQueue(size_t type)
{/*构造*/
LHandle temp = (LHandle)malloc(sizeof(Link));
assert(NULL !=temp);
temp->front = temp->rear =NULL;
temp->size = 0;
temp->type = type;
return temp;
}
extern BOOL QueueEmpty(LHandle q)
{/*empty?*/
assert(NULL !=q);
return ( 0==q->size)?TRUE:FALSE;
}
extern int QueueLength(LHandle q)
{/*length*/
assert(NULL !=q);
return q->size;
}
extern BOOL GetHead(LHandle q, void * elem)
{/*若队列不为空,则COPY队头元素,并返回TRUE*/
assert(NULL !=q);
assert(NULL !=elem);
if(NULL== q->front)return FALSE;
memcpy(elem,q->front->data,q->type);
return TRUE;
}
extern BOOL EnQueue(LHandle q,void * elem)
{/*插入元素elem为新的队尾元素*/
NHandle temp ;
assert(NULL !=q);
assert(NULL !=elem);
temp = AllocNode(q,elem);
/**/
if((q->size)++ != 0){/*长度加1*/
q->rear->next = temp;
q->rear = q->rear->next;
}
else{
q->front=q->rear=temp;
}
return TRUE;
}
extern BOOL DeQueue(LHandle q,void * elem)
{/*如elem不为空,则将删除后的头结点值赋值给elem,返回TRUE*/
NHandle temp;
assert(NULL !=q);
if(0==q->size)return FALSE;
temp= q->front->next;/*新的队头*/
if(NULL!=elem)/*拷贝到elem*/
memcpy(elem,q->front,q->type);
FreeNode( &(q->front));
if( (q->size)-- !=1){
q->front = temp;
}
else{
q->front =q->rear =NULL;
}
return TRUE;
}
extern BOOL ClearQueue(LHandle q)
{/*清空*/
assert(NULL !=q);
while(0 !=q->size)
DeQueue(q,NULL);
return TRUE;
}
extern BOOL DestroyQueue(LHandle * q)
{/*销毁*/
assert(q);
assert(*q);
ClearQueue(*q);
free(*q);
return TRUE;
}