【数据结构】之队列的java实现(二)

来源:互联网 发布:手机上做班服的软件 编辑:程序博客网 时间:2024/03/29 14:12

在上一篇博文中通过java实现了队列的连续存储,下面来讨论队列的链式存储,即链队列。

链队列的定义:

队列的链式存储结构简称为链队列。它是限制仅在表头删除和表尾插入的单链表。

链队列的数据存储形式:

 链队列基本运算的实现:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package study_02.datastructure.queue;  
  2.   
  3.   
  4. /** 
  5.  * 链队列 
  6.  * @author WWX 
  7.  */  
  8. public class LinkQueue<T> {  
  9.       
  10.     //链的数据结构  
  11.     private class Node{  
  12.         public  T data;  
  13.         public  Node next;  
  14.         //无参构造函数  
  15.         public Node(){}  
  16.           
  17.         public Node(T data,Node next){  
  18.             this.data=data;  
  19.             this.next=next;  
  20.         }  
  21.     }  
  22.     //队列头指针  
  23.     private Node front;  
  24.     //队列尾指针  
  25.     private Node rear;  
  26.     //队列长度  
  27.     private int size=0;  
  28.       
  29.     public LinkQueue(){  
  30.         Node n=new Node(null,null);  
  31.         n.next=null;  
  32.         front=rear=n;  
  33.     }  
  34.       
  35.     /** 
  36.      * 队列入队算法 
  37.      * @param data 
  38.      * @author WWX 
  39.      */  
  40.     public void enqueue(T data){  
  41.         //创建一个节点  
  42.         Node s=new Node(data,null);  
  43.         //将队尾指针指向新加入的节点,将s节点插入队尾  
  44.         rear.next=s;  
  45.         rear=s;  
  46.         size++;  
  47.     }  
  48.       
  49.     /** 
  50.      * 队列出队算法 
  51.      * @return 
  52.      * @author WWX 
  53.      */  
  54.     public  T dequeue(){  
  55.         if(rear==front){  
  56.             try {  
  57.                 throw new Exception("堆栈为空");  
  58.             } catch (Exception e) {  
  59.                 e.printStackTrace();  
  60.             }  
  61.             return null;  
  62.         }else{  
  63.             //暂存队头元素  
  64.             Node p=front.next;  
  65.             T x=p.data;  
  66.             //将队头元素所在节点摘链  
  67.             front.next=p.next;  
  68.             //判断出队列长度是否为1  
  69.             if(p.next==null)  
  70.                 rear=front;  
  71.             //删除节点  
  72.             p=null;  
  73.             size--;  
  74.             return  x;  
  75.         }  
  76.     }  
  77.       
  78.     /** 
  79.      * 队列长队 
  80.      * @return 
  81.      * @author WWX 
  82.      */  
  83.     public int size(){  
  84.         return size;  
  85.     }  
  86.       
  87.     /** 
  88.      * 判断队列是否为空 
  89.      * @return 
  90.      * @author WWX 
  91.      */  
  92.     public  boolean isEmpty(){  
  93.         return  size==0;  
  94.           
  95.     }  
  96.       
  97.       
  98.     public String toString() {  
  99.         if(isEmpty()){  
  100.             return "[]";  
  101.         }else{  
  102.             StringBuilder sb = new StringBuilder("[");  
  103.             for(Node current=front.next;current!=null;current=current.next){  
  104.                 sb.append(current.data.toString() + ", ");  
  105.             }  
  106.             int len = sb.length();  
  107.             return sb.delete(len - 2, len).append("]").toString();  
  108.         }  
  109.     }  
  110.       
  111.     //测试  
  112.     public static void main(String[] args) {  
  113.         LinkQueue<Integer> queue=new LinkQueue<Integer>();  
  114.         queue.enqueue(1);  
  115.         queue.enqueue(2);  
  116.         queue.enqueue(3);  
  117.         queue.enqueue(4);  
  118.         queue.enqueue(5);  
  119.         queue.enqueue(6);  
  120.         System.out.println(queue);  
  121.         System.out.println("出队:"+queue.dequeue());  
  122.         System.out.println("队列长度="+queue.size());  
  123.         System.out.println(queue);  
  124.         System.out.println("出队:"+queue.dequeue());  
  125.         System.out.println("队列长度="+queue.size());  
  126.         System.out.println(queue);  
  127.         System.out.println("出队:"+queue.dequeue());  
  128.         System.out.println("队列长度="+queue.size());  
  129.         System.out.println(queue);  
  130.     }  
  131. }  

 

输出结果:

[1, 2, 3, 4, 5, 6]
出队:1
队列长度=5
[2, 3, 4, 5, 6]
出队:2
队列长度=4
[3, 4, 5, 6]
出队:3
队列长度=3
[4, 5, 6]

0 0
原创粉丝点击