Java使用跳轉(zhuǎn)結(jié)構(gòu)實(shí)現(xiàn)隊列和棧流程詳解
導(dǎo)讀
在數(shù)據(jù)結(jié)構(gòu)當(dāng)中所有的數(shù)據(jù)結(jié)構(gòu)都是由 連續(xù)數(shù)據(jù)結(jié)構(gòu)或者跳轉(zhuǎn)數(shù)據(jù)結(jié)構(gòu) 單獨(dú)或者拼接做成。
連續(xù)結(jié)構(gòu)和跳轉(zhuǎn)結(jié)構(gòu)是數(shù)據(jù)結(jié)構(gòu)中常見的兩種基本數(shù)據(jù)結(jié)構(gòu),而我們本次的主角棧和隊列都 既可以使用使用跳轉(zhuǎn)結(jié)構(gòu)實(shí)現(xiàn)也可以使用連續(xù)結(jié)構(gòu)實(shí)現(xiàn)。
本文主要是介紹了如何通過跳轉(zhuǎn)結(jié)構(gòu)實(shí)現(xiàn)棧和隊列,在實(shí)現(xiàn)棧和隊列之后并使用 對數(shù)器 對寫出的棧和隊列進(jìn)行測試。
隊列
跳轉(zhuǎn)結(jié)構(gòu)結(jié)點(diǎn)
public static class Node<T> { public T value; public Node<T> next; public Node(T value) { this.value = value; } @Override public String toString() { ArrayList<T> nums = new ArrayList<>(); Node<T> node = this; while (node != null) { nums.add(node.value); node = node.next; } return nums.toString(); } }
實(shí)現(xiàn)隊列
public static class MyQueue<T> { private Node<T> head; private Node<T> tail; private int size; public MyQueue() { head = null; tail = null; size = 0; } // 插入一個元素 public void offer(T t) { Node<T> node = new Node<>(t); if (head == null) { head = node; } else { tail.next = node; } tail = node; size++; } // 彈出一個元素 public T poll() { T ans = null; if (head != null) { ans = head.value; head = head.next; size--; } if (head == null) { tail = null; } return ans; } // 查看隊首元素 public T peek() { T ans = null; if (head != null) { ans = head.value; } return ans; } //檢查 隊列是否為空 public Boolean isEmpty() { return size == 0; } // 查看隊列的長度 public int size() { return size; } }
測試隊列
public static void main(String[] args) { MyQueue<Integer> myQueue = new MyQueue<>(); Queue<Integer> test = new LinkedList<>(); int testTime = 5000000; int maxValue = 200000000; System.out.println("測試開始!"); for (int i = 0; i < testTime; i++) { if (myQueue.isEmpty() != test.isEmpty()) { System.out.println("Oops!"); } if (myQueue.size() != test.size()) { System.out.println("Oops!"); } double decide = Math.random(); if (decide < 0.33) { int num = (int) (Math.random() * maxValue); myQueue.offer(num); test.offer(num); } else if (decide < 0.66) { if (!myQueue.isEmpty()) { Integer num1 = myQueue.poll(); Integer num2 = test.poll(); if (!num1.equals(num2)) { System.out.println("Oops!"); } } } else { if (!myQueue.isEmpty()) { Integer num1 = myQueue.peek(); Integer num2 = test.peek(); if (!num1.equals(num2)) { System.out.println("Oops!"); } } } } if (myQueue.size() != test.size()) { System.out.println("Oops!"); } while (!myQueue.isEmpty()) { Integer num1 = myQueue.poll(); Integer num2 = test.poll(); if (!num1.equals(num2)) { System.out.println("Oops!"); } } System.out.println("測試結(jié)束!"); }
棧
實(shí)現(xiàn)棧
public static class MyStack<T> { private Node<T> head; private int size; public MyStack() { head = null; size = 0; } //檢查 棧是否為空 public Boolean isEmpty() { return size == 0; } // 查看棧的長度 public int size() { return size; } // 插入一個元素 public void push(T t) { Node<T> node = new Node<>(t); if (head != null) { node.next = head; } head = node; size++; } public T pop() { T ans = null; if (head != null) { ans = head.value; head = head.next; size--; } return ans; } // 查看棧頂元素 public T peek() { T ans = null; if (head != null) { ans = head.value; } return ans; } }
測試代碼
public static void main(String[] args) { MyStack<Integer> myStack = new MyStack<>(); Stack<Integer> test = new Stack<>(); int testTime = 5000000; int maxValue = 200000000; System.out.println("測試開始!"); for (int i = 0; i < testTime; i++) { if (myStack.isEmpty() != test.isEmpty()) { System.out.println("Oops!"); } if (myStack.size() != test.size()) { System.out.println("Oops!"); } double decide = Math.random(); if (decide < 0.33) { int num = (int) (Math.random() * maxValue); myStack.push(num); test.push(num); } else if (decide < 0.66) { if (!myStack.isEmpty()) { int num1 = myStack.pop(); int num2 = test.pop(); if (num1 != num2) { System.out.println("Oops!"); } } } else { if (!myStack.isEmpty()) { int num1 = myStack.peek(); int num2 = test.peek(); if (num1 != num2) { System.out.println("Oops!"); } } } } if (myStack.size() != test.size()) { System.out.println("Oops!"); } while (!myStack.isEmpty()) { int num1 = myStack.pop(); int num2 = test.pop(); if (num1 != num2) { System.out.println("Oops!"); } } System.out.println("測試結(jié)束!"); }
到此這篇關(guān)于Java使用跳轉(zhuǎn)結(jié)構(gòu)實(shí)現(xiàn)隊列和棧流程詳解的文章就介紹到這了,更多相關(guān)Java跳轉(zhuǎn)結(jié)構(gòu)實(shí)現(xiàn)隊列和棧內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java 數(shù)據(jù)結(jié)構(gòu)之棧與隊列
- java 數(shù)據(jù)結(jié)構(gòu)中棧和隊列的實(shí)例詳解
- Java深入了解數(shù)據(jù)結(jié)構(gòu)之棧與隊列的詳解
- Java 棧和隊列的相互轉(zhuǎn)換詳解
- Java棧和基礎(chǔ)隊列的實(shí)現(xiàn)詳解
- 一起來學(xué)習(xí)Java的棧和隊列
- Java?棧與隊列實(shí)戰(zhàn)真題訓(xùn)練
- Java 棧與隊列超詳細(xì)分析講解
- Java線性結(jié)構(gòu)中棧、隊列和串的基本概念和特點(diǎn)詳解
- Java常見的數(shù)據(jù)結(jié)構(gòu)之棧和隊列詳解
- Java 棧和隊列的交互實(shí)現(xiàn)
相關(guān)文章
基于@MapperScan和@ComponentScan的使用區(qū)別
這篇文章主要介紹了@MapperScan和@ComponentScan的使用區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09SpringBoot中整合MyBatis-Plus的方法示例
這篇文章主要介紹了SpringBoot中整合MyBatis-Plus的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Idea工具中創(chuàng)建 SpringBoot工程及入門詳解
這篇文章主要介紹了Idea工具中創(chuàng)建 SpringBoot工程及入門分析詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02springboot2.6.3讀取不到nacos上的配置文件問題
這篇文章主要介紹了springboot2.6.3讀取不到nacos上的配置文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07java中并發(fā)Queue種類與各自API特點(diǎn)以及使用場景說明
這篇文章主要介紹了java中并發(fā)Queue種類與各自API特點(diǎn)以及使用場景說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06