数据结构学习笔记(三):队列(queue)
目錄
1 隊列的結構形式與操作原則
2 兩種順序隊列及其代碼實現(Java)
2.1 簡單隊列
2.1.1 增刪查操作的實現
2.1.2 簡單隊列存在的弊端
2.2 循環隊列
3 鏈式隊列及其代碼實現(Java)
3.1 鏈式隊列的設計思路
3.2 增刪查操作的實現
1 隊列的結構形式與操作原則
隊列是在兩端分別進行增刪操作的線性表。對照棧的數據進出在同一端的特性,雖然隊列的兩端都是開放的,但是各自都只有一種功能,一個為數據的進口,另一個為數據的出口,像極了一條單行道。我們將隊列中新增數據的一端定義為隊尾(rear),將刪除數據的一端定義為隊頭(front)。
隊列的操作原則是先進先出(Fist In Fist Out, FIFO),即先進來的先出去,后進來的后出去。假如我們的“單行道”中依次開進6輛車,如下圖:
榮威最先開進來,也最先開出去,它不出去否則其他車也出不去;紅旗最后開進來,只能等所有前車都開出了才能出去。
隊列的增刪操作只能分別在隊頭和隊尾進行,中間不允許有任何操作,而且一旦有錯誤的數據進入,幾乎沒有修正的余地。雖然后進先出的棧也不允許在中間增刪元素,但是如果將錯誤數據壓棧,還能在下一次壓棧前及時彈棧取出錯誤數據。隊列不提供這種機會,車一旦開進單行道,是不允許再倒出來的,只能硬著頭皮排隊。
隊列根據存儲方式分為兩大類型——順序隊列和鏈式隊列,前者的實現基于數組,后者的實現基于鏈表。
?
2 兩種順序隊列及其代碼實現(Java)
順序隊列有兩種實現方式,一種是簡單的實現,一種是循環的實現。
2.1 簡單隊列
簡單順序隊列是相對與后面要實現的循環順序隊列來說的,用數組來模擬單向線性的數據增刪方式,除非隊列為空,隊尾永遠在隊頭之后。
2.1.1 增刪查操作的實現
為減少內存的浪費和增加使用的靈活性,我們采用帶有動態擴容和縮容功能的數組來實現。數組的第0個非空元素為隊頭元素,數組的最后一個非空元素為隊尾元素。定義一個頭指針front指向隊頭元素,尾指針rear指向隊尾元素后面的null。隊列為空時,front指針和rear指針是重合的;新增數據時,將數據添加到rear指針的位置,然后尾指針后移一位;刪除數據時,front指針位置的值改為null,然后front指針后移一位。只要隊列不為空,rear指針一定在front指針之后。
我們在簡單順序隊列中定義了一系列用于數據增刪查的公共方法,包括:append()新增元素,pop()刪除并取回元素,remove()刪除不取回元素,peek()獲取隊頭元素,last()獲取隊尾元素,getFromFront(int distance)和getFromRear(int distance)分別是根據與隊頭和隊尾的距離查找任意位置的元素,printAll()從隊頭到隊尾遍歷并打印所有的元素。
自定義了兩個異常類,分別為空隊列異常(EmptyQueueException)和距離超出限制異常(DistanceOutOfBoundsException),分別針對刪除方法和兩個帶參數的查找方法,代碼從略。
MySimpleArrayQueue.java:準備工作,創建一個簡單順序隊列的類
package com.notes.data_structure3;import com.notes.data_structure2.DistanceOutOfBoundsException;public class MySimpleArrayQueue<T> {// 容量縮放一次的單位為8,初始值為9,給尾指針留一個位置,因為尾指針在隊尾元素之后private T[] array = (T[]) new Object[9];int front = 0; // 頭指針int rear = 0; // 尾指針/*** 從 隊尾 添加元素* @param data*/public void append(T data) {if(size()==array.length-1){expend(); // 隊列元素數量達到數組容量減1,擴容數組,減1為的是給尾指針留一個空間}array[rear++] = data; // 在尾端(尾指針位置)新增數據,尾指針后移}/*** 刪除 并 取回 隊頭元素* @return* @throws EmptyQueueException*/public T pop() throws EmptyQueueException {if(isEmpty()) { // 如果隊列為空,報出異常throw new EmptyQueueException("隊列是空的");}T ele = peek(); // 取出隊頭元素array[front++] = null; // 將隊頭指針位置數據改為null,然后指針后移if(array.length-size()>8) {shrink(); // 數組的空余容量超過8,縮容數組}if(isEmpty()) { // 如果數組為空,隊頭指針和隊尾指針回到0索引位置front = 0;rear = 0;}return ele;}/*** 刪除 隊頭元素 且不取回* @throws EmptyQueueException*/public void remove() throws EmptyQueueException {if(isEmpty()) { // 如果隊列為空,報出異常throw new EmptyQueueException("隊列是空的");}array[front++] = null;if(array.length-size()>8) {shrink(); // 數組的空余容量超過8,縮容數組}if(isEmpty()) { // 如果數組為空,隊頭指針和隊尾指針回到0索引位置front = 0;rear = 0;}}/*** 獲取 隊頭 元素* @return* @throws EmptyQueueException*/public T peek() throws EmptyQueueException {if(isEmpty()) { // 如果隊列為空,報出異常throw new EmptyQueueException("隊列是空的");}return array[front]; // 隊頭元素在頭指針的位置}/*** 獲取 隊尾 元素* @return* @throws EmptyQueueException*/public T last() throws EmptyQueueException {if(isEmpty()) { // 如果隊列為空,報出異常throw new EmptyQueueException("隊列是空的");}return array[rear-1]; // 隊尾元素在尾指針的前一個位置}/*** 查找任意位置元素,按照與隊列 頭部 元素的距離* @param distance 與隊頭的距離,從隊頭開始數第幾個元素,0即隊頭本身* @return* @throws EmptyQueueException* @throws DistanceOutOfBoundsException*/public T getFromFront(int distance) throws EmptyQueueException, DistanceOutOfBoundsException {if(isEmpty()) { // 如果隊列為空,報出異常throw new EmptyQueueException("隊列是空的");}int temp = front; // 可移動的指針,起始位置為頭指針if(distance<size() & distance>=0) {for(int i=0;i<distance;i++) {temp++; // 指針 后移 distance次,到達指定位置}return array[temp]; // 取出指定位置的元素}else{ // distance為負值 或者 超過隊列容量的限制,報出異常throw new DistanceOutOfBoundsException("與隊列頭部的距離超出限制");}}/*** 查找任意位置元素,按照與隊列 尾部 元素的距離* @param distance 與隊尾的距離,從隊尾開始數第幾個元素,0即隊尾本身* @return* @throws EmptyQueueException* @throws DistanceOutOfBoundsException*/public T getFromRear(int distance) throws EmptyQueueException, DistanceOutOfBoundsException {if(isEmpty()) { // 如果隊列為空,報出異常throw new EmptyQueueException("隊列是空的");}int temp = rear-1; // 起始位置為隊尾元素所在位置,即rear指針前一個位置if(distance<size() & distance>=0) {for(int i=0;i<distance;i++) {temp--; // 指針 前移 distance次,到達指定位置}return array[temp]; // 取出指定位置的元素}else{ // distance為負值 或者 超過隊列容量的限制,報出異常throw new DistanceOutOfBoundsException("與隊列尾部的距離超出限制");}}/*** 遍歷打印 隊列 中的所有元素* @throws EmptyQueueException*/public void printAll() throws EmptyQueueException {if(isEmpty()) { // 如果隊列為空,報出異常throw new EmptyQueueException("隊列是空的");}for(int i=front;i<rear;i++) { // 從隊頭一直遍歷到隊尾System.out.println(array[i]);}}/*** 判斷隊列是否為空* @return 空-true,非空-false*/public Boolean isEmpty() {if(front==rear) { // 頭指針和尾指針相等,說明隊列為空return true;}return false;}/*** 計算隊列中的元素數量* @return int*/public int size() {return rear-front; // 尾指針與頭指針的差值即位隊列的元素數量}/*** 數組的動態擴容*/private void expend() {T[] newArray = (T[]) new Object[array.length + 8];System.arraycopy(array,0,newArray,0,size());array = newArray;}/*** 數組的動態縮容*/private void shrink() {int newSize = array.length - 8;T[] newArray = (T[]) new Object[newSize];System.arraycopy(array,front,newArray,0,newSize);array = newArray;front = 0;rear = array.length;} }SimpleArrayQueueDemo.java:模擬增刪查操作
package com.notes.data_structure3;import com.notes.data_structure2.DistanceOutOfBoundsException;public class SimpleArrayQueueDemo {public static void main(String[] args) throws EmptyQueueException, DistanceOutOfBoundsException {MySimpleArrayQueue queue = new MySimpleArrayQueue();/*** 模擬 增 的操作,從隊列 尾部 加入元素*/queue.append("榮威");queue.append("長安");queue.append("東風");queue.append("比亞迪");queue.append("吉利");queue.append("紅旗");// queue.printAll(); // 打印驗證/*** 模擬 刪 的操作,pop()和remove()兩種方法*/// 先進者先出,榮威 第一個進,第一個出queue.remove();// queue.printAll(); // 打印驗證// 將第二個進入的 長安 取出,并讓其重新排隊String subject = (String) queue.pop();queue.append(subject);// queue.printAll(); // 打印驗證/*** 模擬 查 的操作,peek(), last(), getFromTop(), getFromRear()* 目前隊列里的元素包括:東風 比亞迪 吉利 紅旗 長安*/String head_ele = (String) queue.peek(); // 獲取 隊頭 元素System.out.println(head_ele); // 東風String tail_ele = (String) queue.last(); // 獲取 隊尾 元素System.out.println(tail_ele); // 長安String ele1 = (String) queue.getFromFront(3); // 獲取中間元素,與 隊頭 的距離為3System.out.println(ele1); // 紅旗String ele2 = (String) queue.getFromRear(2); // 獲取中間元素,與 隊尾 的距離為2System.out.println(ele2); // 吉利} }2.1.2 簡單隊列存在的弊端
以上基于帶有縮放機制的數組實現的隊列在設計上還是有一些不合理的地方。由于隊列先進先出的原則,數據的增刪操作互不干擾,完全可以頻繁地交替進行,如果程序有多個線程,增刪還可以同時進行,就像現實中的單行道一樣,每時每刻都有車駛入,有車駛出。
這樣一來,簡單實現方式就存在一個問題:對于隊列底層的數組,刪除操作會造成值為null的空閑空間。與此同時,由于新增操作只能在末端進行,rear指針達到數組末端后,為了加入更多數據不得不向后開辟新的空間(擴容)。在數組觸發縮容條件之前,已經空出來的位置不能被使用。縮容機制正是為了減少這種資源浪費,然而如果我們把縮容條件設得太緊,比如一次只縮放1個單位的容量,很容易引發過于頻繁的縮放操作,從而損耗程序的性能。
在實際應用中,新增操作整體上是快于刪除操作的,新增的過程很簡單,就是把數據存進去,放進來的數據往往在等待某種處理。想象一下,如果我們的“單行道”建在一個加油站里,車輛排著隊等待加油,一輛車駛出單行道進入加油位,后車必須等待前車加完油才能駛出,在這個過程中車輛從道路末端駛入的速度遠遠大于加油的速度,那么我們就要不斷“擴建”加油站的單行道,這是不現實的,也是不必要的。
我們給數組增加擴容機制,目的是打破數組的申明必須指定長度所帶來的限制。其實這種限制對我們的“加油站”來說是一個很好的限流措施,加油站就那么大,單行道上只能放那么多車。采用這種思路就不能采用上面的單向線性的數據增刪方式,因為不設定擴容機制的數組會引發假溢出現象。比如上面的代碼所模擬的刪除與再新增操作,依次取出“榮威”和“長安”后在數組中留下兩個值為null的空閑位置,“長安”如果想重新到隊尾排隊,rear指針將無所適從:
所謂假溢出,就是說對于隊列的底層實現數組來說已經溢出了,rear指針失去了指向;但是對于隊列本身來說,并沒有發生溢出,因為元素的刪除使得隊列有了兩個空余的位置,只是不能使用。
如何讓刪除時留下的空閑位置在新增時得到再次使用,循環隊列是一個很好的解決方案。
?
2.2 循環隊列
循環隊列采用單向循環的方式組織隊列中的數據,讓每一個位置都能循環使用。和簡單隊列最直觀的區別是,循環隊列的rear指針既能在front指針之后,也能在其之前。
設計循環隊列時,無需為rear指針專門留出一個null位置,當數據新增到數組的末端時,如果數組被存滿,rear指針不動,同時拋出異常,以達到“限流”的目的;如果數組不滿,就意味著數組頭部有空位,rear指針移到0索引的位置,這就形成了循環。
代碼實現時,取消了數組的擴容和縮容方法,在構造方法中設定隊列的最大容量。由于數據的增刪操作是在隊列中循環進行的,因此無法通過尾指針和頭指針的差值獲得隊列中的元素數量,所以定義了一個count屬性在數據增刪時記錄隊列中的元素個數。front指針和rear指針重合時數據為空,由于有了count屬性,判斷隊列為空和隊列已滿的方法可以通過count實現。
定義的一系列增刪查的公共方法中,由于循環隊列無法通過與隊頭或隊尾的相對位置查找元素,因此取消了這組方法。隊列元素的遍歷仍然是從隊頭遍歷到隊尾,不論front指針對應的下標是否為0。
MyRingArrayQueue.java:準備工作,創建一個循環順序隊列的類
package com.notes.data_structure3;public class MyRingArrayQueue<T> {private int size; // 隊列的容量private T[] array; // 定義一個數組private int front = 0; // 定義一個頭指針private int rear = 0; // 定義一個尾指針private int count = 0; // 記錄元素個數/*** 構造方法,在此設定 隊列 的容量* 為了給rear指針留位置,數組比隊列要多一位* @param size*/public MyRingArrayQueue(int size) {this.size = size;this.array = (T[]) new Object[size+1];}/*** 添加元素,rear指針后移一位* @param data* @throws FullQueueException*/public void append(T data) throws FullQueueException {if(isFull()) { // 如果隊列已滿,拋出異常throw new FullQueueException("隊列已經滿了");}if(rear!=size) { // 如果沒有rear指針沒有到數組最后array[rear++] = data; //在尾端(尾指針位置)新增數據,尾指針后移}else {array[rear] = data; //在尾端(尾指針位置)新增數據rear = 0; // rear指針回到0索引位置,循環使用空閑位置}count++; // 隊列元素數量增加一個}/*** 刪除 并 取回 元素* @return* @throws EmptyQueueException*/public T pop() throws EmptyQueueException {if(isEmpty()) { // 如果隊列為空,拋出異常throw new EmptyQueueException("隊列是空的");}T ele = peek(); // 取出隊頭元素array[front++] = null; // 將隊頭指針位置數據改為null,然后指針后移count--; // 隊列元素數量減少一個return ele;}/*** 刪除元素且不取回* @throws EmptyQueueException*/public void remove() throws EmptyQueueException {if(isEmpty()) { // 如果隊列為空,拋出異常throw new EmptyQueueException("隊列是空的");}array[front++] = null; // 將隊頭指針位置數據改為null,然后指針后移count--; // 隊列元素數量減少一個}/*** 獲取 隊頭 元素* @return* @throws EmptyQueueException*/public T peek() throws EmptyQueueException {if(isEmpty()) { // 如果隊列為空,拋出異常throw new EmptyQueueException("隊列是空的");}return array[front]; // 取出隊頭元素}/*** 獲取 隊尾 元素* @return* @throws EmptyQueueException*/public T last() throws EmptyQueueException {if(isEmpty()) { // 如果隊列為空,拋出異常throw new EmptyQueueException("隊列是空的");}return array[rear-1]; // 取出隊尾元素}/*** 遍歷打印 隊列 中的所有元素* @throws EmptyQueueException*/public void printAll() throws EmptyQueueException {if(isEmpty()) { // 如果隊列為空,拋出異常throw new EmptyQueueException("隊列是空的");}for(int i=0;i<count;i++) { // 遍歷次數為隊列中的元素個數if(front<=size) { // 先從 隊頭 遍歷到 數組末端System.out.println(array[front++]);}else { // 如果還有遍歷次數,再從 數組頭部 開始遍歷int temp = 0;System.out.println(array[temp++]);}}}/*** 判斷隊列是否為空* @return*/public Boolean isEmpty() {if(count==0) {return true;}return false;}/*** 判斷隊列是否已滿* @return*/public Boolean isFull() {if(count==size) {return true;}return false;}/*** 返回當前隊列的元素數量* @return*/public int getCount() {return count;} }RingArrayQueueDemo.java:模擬增刪查操作
package com.notes.data_structure3;public class RingArrayQueueDemo {public static void main(String[] args) throws FullQueueException, EmptyQueueException {/*** 設定 隊列 容量,“加油站”最多駛入 6 輛車*/MyRingArrayQueue queue = new MyRingArrayQueue(6);/*** 模擬 增 的操作,從隊列 尾部 加入元素*/queue.append("榮威");queue.append("長安");queue.append("東風");queue.append("比亞迪");queue.append("吉利");queue.append("紅旗");// queue.printAll(); // 打印驗證/*** 模擬 刪 的操作,pop()和remove()兩種方法*/// 先進者先出,榮威 第一個進,第一個出queue.remove();// queue.printAll(); // 打印驗證// 將第二個進入的 長安 取出,并讓其重新排隊String subject = (String) queue.pop();queue.append(subject);// queue.printAll(); // 打印驗證// 再新增一個 奇瑞queue.append("奇瑞");// queue.printAll(); // 打印驗證/*** 模擬 查 的操作,peek(), last()* 目前隊列里的元素包括:東風 比亞迪 吉利 紅旗 長安 奇瑞*/String head_ele = (String) queue.peek(); // 獲取 隊頭 元素System.out.println(head_ele); // 東風String tail_ele = (String) queue.last(); // 獲取 隊尾 元素System.out.println(tail_ele); // 奇瑞} }?
3 鏈式隊列及其代碼實現(Java)
3.1 鏈式隊列的設計思路
鏈式隊列顧名思義就是基于鏈表實現的隊列,用Java實現鏈式隊列的方法和實現單向鏈表的方法是非常相似的,參見單向鏈表的筆記,鏈接在下面:
https://blog.csdn.net/weixin_45370422/article/details/116573863
之所以能夠用實現單向鏈表的方法實現鏈式隊列,源于我們對鏈式隊列的設計思路。在我們的設想中,隊列的rear指針指向最新進入隊列的元素,front指針指向不存數據的頭結點,頭結點指向隊頭元素。設計頭結點的目的是:當隊列為空時,指針仍有所指向,不至于成為“野指針”。
鏈式隊列和順序隊列一樣,當隊列為空時,front指針和rear指針的指向相同;和順序隊列不同的是,順序隊列給rear指針留了一個空位,鏈式隊列與之相反,給front指針留了一個空位。
回顧用Java模擬單向鏈表的過程,我們定義了一個頭結點(headNode)。在鏈式隊列的設想中,需要一個外部指針front指向這個頭結點。然而,指針其實也是一個結點對象,那么為了簡化代碼,完全可以讓front指針自己充當頭結點,front.next指向隊頭結點,即讓頭指針的指針指向隊頭。同樣地,單向鏈表中還定義了一個當前結點(currentNode),而當且結點正相當于鏈式隊列中rear指針要指向的最新進入的元素,那么為了簡化代碼,也完全可以讓rear指針自己充當隊尾結點,rear.next指向null,即隊尾(鏈尾)的指針是空指針,新增結點時,rear指針后移一位。
這樣一來,我們就可以用實現單向鏈表的方法來實現鏈式隊列,增刪操作分別通過調節front的指針(next)和rear的指針(next)來實現。基于隊列不能在中間增刪元素的原則,我們取消了單向鏈表中定義的在中間插入和刪除元素的方法。
3.2 增刪查操作的實現
MyLinkedQueue.java:準備工作,創建一個鏈式隊列的類
package com.notes.data_structure3;import com.notes.data_structure2.DistanceOutOfBoundsException;public class MyLinkedQueue<T> {private Node front = new Node(null); // 隊頭指針private Node rear = front; // 隊尾指針private int count; // 用于統計隊列中的元素數量// 定義一個結點類public class Node {// 結點的兩個要素:數據和指針private T data;private Node next;// 構造方法,初始化data屬性public Node(T data) {this.data = data;}@Overridepublic String toString() { // 可供printAll()方法調用return "Node{" +"data=" + data +'}';}}/*** 向 隊尾 增加元素* @param data*/public void append(T data) {Node node = new Node(data);if(isEmpty()) { // 如果隊列為空,front指針充當頭結點front.next = node; // front的指針指向隊頭元素}rear.next = node; // rear的指針指向新結點rear = node; // 新結點為rear結點,正式加入隊列,rear實現后移count++; // 隊列元素數量增加一個}/*** 從 隊頭 刪除元素 并取回* @return*/public T pop() throws EmptyQueueException {if(isEmpty()) { // 如果隊列為空,拋出異常throw new EmptyQueueException("隊列是空的");}T ele = peek(); // 取出 隊頭 元素front.next = front.next.next; // front的指針指向隊頭元素的下一個元素count--; // 隊列元素數量減少一個return ele;}/*** 從 隊頭 刪除元素 不取回*/public void remove() throws EmptyQueueException {if(isEmpty()) { // 如果隊列為空,拋出異常throw new EmptyQueueException("隊列是空的");}front.next = front.next.next; // front的指針指向隊頭元素的下一個元素count--; // 隊列元素數量減少一個}/*** 獲取 隊頭 元素* @return* @throws EmptyQueueException*/public T peek() throws EmptyQueueException {if(isEmpty()) { // 如果隊列為空,拋出異常throw new EmptyQueueException("隊列是空的");}return (T) front.next.data; // 返回隊頭結點的數據值}/*** 獲取 隊尾 元素* @return* @throws EmptyQueueException*/public T last() throws EmptyQueueException {if(isEmpty()) { // 如果隊列為空,拋出異常throw new EmptyQueueException("隊列是空的");}return rear.data; // 返回隊尾結點的數據值}/*** 查找任意位置元素,按照與隊列 頭部 元素的距離* @param distance 與隊頭的距離,從隊頭開始數第幾個元素,0即隊頭本身* @return* @throws EmptyQueueException* @throws DistanceOutOfBoundsException*/public T getFromFront(int distance) throws EmptyQueueException, DistanceOutOfBoundsException {if(isEmpty()) { // 如果隊列為空,拋出異常throw new EmptyQueueException("隊列是空的");}if(distance<count & distance>=0){Node temp = front; // 可移動的指針,起始位置為頭結點for(int i=0;i<distance+1;i++) {temp = temp.next; // 指針 后移 distance+1 次,到達指定位置}return (T) temp.data; // 取出指定位置的元素}else { // distance為負值 或者 超過隊列容量的限制,報出異常throw new DistanceOutOfBoundsException("與隊頭的距離超出限制");}}/*** 查找任意位置元素,按照與隊列 尾部 元素的距離* @param distance 與隊尾的距離,從隊尾開始數第幾個元素,0即隊尾本身* @throws EmptyQueueException* @throws DistanceOutOfBoundsException*/public T getFromRear(int distance) throws EmptyQueueException, DistanceOutOfBoundsException {if(isEmpty()) { // 如果隊列為空,拋出異常throw new EmptyQueueException("隊列是空的");} // 根據與隊尾的距離計算出與隊頭的距離,再調用getFromFront()方法return getFromFront(count-1-distance);}/*** 打印全部元素*/public void printAll() {Node temp = front; // 可移動的指針,起始位置為頭結點while (temp.next!=null) {temp = temp.next; // 不斷后移,直到nullSystem.out.println(temp);}}/*** 判斷隊列是否為空* @return*/public Boolean isEmpty() {if(rear==front) { // 如果隊頭和隊尾指向相同,隊列為空return true;}return false;}/*** 隊列當前的元素個數* @return*/public int size() {return count;} }LinkedQueueDemo.java:模擬增刪查操作
package com.notes.data_structure3;import com.notes.data_structure2.DistanceOutOfBoundsException;public class LinkedQueueDemo {public static void main(String[] args) throws EmptyQueueException, DistanceOutOfBoundsException {MyLinkedQueue queue = new MyLinkedQueue();/*** 模擬 增 的操作,從 隊頭 加入元素,即 “先進”*/queue.append("榮威");queue.append("長安");queue.append("東風");queue.append("比亞迪");queue.append("吉利");queue.append("紅旗");// queue.printAll(); // 打印驗證/*** 模擬 刪 的操作,pop()和remove()兩種方法*/// 先進者先出,榮威 第一個進,第一個出queue.remove();// queue.printAll(); // 打印驗證// 將第二個進入的 長安 取出,并讓其重新排隊String subject = (String) queue.pop();queue.append(subject);// queue.printAll(); // 打印驗證/*** 模擬 查 的操作,peek(), last(), getFromTop(), getFromRear()* 目前隊列里的元素包括:東風 比亞迪 吉利 紅旗 長安*/String head_ele = (String) queue.peek(); // 獲取 隊頭 元素System.out.println(head_ele); // 東風String tail_ele = (String) queue.last(); // 獲取 隊尾 元素System.out.println(tail_ele); // 長安String ele1 = (String) queue.getFromFront(3); // 獲取中間元素,與 隊頭 的距離為3System.out.println(ele1); // 紅旗String ele2 = (String) queue.getFromRear(2); // 獲取中間元素,與 隊尾 的距離為2System.out.println(ele2); // 吉利} }?
總結
以上是生活随笔為你收集整理的数据结构学习笔记(三):队列(queue)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python os和os.path的基础
- 下一篇: python enumeration_如