|
| 1 | +package com.zejian.structures.Queue; |
| 2 | + |
| 3 | +import com.zejian.structures.LinkedList.singleLinked.Node; |
| 4 | + |
| 5 | +import java.io.Serializable; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +/** |
| 9 | + * Created by zejian on 2016/11/28. |
| 10 | + * Blog : http://blog.csdn.net/javazejian/article/details/53375004 [原文地址,请尊重原创] |
| 11 | + * 链式队列的实现 |
| 12 | + */ |
| 13 | +public class LinkedQueue<T> implements Queue<T> ,Serializable{ |
| 14 | + private static final long serialVersionUID = 1406881264853111039L; |
| 15 | + /** |
| 16 | + * 指向队头和队尾的结点 |
| 17 | + * front==null&&rear==null时,队列为空 |
| 18 | + */ |
| 19 | + private Node<T> front,rear; |
| 20 | + |
| 21 | + private int size; |
| 22 | + /** |
| 23 | + * 用于控制最大容量,默认128,offer方法使用 |
| 24 | + */ |
| 25 | + private int maxSize=128; |
| 26 | + |
| 27 | + public LinkedQueue(){ |
| 28 | + //初始化队列 |
| 29 | + this.front=this.rear=null; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public int size() { |
| 34 | + return size; |
| 35 | + } |
| 36 | + |
| 37 | + public void setMaxSize(int maxSize){ |
| 38 | + this.maxSize=maxSize; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public boolean isEmpty() { |
| 43 | + return front==null&&rear==null; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * data 入队,添加成功返回true,否则返回false,可扩容 |
| 48 | + * @param data |
| 49 | + * @return |
| 50 | + */ |
| 51 | + @Override |
| 52 | + public boolean add(T data) { |
| 53 | + Node<T> q=new Node<>(data,null); |
| 54 | + if (this.front==null) {//空队列插入 |
| 55 | + this.front = q; |
| 56 | + } else {//非空队列,尾部插入 |
| 57 | + this.rear.next=q; |
| 58 | + } |
| 59 | + this.rear=q; |
| 60 | + size++; |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * offer 方法可插入一个元素,这与add 方法不同, |
| 66 | + * 该方法只能通过抛出未经检查的异常使添加元素失败。 |
| 67 | + * 而不是出现异常的情况,例如在容量固定(有界)的队列中 |
| 68 | + * NullPointerException:data==null时抛出 |
| 69 | + * IllegalArgumentException:队满,使用该方法可以使Queue的容量固定 |
| 70 | + * @param data |
| 71 | + * @return |
| 72 | + */ |
| 73 | + @Override |
| 74 | + public boolean offer(T data) { |
| 75 | + if (data==null) |
| 76 | + throw new NullPointerException("The data can\'t be null"); |
| 77 | + if (size>=maxSize) |
| 78 | + throw new IllegalArgumentException("The capacity of LinkedQueue has reached its maxSize:128"); |
| 79 | + |
| 80 | + Node<T> q=new Node<>(data,null); |
| 81 | + if (this.front==null) {//空队列插入 |
| 82 | + this.front = q; |
| 83 | + } else {//非空队列,尾部插入 |
| 84 | + this.rear.next=q; |
| 85 | + } |
| 86 | + this.rear=q; |
| 87 | + size++; |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * 返回队头元素,不执行删除操作,若队列为空,返回null |
| 93 | + * @return |
| 94 | + */ |
| 95 | + @Override |
| 96 | + public T peek() { |
| 97 | + return this.isEmpty()? null:this.front.data; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * 返回队头元素,不执行删除操作,若队列为空,抛出异常:NoSuchElementException |
| 102 | + * @return |
| 103 | + */ |
| 104 | + @Override |
| 105 | + public T element() { |
| 106 | + if(isEmpty()){ |
| 107 | + throw new NoSuchElementException("The LinkedQueue is empty"); |
| 108 | + } |
| 109 | + return this.front.data; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * 出队,执行删除操作,返回队头元素,若队列为空,返回null |
| 114 | + * @return |
| 115 | + */ |
| 116 | + @Override |
| 117 | + public T poll() { |
| 118 | + if (this.isEmpty()) |
| 119 | + return null; |
| 120 | + T x=this.front.data; |
| 121 | + this.front=this.front.next; |
| 122 | + if (this.front==null) |
| 123 | + this.rear=null; |
| 124 | + size--; |
| 125 | + return x; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * 出队,执行删除操作,若队列为空,抛出异常:NoSuchElementException |
| 130 | + * @return |
| 131 | + */ |
| 132 | + @Override |
| 133 | + public T remove() { |
| 134 | + if (isEmpty()){ |
| 135 | + throw new NoSuchElementException("The LinkedQueue is empty"); |
| 136 | + } |
| 137 | + T x=this.front.data; |
| 138 | + this.front=this.front.next; |
| 139 | + if (this.front==null) |
| 140 | + this.rear=null; |
| 141 | + size--; |
| 142 | + return x; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void clearQueue() { |
| 147 | + this.front= this.rear=null; |
| 148 | + size=0; |
| 149 | + } |
| 150 | +} |
0 commit comments