队列的链式存储
作者:
小鸡炖土豆
,
2022-06-01 16:05:51
,
所有人可见
,
阅读 169
#include<stdio.h>
#include<malloc.h>
typedef int Elemtype;
typedef struct qnode
{
Elemtype data;
struct qnode* next;
}DataNode;
typedef struct
{
DataNode* front;
DataNode* rear;
}Lq;
void InitQueue(Lq*& q)
{
q = (Lq*)malloc(sizeof(Lq));
q->front = q->rear = NULL;
}
bool Enqueue(Lq*& q, Elemtype e)
{
DataNode* s;
s = (DataNode*)malloc(sizeof(DataNode));
s->data = e;
s->next = NULL;
if (q->rear == NULL)
{
q->front = q->rear = s;
}
else
{
q->rear->next = s;
q->rear = s;
}
}
bool Dequeue(Lq*& q, Elemtype& e)
{
DataNode* T;
if (q->rear == NULL)
{
return false;
}
T = q->front;
if (q->front == q->rear)
{
q->front = q->rear = NULL;
}
else
{
q->front = q->front -> next;
}
e = T->data;
free(T);
return true;
}