国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Java實現隊列的三種方法集合

瀏覽:22日期:2022-08-24 14:54:57

數組實現隊列

//數組實現隊列class queue{ int[] a = new int[5]; int i = 0; //入隊操作 public void in(int m) { a[i++] = m; }// 出隊列操作 取出最前面的值 通過循環(huán)遍歷把所有的數據向前一位 public int out() { int index = 0; int temp = a[0]; for(int j = 0;j < i;j++) { a[j] = a[j + 1]; } return temp; } }

ArrayList實現隊列

//集合實現隊列class queue{ List<Integer> list = new ArrayList<Integer>(); int index = 0; public void in(int n) { list.add(n); index++; } //出隊列操作 //出隊 public int out(){ if(!list.isEmpty()){ index--; return list.remove(0); } return -1; } }

兩個堆棧實現隊列

//兩個堆棧實現一個隊列class queue3 { Stack<Integer> stackA = new Stack<Integer>(); Stack<Integer> stackB = new Stack<Integer>(); //入隊 public void in(int n) { stackA.push(n); } //出隊 我們把A里面的元素遍歷拿出放入B中 再拿出B中的第一個元素 public int out() { //判斷b棧有沒有元素 有返回false 無返回真 if(stackB.isEmpty()) { while(!stackA.isEmpty()) { stackB.push(stackA.pop()); } } return stackB.pop(); }}

補充知識:java使用鏈表實現隊列

隊列使用Java進行鏈表實現,在網上找到了一張圖,很好,借鑒一下

Java實現隊列的三種方法集合

設置兩個結點node,front指向隊首元素,rear指向隊尾;

上代碼:

public class LinkedQueue { Node front;//隊頭指針,指向隊頭節(jié)點 Node rail;//隊尾指針,指向隊尾節(jié)點 int size = 0;//記錄隊列長度 //構造函數 public LinkedQueue() { front = rail = null; } public boolean isEmpty() { return size == 0 ? true : false; } //添加元素 public boolean addQueue(Object ele) { if (size == 0) { front = new Node(null, ele); rail = front; size++; return true; } Node s = new Node(null, ele); //這塊有個主意的地方,一旦rail設置了next屬性,因為front節(jié)點與rail節(jié)點指向了同一個node節(jié)點,持有同一個結點的一個引用,因此front節(jié)點next屬性也被填充 rail.setNext(s); rail = s; size++; return true; } /** * 刪除元素,出隊列 * @return */ public boolean deleteQueue() { if (isEmpty()) { System.out.println('當前隊列為空'); return false; } front = front.next; size--; return true; } public static void main(String[] args) { LinkedQueue queue = new LinkedQueue(); queue.addQueue(1); queue.addQueue(2); queue.addQueue(3); queue.deleteQueue(); } } /** * 首先鏈表底層是一個個結點 */class Node { Node next; Object element; public Node(Node next, Object element) { this.next = next; this.element = element; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } public Object getElement() { return element; } public void setElement(Object element) { this.element = element; } }

以上這篇Java實現隊列的三種方法集合就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 日本乱人伦片中文三区 | 一本大道香蕉久在线不卡视频 | 草久在线观看 | 久久久www免费人成看片 | 国产视频一区二区三区四区 | 亚洲欧洲一区二区 | 日韩一级影片 | 一级毛片韩国 | 国产精品视频免费播放 | 日本午夜视频 | 国产亚洲精品hd网站 | 欧美日韩综合网在线观看 | 国产精品成久久久久三级 | 国语精品视频在线观看不卡 | a级网站在线观看 | 天堂精品高清1区2区3区 | 亚洲欧美视屏 | 精品免费久久久久欧美亚一区 | 国产综合视频在线观看一区 | 久久国产精品一国产精品 | 乱系列中文字幕在线视频 | 91精品91 | 九九久久九九久久 | xoxoxoxo欧美性护士 | 国产成人高清亚洲一区91 | 99亚洲| 99久久精彩视频 | 亚州综合 | 欧美xxxx成人免费网站 | 欧美一级aⅴ毛片 | 亚洲综合91社区精品福利 | 国产日韩欧美 | 国产精品久草 | 免费在线精品视频 | 一级特一级特色生活片 | 九九九九热精品免费视频 | 国产4tube在线播放 | 正在播放国产一区 | 国产男女爽爽爽爽爽视频 | 免费看a级 | 午夜看毛片 |