av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Java實(shí)現(xiàn)隊(duì)列的三種方法集合

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

數(shù)組實(shí)現(xiàn)隊(duì)列

//數(shù)組實(shí)現(xiàn)隊(duì)列class queue{ int[] a = new int[5]; int i = 0; //入隊(duì)操作 public void in(int m) { a[i++] = m; }// 出隊(duì)列操作 取出最前面的值 通過循環(huán)遍歷把所有的數(shù)據(jù)向前一位 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實(shí)現(xiàn)隊(duì)列

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

兩個(gè)堆棧實(shí)現(xiàn)隊(duì)列

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

補(bǔ)充知識(shí):java使用鏈表實(shí)現(xiàn)隊(duì)列

隊(duì)列使用Java進(jìn)行鏈表實(shí)現(xiàn),在網(wǎng)上找到了一張圖,很好,借鑒一下

Java實(shí)現(xiàn)隊(duì)列的三種方法集合

設(shè)置兩個(gè)結(jié)點(diǎn)node,front指向隊(duì)首元素,rear指向隊(duì)尾;

上代碼:

public class LinkedQueue { Node front;//隊(duì)頭指針,指向隊(duì)頭節(jié)點(diǎn) Node rail;//隊(duì)尾指針,指向隊(duì)尾節(jié)點(diǎn) int size = 0;//記錄隊(duì)列長度 //構(gòu)造函數(shù) 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); //這塊有個(gè)主意的地方,一旦rail設(shè)置了next屬性,因?yàn)閒ront節(jié)點(diǎn)與rail節(jié)點(diǎn)指向了同一個(gè)node節(jié)點(diǎn),持有同一個(gè)結(jié)點(diǎn)的一個(gè)引用,因此front節(jié)點(diǎn)next屬性也被填充 rail.setNext(s); rail = s; size++; return true; } /** * 刪除元素,出隊(duì)列 * @return */ public boolean deleteQueue() { if (isEmpty()) { System.out.println('當(dāng)前隊(duì)列為空'); 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(); } } /** * 首先鏈表底層是一個(gè)個(gè)結(jié)點(diǎn) */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實(shí)現(xiàn)隊(duì)列的三種方法集合就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 日韩视频中文字幕 | 亚洲视频精品在线 | 韩日一区| 亚洲精品1区 | 日本久久网 | 天堂资源视频 | 九九热精品视频 | 久久国产精品无码网站 | 精品一区二区久久久久久久网精 | 超级乱淫av片免费播放 | 久久成人精品视频 | 天天拍天天插 | 国产激情视频网站 | 国产视频导航 | 伦理二区 | 国产精品美女久久久久aⅴ国产馆 | 天天干天天爽 | 精品一二区 | 国产91丝袜在线18 | 99re在线观看 | 自拍偷拍中文字幕 | 日韩电影一区二区三区 | 久久精品视频在线播放 | 亚洲福利av | 亚洲精品毛片av | 亚洲福利在线观看 | 日本不卡免费新一二三区 | 精品国产乱码久久久久久影片 | 九色91视频 | 亚洲欧美国产毛片在线 | 免费黄色的视频 | 欧美一区二区三区在线观看 | 国产在线a | 毛片一区二区三区 | 欧美性猛交一区二区三区精品 | 日韩精品免费在线观看 | 99精品国产一区二区三区 | 99re视频在线免费观看 | 国产农村妇女精品一二区 | 久久人体视频 | 激情五月婷婷 |