#ifndef LINK_QUEUE_HEAD
#define LINK_QUEUE_HEAD
#include <stdio.h>
#include <stdlib.h>
#define Status int
#define OVERFLOW -1
#define OK 0
#define ERROR 1
typedef int ElemType;
typedef struct _Queue_Node {
ElemType data;
struct _Queue_Node *next;
} Node;
typedef struct {
Node *front;//头指针
Node *rear;//尾指针
} LinkQueue;
//初始化
Status init(LinkQueue *q)
{
q->front = NULL;
q->rear = NULL;
return OK;
}
//销毁队列,注意把队列中的元素全部销毁
Status destroy(LinkQueue *q)
{
q->rear = q->front;
while (q->rear != NULL){
q->rear = q->rear->next;
free(q->front);
q->front = q->rear;
}
q->front = NULL;
q->rear = NULL;
return OK;
}
//压入队列
Status push(LinkQueue *q, ElemType e)
{
Node *p = (Node *)malloc(sizeof(Node));
if (p == NULL) return OVERFLOW;
p->data = e;
p->next = NULL;
if (q->front == NULL)//如果队列为空,则重置头指针
q->front = p;
else
q->rear->next = p;//队列存在,则把当前尾部元素的next指向新入队元素
q->rear = p;//重置尾指针
return OK;
}
//出队
Status pop(LinkQueue *q, ElemType *e)
{
Node *p;
if (q->front == NULL) return OVERFLOW;
*e = q->front->data;
p = q->front;
q->front = q->front->next;//front指针后移
free(p);//已出队的元素需要销毁
p = NULL;
return OK;
}
#endif