存储结构定义(巧用void* 实现泛型队列)
typedef struct Aqueue{
void *data[MAXQUEUE];
int front;
int rear;
size_t data_size;
} AQueue;
typedef enum{
FALSE=0, TRUE=1
} Status;
初始化队列
void InitAQueue(AQueue *Q){
int i;
for (i = 0; i < MAXQUEUE; i++)
Q->data[i] = (void *)malloc(Q->data_size);
Q->front = 0;
Q->rear = 0;
}
判断空队列
Status IsEmptyAQueue(const AQueue *Q) {
return (Q->front == Q->rear?TRUE: FALSE);
}
判断满队列
Status IsFullAQueue(const AQueue *Q) {
return (Q->front == (Q->rear + 1) % MAXQUEUE? TRUE: FALSE);
}
入队
Status EnAQueue(AQueue *Q, void *data) {
if (IsFullAQueue(Q))
return FALSE;
Q->rear = (Q->rear+1) % MAXQUEUE;
memcpy(Q->data[Q->rear], data, Q->data_size);
return TRUE;
}
出队
Status DeAQueue(AQueue *Q) {
if (Q->front == Q->rear)
return FALSE;
Q->front = (Q->front + 1) % MAXQUEUE;
return TRUE;
}
销毁队列
void DestoryAQueue(AQueue *Q) {
int i;
for (i = 0; i < MAXQUEUE; i++)
free(Q->data[i]);
return;
}
清空队列(内存空间未释放)
void ClearAQueue(AQueue *Q) {
if (IsEmptyAQueue(Q))
return;
Q->front = 0;
Q->rear = 0;
return;
}
获得队列长度
int LengthAQueue(AQueue *Q) {
return (Q->rear - Q->front + MAXQUEUE) % MAXQUEUE;
}
获得队头元素(此处也可以使用二级指针)
Status GetHeadAQueue(AQueue *Q, void *e) {
if (IsEmptyAQueue(Q))
return FALSE;
int i = Q->front;
i = (i + 1) % MAXQUEUE;
memcpy(e, Q->data[i], Q->data_size);
return TRUE;
}
遍历队列
Status TraverseAQueue(const AQueue *Q, void (*foo)(void *q, int size)) {
if (Q->front == Q->rear)
return FALSE;
int i = Q->front + 1;
while (i <= (MAXQUEUE - Q->front + Q->rear) % MAXQUEUE ) {
foo(Q->data[i], Q->data_size);
i = (i + 1) % MAXQUEUE;
}
printf("\n");
return TRUE;
}
void APrint(void *q, int size) {
if (size == sizeof(double))
printf("%lf", *(double *)q);
if (size == sizeof(char))
printf("%c", *(char *)q);
if (size == sizeof (int))
printf("%d", *(int *)q);
printf("-<");
}
存储结构定义
typedef struct node{
void* data;
struct node *next;
} Node;
typedef struct Lqueue{
Node *front;
Node *rear;
size_t data_size;
} LQueue;
typedef enum{
FALSE=0, TRUE=1
} Status;
初始化带头结点的链队列
void InitLQueue(LQueue *Q) {
Node *p = (Node *)malloc(sizeof(Node));
if (NULL == p)
return;
p->next = NULL;
Q->front = p;
Q->rear = p;
return;
}
判空
Status IsEmptyLQueue(const LQueue *Q) {
return (Q->front == Q->rear? TRUE: FALSE);
}
入队
Status EnLQueue(LQueue *Q, void *data) {
Node *p = (Node *)malloc(sizeof(Node));
if (NULL == p)
return FALSE;
p->data = (void *)malloc(Q->data_size);
memcpy(p->data, data, Q->data_size);
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
return TRUE;
}
出队
Status DeLQueue(LQueue *Q) {
if(IsEmptyLQueue(Q))
return FALSE;
Node *p = (Node *)malloc(sizeof(Node));
p = Q->front->next;
Q->front->next = p->next;
if (NULL == Q->front->next)
Q->rear = Q->front;
free(p);
return TRUE;
}
获得链队列头元素
Status GetHeadLQueue(LQueue *Q, void *e) {
if (IsEmptyLQueue(Q))
return FALSE;
memcpy(e, Q->front->next->data, Q->data_size);//在头结点下一个节点指向的位置赋予字节数
return TRUE;
}
队列长度
int LengthLQueue(LQueue *Q) {
int length;
Node *p = (Node *)malloc(sizeof(Node));
p = Q->front;
for (length = 0; p != Q->rear; length++)
p = p->next;
return length;
}
销毁队列
void DestoryLQueue(LQueue *Q) {
if (IsEmptyLQueue(Q))
return;
ClearLQueue(Q);
free(Q->front);
free(Q->rear);
}
清空队列
void ClearLQueue(LQueue *Q) {
if (IsEmptyLQueue(Q))
return;
Node *p, *q;
Q->rear = Q->front;
p = Q->front->next;
while (NULL != p) {
q = p;
p = p->next;
free(q);
}
free(p);
return;
}
遍历链队列
//foo利用Lprint打印函数
Status TraverseLQueue(const LQueue *Q, void (*foo)(void *q, int size)) {
if (IsEmptyLQueue(Q))
return FALSE;
Node* p = (void *)malloc(Q->data_size);
p = Q->front->next;
int i = 1;
while (p != NULL) {
if (i % 5 == 0) printf("\n");
foo(p->data, Q->data_size);
p = p->next;
i++;
}
printf("\n");
return TRUE;
}
//根据字节数来实现遍历
void LPrint(void *q, int size) {
if (size == sizeof(double))
printf("%lf", *(double *)q);
if (size == sizeof(char))
printf("%c", *(char *)q);
if (size == sizeof (int))
printf("%d", *(int *)q);
printf("-<");
}