数据结构(十四)队列的链式存储结构(链队列)

  一、队列的链式存储结构:其实就是线性表的单链表,只不过它只能尾进头出而已,简称为链队列。

  二、将队头指针front指向链队列的头结点,队尾指针rear指向终端结点。空队列时,front和rear都指向头结点。

  三、在可以确定队列长度最大值的情况下,建议用循环队列,如果无法估计预估队列的长度,则用链队列。

  四、链队列的C语言代码实现:

#include "stdio.h"    
#include "stdlib.h"   
#include "io.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("
");
    return OK;
}

int main()
{
    int i;
    QElemType d;
    LinkQueue q;
    i=InitQueue(&q);
    if(i)
        printf("成功地构造了一个空队列!
");
    printf("是否空队列?%d(1:空 0:否)  ",QueueEmpty(q));
    printf("队列的长度为%d
",QueueLength(q));
    EnQueue(&q,-5);
    EnQueue(&q,5);
    EnQueue(&q,10);
    printf("插入3个元素(-5,5,10)后,队列的长度为%d
",QueueLength(q));
    printf("是否空队列?%d(1:空 0:否)  ",QueueEmpty(q));
    printf("队列的元素依次为:");
    QueueTraverse(q);
    i=GetHead(q,&d);
    if(i==OK)
     printf("队头元素是:%d
",d);
    DeQueue(&q,&d);
    printf("删除了队头元素%d
",d);
    i=GetHead(q,&d);
    if(i==OK)
        printf("新的队头元素是:%d
",d);
    ClearQueue(&q);
    printf("是否空队列?%d(1:空 0:否)
 ",QueueEmpty(q));
    DestroyQueue(&q);
    printf("是否空队列?%d(1:空 0:否)
  ",QueueEmpty(q));
    
    return 0;
}


输出为:
成功地构造了一个空队列!
是否空队列?1(1:空 0:否)  队列的长度为0
插入3个元素(-5,5,10)后,队列的长度为3
是否空队列?0(1:空 0:否)  队列的元素依次为:-5 5 10 
队头元素是:-5
删除了队头元素-5
新的队头元素是:5
是否空队列?1(1:空 0:否)
 是否空队列?1(1:空 0:否)
  

  五、链队列的Java语言代码实现:

  • 结点类:
package bigjun.iplab.linkQueue;

public class Node {
    
    public Integer data;   // 存放结点的数据元素的数据域(int类型不能设置null,而Integer类型可以)
    public Node next;      // 存放后继元素的引用
    
    // 可实现初始化一个空的结点
    public Node() {
        this(null, null);
    }
    
    // 可实现构造一个数据域值为指定参数值,而指针域为空的结点
    public Node(Integer data) {
        this(data, null);
    }
    
    // 可实现构造一个数据域和指针域值都为指定参数的结点
    public Node(Integer data, Node next) {
        this.data = data;
        this.next = next;
    }
    
}
  • 接口类:
package bigjun.iplab.linkQueue;

public interface LinkQueueINF {
    // 判断链队列是否为空
    public boolean isqueueEmpty();
    // 将一个已经存在的链队列置成空表
    public void queueClear();
    // 求链队列的长度
    public int queueLength();
    // 读取链队列的队列队头元素
    public int getHeadElem() throws Exception;
    // 在链队列的队尾插入元素e
    public void queueEnter(int e) throws Exception;
    // 删除链队列队头元素
    public void queueDel() throws Exception ;
    // 输出链队列中的所有元素
    public void queueTraverse() throws Exception;
}
  • 实现类:
package bigjun.iplab.linkQueue;

import bigjun.iplab.linkStack.Node;

public class LinkQueue implements LinkQueueINF{
    
    private Node front;     // 队头指针指向头结点
    private Node rear;      // 队尾指针指向终端结点
    
    public LinkQueue() {
        front = rear = null;
    }

    public boolean isqueueEmpty() {
        return front == rear;
    }

    public void queueClear() {
        front = rear = null;
    }

    public int queueLength() {
        int length = 0;
        Node p = front;
        while (p != null) {
            p = p.next;
            ++ length;
        }
        return length;
    }

    public int getHeadElem() throws Exception {
        if (front == null) 
            throw new Exception("顺序队列为空,无法获取队头元素");
        return front.data;
    }

    public void queueEnter(int e){
        Node p = new Node(e);
        if (front != null) {
            rear.next = p;
            rear = p;
        } else {
            front = rear = p;
        }
    }

    public void queueDel() throws Exception {
        if (front == null || front.next==rear) 
            throw new Exception("链队列为空,无法删除队头元素");
        front = front.next;
    }

    public void queueTraverse() {
        Node p = front;
        System.out.print("此时,链队列中的元素为: ");
        while (p != null) {
            System.out.print(p.data + " ");
            p = p.next;
        }
        System.out.println();
    }
    
    public static void main(String[] args) throws Exception {
        LinkQueue lQueue = new LinkQueue();
        lQueue.queueEnter(-5);
        lQueue.queueTraverse();
        System.out.println("此时,链队列的长度为: " + lQueue.queueLength());
        
        lQueue.queueEnter(5);
        lQueue.queueEnter(10);
        lQueue.queueTraverse();
        System.out.println("此时,链队列的长度为: " + lQueue.queueLength());
        System.out.println("此时,链队列的头结点元素为: " + lQueue.getHeadElem());
        
        lQueue.queueDel();
        lQueue.queueTraverse();
        System.out.println("此时,链队列的长度为: " + lQueue.queueLength());
        System.out.println("此时,链队列的头结点元素为: " + lQueue.getHeadElem());
        
        lQueue.queueClear();
        System.out.println("此时,链队列是否为空: " + lQueue.isqueueEmpty());
        
    }
    

}
  • 输出:
此时,链队列中的元素为: -5 
此时,链队列的长度为: 1
此时,链队列中的元素为: -5 5 10 
此时,链队列的长度为: 3
此时,链队列的头结点元素为: -5
此时,链队列中的元素为: 5 10 
此时,链队列的长度为: 2
此时,链队列的头结点元素为: 5
此时,链队列是否为空: true
原文地址:https://www.cnblogs.com/BigJunOba/p/9188051.html