定义
队列的链式存储结构,其实就是线性表的单链表,只不过它只能尾进头出而异,我们把它简称为链队列
链队列 VS 循环队列
时间角度
它们的基本操作都是常数时间,为 O[1] ,不过循环队列是事先申请好空间,使用期间不释放
对于链队列,每次申请和释放节点也会存在一点儿时间开销
空间角度
循环队列必须有一个固定的长度,所以有了存储元素个数和空间浪费的问题。而链队列更加灵活。
汇总
在可以确定队列长度最大值得情况下,建议使用循环队列,否则使用链队列。
链队列代码实现( C 、Java )
C 语言
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配量 */
typedef int Status;
typedef int QElemType; /* QElemType类型根据实际情况而定,这里假设为int */
typedef struct QNode /* 结点结构 */
{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct /* 队列的链表结构 */
{
QueuePtr front,rear; /* 队头、队尾指针 */
}LinkQueue;
Status visit(QElemType c)
{
printf("%d ",c);
return OK;
}
/* 构造一个空队列Q */
Status InitQueue(LinkQueue *Q)
{
Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q->front)
exit(OVERFLOW);
Q->front->next=NULL;
return OK;
}
/* 销毁队列Q */
Status DestroyQueue(LinkQueue *Q)
{
while(Q->front)
{
Q->rear=Q->front->next;
free(Q->front);
Q->front=Q->rear;
}
return OK;
}
/* 将Q清为空队列 */
Status ClearQueue(LinkQueue *Q)
{
QueuePtr p,q;
Q->rear=Q->front;
p=Q->front->next;
Q->front->next=NULL;
while(p)
{
q=p;
p=p->next;
free(q);
}
return OK;
}
/* 若Q为空队列,则返回TRUE,否则返回FALSE */
Status QueueEmpty(LinkQueue Q)
{
if(Q.front==Q.rear)
return TRUE;
else
return FALSE;
}
/* 求队列的长度 */
int QueueLength(LinkQueue Q)
{
int i=0;
QueuePtr p;
p=Q.front;
while(Q.rear!=p)
{
i++;
p=p->next;
}
return i;
}
/* 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR */
Status GetHead(LinkQueue Q,QElemType *e)
{
QueuePtr p;
if(Q.front==Q.rear)
return ERROR;
p=Q.front->next;
*e=p->data;
return OK;
}
/* 插入元素e为Q的新的队尾元素 */
Status EnQueue(LinkQueue *Q,QElemType e)
{
QueuePtr s=(QueuePtr)malloc(sizeof(QNode));
if(!s) /* 存储分配失败 */
exit(OVERFLOW);
s->data=e;
s->next=NULL;
Q->rear->next=s; /* 把拥有元素e的新结点s赋值给原队尾结点的后继,见图中① */
Q->rear=s; /* 把当前的s设置为队尾结点,rear指向s,见图中② */
return OK;
}
/* 若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR */
Status DeQueue(LinkQueue *Q,QElemType *e)
{
QueuePtr p;
if(Q->front==Q->rear)
return ERROR;
p=Q->front->next; /* 将欲删除的队头结点暂存给p,见图中① */
*e=p->data; /* 将欲删除的队头结点的值赋值给e */
Q->front->next=p->next;/* 将原队头结点的后继p->next赋值给头结点后继,见图中② */
if(Q->rear==p) /* 若队头就是队尾,则删除后将rear指向头结点,见图中③ */
Q->rear=Q->front;
free(p);
return OK;
}
/* 从队头到队尾依次对队列Q中每个元素输出 */
Status QueueTraverse(LinkQueue Q)
{
QueuePtr p;
p=Q.front->next;
while(p)
{
visit(p->data);
p=p->next;
}
printf("\n");
return OK;
}
int main()
{
int i;
QElemType d;
LinkQueue q;
i=InitQueue(&q);
if(i)
printf("成功地构造了一个空队列!\n");
printf("是否空队列?%d(1:空 0:否) ",QueueEmpty(q));
printf("队列的长度为%d\n",QueueLength(q));
EnQueue(&q,-5);
EnQueue(&q,5);
EnQueue(&q,10);
printf("插入3个元素(-5,5,10)后,队列的长度为%d\n",QueueLength(q));
printf("是否空队列?%d(1:空 0:否) ",QueueEmpty(q));
printf("队列的元素依次为:");
QueueTraverse(q);
i=GetHead(q,&d);
if(i==OK)
printf("队头元素是:%d\n",d);
DeQueue(&q,&d);
printf("删除了队头元素%d\n",d);
i=GetHead(q,&d);
if(i==OK)
printf("新的队头元素是:%d\n",d);
ClearQueue(&q);
printf("清空队列后,q.front=%u q.rear=%u q.front->next=%u\n",q.front,q.rear,q.front->next);
DestroyQueue(&q);
printf("销毁队列后,q.front=%u q.rear=%u\n",q.front, q.rear);
return 0;
}
Java 语言
package com.example.stack;
/* 链队列结构
* 备注:头结点front.data不存储元素
* */
public class LinkQueue<T> {
@SuppressWarnings("hiding")
class Node<T> {
protected Node<T> next;
protected T data;
public Node(T data){
this.data = data;
}
}
/* 队头、队尾指针 */
public Node<T> front;
/*链栈长度 */
public Node<T> rear;
/* 构造一个空队列Q */
int initQueue(){
this.front = new Node<T>(null);
this.rear = new Node<T>(null);
return 1;
}
/*
* 销毁队列 VS 清空队列 ?
* 销毁队列:队列的结构都不存在了
* 清空队列:有效结点清除了,但队头队尾还在,队列结构还在
* 在 Java 无法做到清除结构亦或者构建的元素,顾两方法都一致
* 备注;也许以后的 Java 可以做到,或没有必要
*/
/* 销毁队列Q */
int destroyQueue(){
Node<T> tempNode = this.front.next;
while(tempNode.data != null){
tempNode.data = null;
tempNode = tempNode.next;
this.front.next = tempNode;
}
return 1;
}
/* 将Q清空队列 */
int clearQueue(){
Node<T> tempNode = this.front.next;
while(tempNode.data != null){
tempNode.data = null;
tempNode = tempNode.next;
this.front.next = tempNode;
}
return 1;
}
/* 若Q为空队列,则返回TRUE,否则返回FALSE */
boolean queueEmpty(){
if(this.front.data == null && this.rear.data == null)
return true;
else
return false;
}
/* 求队列的长度
* 遗留:Java 中New的两个新的引用对象 a 和 b,在进行 = 号表达时,无法判断它们彼此的地址是否相等?
* 如本题中的 front 与 rear,无法与 C 语言中的一致,进行指针的比较
* */
int queueLength(){
int i = 0;
if(this.front.data == null && this.rear.data == null)
return i;
else{
i = 0;
this.front = this.front.next;
while(this.front.data != null){
i++;
this.front = this.front.next;
}
}
return i;
}
/* 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR */
T getHead(){
if(this.front.data == null && this.rear.data == null)
throw new NullPointerException("为空队列!");
return this.front.next.data;
}
/* 插入元素e为Q的新的队尾元素 */
int enQueue(T elem){
Node<T> tempNode = new Node<T>(elem);
if(this.queueLength() == 0){
this.rear=tempNode;
this.rear.next = this.front;
this.front.next = this.rear;
}else{
this.rear.next = tempNode;
this.rear = tempNode;
this.rear.next = this.front;
}
return 1;
}
/* 若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR */
T deQueue(){
if(this.front.data == null && this.rear.data == null)
throw new NullPointerException("为空队列!");
T elem = this.front.next.data;
//待删除元素赋予空值
this.front.next.data =null;
this.front.next = this.front.next.next;
return elem;
}
/* 从队头到队尾依次对队列Q中每个元素输出 */
void queueTraverse(){
Node<T> tempNode = this.front.next;
while(tempNode.data != null){
System.out.println(tempNode.data);
tempNode = tempNode.next;
}
}
public static void main(String[] args){
LinkQueue<Integer> q = new LinkQueue<Integer>();
q.initQueue();
System.out.println("是否空队列?%d(1:空 0:否) "+q.queueEmpty());
System.out.println("队列的长度为%d\n"+q.queueLength());
q.enQueue(-5);
q.enQueue(5);
q.enQueue(10);
System.out.println("插入3个元素(-5,5,10)后,队列的长度为%d\n"+q.queueLength());
System.out.println("是否空队列?%d(1:空 0:否) "+q.queueEmpty());
System.out.println("队列的元素依次为:");
q.queueTraverse();
System.out.println("队头元素是:%d\n"+q.getHead());
System.out.println("删除了队头元素%d\n"+q.deQueue());
System.out.println("新的队头元素是:%d\n"+q.getHead());
q.clearQueue();
System.out.println("清空队列后,q.front="+q.front.data+" q.rear="+q.rear.data+" q.front->next="+q.front.next.data);
//q.destroyQueue();
//System.out.println("销毁队列后,q.front=%u q.rear=%u\n"+q.front.data+"==========="+q.rear.data);
}
}