1、隊列的特色前端
一、隊列是線性結構node
二、先進先出,先進入隊列的排在隊列前端,會比後進隊列的先出隊列。FIFO數組
2、經過數組來實現隊列函數
//本身實現數組隊列,隊列的特定就是先進先出 public class MyArrayQueue<E> { //用數組來保存 private Object[] queue; //隊列容量 private int capacity; //隊列中元素的個數 private int size; //隊列頭部元素對應的下標 private int head; //隊列的尾部的下一個位置下標 private int tail; public MyArrayQueue(int capacity){ this.capacity = capacity; this.queue = new Object[capacity]; } //將元素加入到隊列的隊尾,若是隊列空間不足,拋出異常 public boolean add(E e) throws Exception{ //先肯定空間是否足夠,已經滿了就拋出異常 if(size == capacity){ throw new Exception("queue full"); } //沒滿就加入到隊尾 queue[tail] = e; //計算新的隊尾 tail = (tail+1) % capacity; size ++; return true; } //返回並刪除隊列頭部的元素,若是隊列爲空,拋出異常 public E remove() throws NoSuchElementException{ //判斷是否爲空,爲空則報錯 if(size == 0){ throw new NoSuchElementException(); } E removed = elementData(head); //將頭部元素設置爲null queue[head] = null; //從新計算head的值 head = (head+1) % capacity; size -- ; return removed; } //將元素加入到隊尾,若是隊列已經滿了,返回false public boolean offer(E e){ //先肯定空間是否足夠,已經滿了就返回false if(size == capacity){ return false; } //沒滿就加入到隊尾 queue[tail] = e; //計算新的隊尾 tail = (tail+1) % capacity; size ++; return true; } //返回並刪除隊列頭部的元素,若是隊列爲空,返回null public E poll(){ //判斷是否爲空,爲空則返回null if(size == 0){ return null; } E removed = elementData(head); //將頭部元素設置爲null queue[head] = null; //從新計算head的值 head = (head+1) % capacity; size -- ; return removed; } //返回隊列頭部的元素,若是隊列爲空,拋出異常 public E element(){ //判斷是否爲空,爲空則報錯 if(size == 0){ throw new NoSuchElementException(); } E e = elementData(head); return e; } //返回隊列頭部的元素,若是隊列爲空,返回null public E peek(){ //判斷是否爲空,爲空則返回null if(size == 0){ return null; } E e = elementData(head); return e; } @SuppressWarnings("unchecked") private E elementData(int index) { return (E) queue[index]; } }
3、經過鏈表來實現隊列this
//使用鏈表來實現隊列 public class MyLinkedQueue<E> { //節點,保存元素信息,經過next指向下一個節點,造成單鏈表 private static class Node<E>{ E item; Node<E> next;//下一個節點 Node(E e, Node<E> next){ this.item = e; this.next = next; } } //容量 private int capacity; //元素個數 private int size; //頭節點 private Node<E> head; //尾節點 private Node<E> tail; //構造函數 public MyLinkedQueue(int capacity){ this.capacity = capacity; } //將元素加入到隊列的隊尾,若是隊列空間不足,拋出異常 public boolean add(E e) throws Exception{ //先肯定空間是否足夠,已經滿了就拋出異常 if(size == capacity){ throw new Exception("queue full"); } //建立一個新的節點,而後添加到隊列尾部 Node<E> node = new Node<E>(e,null); if(size == 0){//若是隊列爲空 head = tail = node; }else{//若是隊列中已經有節點了 tail.next = node; tail = node; } size ++; return true; } //返回並刪除隊列頭部的元素,若是隊列爲空,拋出異常 public E remove() throws NoSuchElementException{ //判斷是否爲空,爲空則報錯 if(size == 0){ throw new NoSuchElementException(); } E e = head.item; head.item = null; //方便GC //將head指向下一個節點 head = head.next; if(head == null){//刪除後隊列爲空,頭節點和尾節點都爲null tail = null; } size -- ; return e ; } //將元素加入到隊尾,若是隊列已經滿了,返回false public boolean offer(E e){ //先肯定空間是否足夠,已經滿了就返回false if(size == capacity){ return false; } //建立一個新的節點,而後添加到隊列尾部 Node<E> node = new Node<E>(e,null); if(size == 0){//若是隊列爲空 head = tail = node; }else{//若是隊列中已經有節點了 tail.next = node; tail = node; } size ++; return true; } //返回並刪除隊列頭部的元素,若是隊列爲空,返回null public E poll(){ //判斷是否爲空,爲空則返回null if(size == 0){ return null; } E e = head.item; head.item = null; //方便GC //將head指向下一個節點 head = head.next; if(head == null){//刪除後隊列爲空,頭節點和尾節點都爲null tail = null; } size -- ; return e ; } //返回隊列頭部的元素,若是隊列爲空,拋出異常 public E element(){ //判斷是否爲空,爲空則報錯 if(size == 0){ throw new NoSuchElementException(); } E e = head.item; return e; } //返回隊列頭部的元素,若是隊列爲空,返回null public E peek(){ //判斷是否爲空,爲空則返回null if(size == 0){ return null; } E e = head.item ; return e; } }