久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

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

瀏覽:40日期: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ì)列操作 取出最前面的值 通過(guò)循環(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棧有沒(méi)有元素 有返回false 無(wú)返回真 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ì)列長(zhǎng)度 //構(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)文章:
主站蜘蛛池模板: 兴文县| 太仆寺旗| 收藏| 美姑县| 慈溪市| 宁波市| 舟山市| 三明市| 定州市| 张家港市| 镇康县| 北海市| 盱眙县| 顺昌县| 高尔夫| 西乌珠穆沁旗| 浠水县| 东乡族自治县| 山阳县| 睢宁县| 无棣县| 同德县| 白沙| 大同市| 苍梧县| 连平县| 谢通门县| 太仓市| 太谷县| 久治县| 正宁县| 西和县| 阳山县| 太湖县| 南平市| 大田县| 呈贡县| 扶绥县| 济南市| 玉田县| 类乌齐县|