大话数据结构九:队列的链式存储结构(链队列)

来源:互联网 发布:debian 没有yum 编辑:程序博客网 时间:2024/06/02 00:01

1. 链队列的特点:

链队列其实就是单链表,只不过它是先进先出的单链表,为了实现方便,程序中设置了队头(front),队尾(rear)两个指针。


2. Java使用链表实现队列: 

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //结点类,包含结点的数据和指向下一个节点的引用  
  2. public class Node<E> {  
  3.     private E data; // 数据域  
  4.     private Node<E> next; // 指针域保存着下一节点的引用  
  5.   
  6.     public Node() {  
  7.     }  
  8.   
  9.     public Node(E data) {  
  10.         this.data = data;  
  11.     }  
  12.   
  13.     public Node(E data, Node<E> next) {  
  14.         this.data = data;  
  15.         this.next = next;  
  16.     }  
  17.   
  18.     public E getData() {  
  19.         return data;  
  20.     }  
  21.   
  22.     public void setData(E data) {  
  23.         this.data = data;  
  24.     }  
  25.   
  26.     public Node<E> getNext() {  
  27.         return next;  
  28.     }  
  29.   
  30.     public void setNext(Node<E> next) {  
  31.         this.next = next;  
  32.     }  
  33. }  
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // Java实现链队列  
  2. public class LinkedQueue<T> {  
  3.     private Node<T> front; // 记住队头结点  
  4.     private Node<T> rear; // 记住队尾结点  
  5.     private int size; // 记住链表长度  
  6.   
  7.     public LinkedQueue() {  
  8.         front = rear = null;  
  9.         size = 0;  
  10.     }  
  11.   
  12.     // 将元素追加到队列尾部  
  13.     public boolean enqueue(T data) {  
  14.         Node<T> newNode = new Node<T>(data);  
  15.         if (isEmpty()) { // 判断队列是否为空  
  16.             front = rear = newNode;  
  17.         } else {  
  18.             rear.setNext(newNode); // 将新进来的结点设置为尾结点  
  19.             rear = newNode;  
  20.         }  
  21.         size++;  
  22.         System.out.println(data + "入队..");  
  23.         return true;  
  24.     }  
  25.   
  26.     // 队列头部的第一个元素出队  
  27.     public T dequeue() {  
  28.         T data = null;  
  29.         if (isEmpty()) {  
  30.             System.out.println("队列为空,无法出队..");  
  31.         } else {  
  32.             if (front.getNext() == null) { // 队列中只有一个结点  
  33.                 rear = null;  
  34.             }  
  35.             data = front.getData();  
  36.             front = front.getNext(); // 将原队头的下一个结点设置为队头  
  37.             System.out.println(data + "出队..");  
  38.             size--;  
  39.         }  
  40.         return data;  
  41.     }  
  42.   
  43.     // 获取链队列的长度  
  44.     public int size() {  
  45.         return size;  
  46.     }  
  47.   
  48.     // 判断链队列是否为空  
  49.     public boolean isEmpty() {  
  50.         return size == 0;  
  51.     }  
  52.       
  53.     // 清空链队列  
  54.     public void clear() {  
  55.         front = rear = null;  
  56.         size = 0;  
  57.     }  
  58.       
  59.     // 打印队列中的元素  
  60.     public void display() {  
  61.         if (!isEmpty()) {  
  62.             Node<T> nextNode = front;  
  63.             for (int i = 0; i < size(); i++) {  
  64.                 System.out.print(" " + nextNode.getData());  
  65.                 nextNode = nextNode.getNext();  
  66.             }  
  67.         }  
  68.     }  
  69.       
  70.     // 测试方法  
  71.     public static void main(String[] args) {  
  72.         LinkedQueue<String> queue = new LinkedQueue<String>();  
  73.         queue.enqueue("张三");  
  74.         queue.dequeue();  
  75.         queue.dequeue();  
  76.         queue.enqueue("李四");  
  77.         queue.enqueue("王五");  
  78.         queue.enqueue("赵六");  
  79.         queue.dequeue();  
  80.         queue.enqueue("田七");  
  81.         queue.dequeue();  
  82.         queue.enqueue("周八");  
  83.         System.out.println("队列中元素个数为: " + queue.size());  
  84.         System.out.print("打印队列中的元素: ");  
  85.         queue.display();  
  86.     }  
  87. }  

3. 链队列和循环队列比较:

1.) 时间上:循环队列事先申请好空间,使用期间不释放。链队列每次申请和释放结点存在一些时间开销,如果入队出队操作频繁,链队列性能稍差。

2.) 空间上:循环队列必须有一个固定长度,所以就有了存储元素个数和空间浪费的问题。链队列不存在这个问题,所以在空间上更为灵活。


4. 什么时候使用链队列:

1.) 在可以确定队列长度最大值的情况下,建议用循环队列。

2.) 如果无法预估队列的长度,则用链队列 。


0 0