# 数据结构：用单链表实现的队列

```#ifndef 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;//尾指针

//初始化
{
q->front = NULL;
q->rear  = NULL;
return OK;
}

//销毁队列，注意把队列中的元素全部销毁
{
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;
}

//压入队列
{
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;
}

//出队
{
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```

原文作者：TsingFung Lee
原文地址: https://www.cnblogs.com/ifan/p/4227899.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。