1.1链队列
仅限在表头删除和表尾插入的单链表,为了便于在表尾插入,需增加一个尾指针,将头指针和尾指针封装在一起类型定义如下:
typedef struct node
{
datatype data;
struct node*next;
}QueueNode;//链队列结点类型
typedef struct
{
QueueNode *front,*rear;
}LinkQueue;//队头队尾指针的结构体类型
1.2链队列的基本运算
1.2.1初始化
void InitQueue(LinkQueue*&q)
{
q=(LinkQueue*)malloc(sizeof(LinkQueue));//产生队尾队头指针结构体
q->front=q->rear=(QueueNode*)malloc(sizeof(QueueNode));//产生头结点
q->front->next=NULL;//头结点置空
}
1.2.2入队
void EnQueue(LinkQueue *q,datatype x)
{
q->rear->next=(QueueNode*)malloc(sizeof(QueueNode));//先开辟一个空间给新的结点
q->rear=q->rear->next;//尾指针指向新的结点
q->rear->data=x;
q->rear->next=NULL;//对尾结点置空
}
链队列不会出现上溢,故不需要判断队满。
1.2.3出队
int DeQueue(LinkQueue*q,datatype x)
{
QueueNode*s;
if(q->front==q->rear)//判断是否为空队列,是否下溢
{
printf("下溢");
return 0;
}
else
{
s=q->front->rear;//s指向被删除的队头结点
if(s->next=NULL)
{
q->front->next=NULL;
q->rear=q->front;
}
else
{
q->front->next=s->next;
x=s->data;
free(s);
return 1;
}
}
}