您好,登錄后才能下訂單哦!
本篇內容介紹了“Java怎么使用跳轉結構實現隊列和棧”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
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(); } }
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("測試結束!"); }
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("測試結束!"); }
“Java怎么使用跳轉結構實現隊列和棧”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。