common.h
#ifndef __COMMON_H__
#define __COMMON_H__
#define QUEUE_SIZE_MAX 5
typedef int QElemType;
typedef struct queue_s
{
QElemType *base; // queue base
int front; // head
int rear; // tail
}QUEUE_S, *PQUEUE_S;
int queue_init(QUEUE_S *q);
void queue_deinit(QUEUE_S *q);
int queue_en(QUEUE_S *q, QElemType e);
int queue_de(QUEUE_S *q, QElemType *e);
void queue_dump(QUEUE_S *q);
typedef struct qnode_s
{
QElemType data;
struct qnode_s *next;
}QNODE_S, *PQNODE_S;
typedef struct linkqueue_s
{
PQNODE_S front;
PQNODE_S rear;
}LINKQUEUE_S, *PLINKQUEUE_S;
int linkqueue_init(LINKQUEUE_S *q);
void linkqueue_deinit(LINKQUEUE_S *q);
int linkqueue_en(LINKQUEUE_S *q, QElemType e);
int linkqueue_de(LINKQUEUE_S *q, QElemType *e);
void linkqueue_dump(LINKQUEUE_S *q);
#endif
顺序队列queue.c
//顺序队列
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
int queue_init(QUEUE_S *q)
{
if(!q)
{
printf("ERR: NULL pointer.\n");
return -1;
}
q->base = malloc(QUEUE_SIZE_MAX * sizeof(QElemType));
if(!(q->base))
{
printf("ERR: malloc fail\n");
return -1;
}
q->rear = q->front = 0;
return 0;
}
void queue_deinit(QUEUE_S *q)
{
if(!q)
{
printf("ERR: NULL pointer.\n");
return;
}
if(!(q->base))
{
printf("ERR: queue is null.\n");
return;
}
free(q->base);
q->base = NULL;
}
int queue_en(QUEUE_S *q, QElemType e)
{
if(!q)
{
printf("ERR: NULL pointer.\n");
return -1;
}
/* full? */
if(((q->rear + 1) % QUEUE_SIZE_MAX) == q->front)
{
printf("ERR: queue is full.\n");
return -1;
}
q->base[q->rear] = e;
q->rear = (q->rear+1) % QUEUE_SIZE_MAX;
return 0;
}
int queue_de(QUEUE_S *q, QElemType *e)
{
if(!q)
{
printf("ERR: NULL pointer.\n");
return -1;
}
/* empty? */
if(q->rear == q->front)
{
printf("ERR: queue is empty.\n");
return -1;
}
*e = q->base[q->front];
q->front = (q->front+1) % QUEUE_SIZE_MAX;
return 0;
}
void queue_dump(QUEUE_S *q)
{
int i;
QElemType e;
int used_size;
if(!q)
{
printf("ERR: NULL pointer.\n");
return;
}
printf("======================================\n");
printf(" queue dump \n");
printf("======================================\n");
used_size = (QUEUE_SIZE_MAX + q->rear - q->front) % QUEUE_SIZE_MAX;
printf(" Used / Total : %d / %d.\n", used_size, QUEUE_SIZE_MAX);
for(i = 0; i < used_size; i++)
{
e = q->base[(q->front + i) % QUEUE_SIZE_MAX];
printf(" %02d: %d.\n", (q->front + i), e);
}
}
链队linkqueue.c
//链队
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
#if 1
int linkqueue_init(LINKQUEUE_S *q)
{
if(!q)
{
printf("ERR: NULL pointer.\n");
return -1;
}
q->front = q->rear = malloc(sizeof(QNODE_S));
q->front->next = NULL;
return 0;
}
void linkqueue_deinit(LINKQUEUE_S *q)
{
PQNODE_S ptr, tmp;
if(!(q))
{
printf("ERR: %s--> link stack not exist.\n", __func__);
return;
}
ptr = (q->front);
while(ptr->next != NULL)
{
tmp = ptr;
free(ptr);
ptr = tmp->next;
}
free(q->front);
}
int linkqueue_en(LINKQUEUE_S *q, QElemType e)
{
PQNODE_S ptr = malloc(sizeof(QNODE_S));
ptr->data = e;
ptr->next = NULL;
(q->rear)->next = ptr;
q->rear = ptr;
return 0;
}
int linkqueue_de(LINKQUEUE_S *q, QElemType *e)
{
PQNODE_S ptr;
if(!(q))
{
printf("ERR: %s--> link stack not exist.\n", __func__);
return -1;
}
// empty?
if(q->front == q->rear)
{
printf("ERR: %s--> link stack empty.\n", __func__);
return -1;
}
ptr = (q->front)->next;
*e = ptr->data;
(q->front)->next = ptr->next;
// empty now?
if(q->rear == ptr)
{
q->rear = q->front;
q->front->next = NULL;
}
free(ptr);
return 0;
}
void linkqueue_dump(LINKQUEUE_S *q)
{
int i = 0;
PQNODE_S ptr = q->front;
if(!(q))
{
printf("ERR: %s--> link stack not exist.\n", __func__);
return;
}
printf("======================================\n");
printf(" linkt stack dump \n");
printf("======================================\n");
while(ptr->next != NULL)
{
printf("top - %d : %d.\n", i, (ptr->next)->data);
i++;
ptr = ptr->next;
}
printf("==========dump=end====================\n");
}
#endif