栈、队列 java代码实现
生活随笔
收集整理的這篇文章主要介紹了
栈、队列 java代码实现
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 普通隊(duì)列
- 數(shù)組實(shí)現(xiàn)
- java代碼實(shí)現(xiàn)
- 單元測(cè)試
- 控制臺(tái)打印
- 鏈表實(shí)現(xiàn)
- java代碼實(shí)現(xiàn)
- 單元測(cè)試
- 控制臺(tái)打印
- LinkedList隊(duì)列使用
- 優(yōu)先隊(duì)列:PriorityQueue使用
- 棧
- 數(shù)組實(shí)現(xiàn)
- java代碼實(shí)現(xiàn)
- 單元測(cè)試
- 控制臺(tái)打印
- 鏈表實(shí)現(xiàn)
- java代碼實(shí)現(xiàn)
- 單元測(cè)試
- 控制臺(tái)打印
普通隊(duì)列
概念:先入先出
數(shù)組實(shí)現(xiàn)
java代碼實(shí)現(xiàn)
package csdn.dreamzuora.queue;/*** @author weijie* @date 2020/10/15 19:54* Description: 先入先出*/ public class ArrayQueue {int[] queue;/*** 隊(duì)頭*/int head = 0;/*** 隊(duì)尾*/int tail = 0;/*** 初始化* @param size*/public ArrayQueue(int size) {this.queue = new int[size];}/*** 入棧* @param data*/public boolean enqueue(int data){if (tail >= queue.length){return false;}queue[tail++] = data;return true;}/*** 出棧* @return*/public int dequeue(){return queue[head++];}public boolean isEmpty(){/*** 隊(duì)頭等于隊(duì)尾此時(shí)表示隊(duì)列為空*/if (head == tail){return true;}return false;}}單元測(cè)試
package csdn.dreamzuora.queue;import org.junit.Test;import static org.junit.Assert.*;/*** Title:* Description:** @version 1.0* @author: weijie* @date: 2020/10/16 10:25*/ public class ArrayQueueTest {ArrayQueue queue = new ArrayQueue(10);@Testpublic void enqueue() {System.out.print("入隊(duì)列:");for (int i = 0; i < 10; i++){System.out.print("->" + i);queue.enqueue(i);}System.out.println();System.out.print("出隊(duì)列:");while (!queue.isEmpty()){System.out.print("->" + queue.dequeue());}} }控制臺(tái)打印
鏈表實(shí)現(xiàn)
java代碼實(shí)現(xiàn)
package csdn.dreamzuora.queue;/*** Title: Node* Description:** @version 1.0* @author: weijie* @date: 2020/10/16 9:54*/ public class Node {int data;Node next;public Node(int data) {this.data = data;} } package csdn.dreamzuora.queue;/*** @author weijie* @date 2020/10/15 19:56* Description:*/ public class LinkedQueue {Node head;Node tail;/*** 入棧* @return*/public void enqueue(int data){Node appendNode = new Node(data);if (tail == null){tail = appendNode;head = appendNode;}else {tail.next = appendNode;tail = appendNode;}}/*** 出棧*/public int dequeue(){int data = head.data;head = head.next;return data;}public boolean isEmpty(){if (head == null){return true;}return false;} }單元測(cè)試
package csdn.dreamzuora.queue;import org.junit.Test;import java.util.LinkedList;import static org.junit.Assert.*;/*** Title:* Description:** @version 1.0* @author: weijie* @date: 2020/10/16 10:25*/ public class LinkedQueueTest {LinkedQueue queue = new LinkedQueue();@Testpublic void enqueue() {System.out.print("入隊(duì)列:");for (int i = 0; i < 10; i++){System.out.print("->" + i);queue.enqueue(i);}System.out.println();System.out.print("出隊(duì)列:");while (!queue.isEmpty()){System.out.print("->" + queue.dequeue());}} }控制臺(tái)打印
LinkedList隊(duì)列使用
package csdn.dreamzuora.queue;import org.junit.Test;import java.util.LinkedList;/*** Title: 鏈表當(dāng)作隊(duì)列使用* Description:** @version 1.0* @author: weijie* @date: 2020/10/19 16:52*/ public class LinkedListTest {@Testpublic void example(){LinkedList<Integer> queue = new LinkedList<>();/*** 入隊(duì)*/queue.offer(1);queue.offer(2);queue.offer(3);/*** 出隊(duì)*/while (!queue.isEmpty()){System.out.println(queue.poll());}}}優(yōu)先隊(duì)列:PriorityQueue使用
package csdn.dreamzuora.queue;import org.junit.Test;import java.util.Comparator; import java.util.PriorityQueue;/*** Title: 優(yōu)先隊(duì)列* Description:** @version 1.0* @author: weijie* @date: 2020/10/23 15:43*/ public class PriorityQueueTest {@Testpublic void example(){PriorityQueue<Integer> queue = new PriorityQueue<>(10);for (int i = 10; i > 0; i--){queue.add(i);}System.out.println(queue);System.out.println(queue.poll());}@Testpublic void compare(){PriorityQueue<Student> priorityQueue = new PriorityQueue<>(new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {return Integer.compare(o2.getAge(), o1.getAge());}});for (int i = 0; i < 10; i++){Student student = new Student(i, "name-" + i);priorityQueue.add(student);}System.out.println(priorityQueue.peek());System.out.println("---------------------");for (int i = 20; i < 25; i++){priorityQueue.add(new Student(i, "name-" + i));}while (!priorityQueue.isEmpty()){System.out.println(priorityQueue.poll());}}class Student{Integer age;String name;public Student() {}public Student(Integer age, String name) {this.age = age;this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Student{" +"age=" + age +", name='" + name + '\'' +'}';}} }棧
概念:先入后出
數(shù)組實(shí)現(xiàn)
java代碼實(shí)現(xiàn)
package csdn.dreamzuora.stack;/*** @author weijie* @date 2020/10/15 16:02* Description: 先入后出*/ public class ArrayStack {int[] stack;int i = 0;/*** 初始化* @param size*/public ArrayStack(int size) {stack = new int[size];}/*** 入棧* @param data*/public boolean push(int data){if (i >= stack.length){return false;}stack[i++] = data;return true;}public boolean isEmpty(){if(i <= 0){return true;}return false;}/*** 出棧* @return*/public int pop(){return stack[--i];}}單元測(cè)試
package csdn.dreamzuora.stack;import org.junit.Test;import static org.junit.Assert.*;/*** @author weijie* @date 2020/10/15 16:09* Description:*/ public class ArrayStackTest {ArrayStack stack = new ArrayStack(10);@Testpublic void print() {System.out.print("入棧:");for (int i = 0; i < 10; i++){stack.push(i);System.out.print("->" + i);}System.out.println();System.out.print("出棧:");while (!stack.isEmpty()){System.out.print("->" + stack.pop());}} }控制臺(tái)打印
鏈表實(shí)現(xiàn)
java代碼實(shí)現(xiàn)
package csdn.dreamzuora.stack;/*** @author weijie* @date 2020/10/15 16:19* Description:*/ public class Node {int value;Node next;public Node(int value) {this.value = value;} } package csdn.dreamzuora.stack;/*** @author weijie* @date 2020/10/15 16:18* Description:*/ public class LinkedListStack {private Node head;private int size = 0;public void push(Node node){if (size == 0){head = node;}else {node.next = head;head = node;}size ++;}public Node pop(){if (size != 0){Node node = head;head = head.next;size--;return node;}else {return null;}}public boolean isEmpty(){if (size <= 0){return true;}return false;}}單元測(cè)試
package csdn.dreamzuora.stack;import org.junit.Test;import static org.junit.Assert.*;/*** @author weijie* @date 2020/10/15 16:31* Description:*/ public class LinkedListStackTest {LinkedListStack stack = new LinkedListStack();@Testpublic void print() {System.out.print("入棧:");for (int i = 0; i < 10; i++){stack.push(new Node(i));System.out.print("->" + i);}System.out.println();System.out.print("出棧:");while (!stack.isEmpty()){System.out.print("->" + stack.pop().value);}} }控制臺(tái)打印
總結(jié)
以上是生活随笔為你收集整理的栈、队列 java代码实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell读取文件并且遍历输出
- 下一篇: python机器学习常用包下载安装以及使