链队列实际上就是个带头尾节点的单链表,并且把插入删除都限定在一端了,相对而言实现起来比较简单,仅供参考
#include<stdio.h>
#include<iostream>
#include<algorithm>
typedef struct LinkNode //链式队列结点
{
int data;
struct LinkNode *next;
}LinkNode;
typedef struct //链式队列
{
LinkNode *front,*rear;
}LinkQueue;
void InitQueue(LinkQueue &Q){ //初始化链队列
Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode)); //建立头结点,头结点的next为空
Q.front->next = NULL;
}
bool IsEmpty(LinkQueue Q){ //判空
if(Q.front == Q.rear){
return false;
}
return true;
}
void EnQueue(LinkQueue &Q,int e) //入队操作
{
LinkNode *s = (LinkNode*)malloc(sizeof(LinkNode));
s->next = NULL;
s->data = e;
Q.rear->next = s;
Q.rear = s;
}
bool DeQueue(LinkQueue &Q,int &e){ //出队操作
if(Q.front == Q.rear){
return false;
}
LinkNode *s;
s = Q.front->next;
e = s->data;
Q.front->next = s->next;
free(s);
return true;
}
void show(LinkQueue Q){ //遍历
LinkNode *s=Q.front->next;
while(s){
printf("%d\t",s->data);
s = s->next;
}
}
int main(){
LinkQueue Q;
InitQueue(Q);
EnQueue(Q,1);
EnQueue(Q,2);
EnQueue(Q,3);
show(Q);
int e;
DeQueue(Q,e);
printf("\n%d\n",e);
show(Q);
return 0;
}