文章目录
- 普通队列
- 数组实现
- java代码实现
- 单元测试
- 控制台打印
- 链表实现
- java代码实现
- 单元测试
- 控制台打印
- LinkedList队列使用
- 优先队列:PriorityQueue使用
- 栈
- 数组实现
- java代码实现
- 单元测试
- 控制台打印
- 链表实现
- java代码实现
- 单元测试
- 控制台打印
普通队列
概念:先入先出
数组实现
java代码实现
package csdn.dreamzuora.queue;/*** @author weijie* @date 2020/10/15 19:54* Description: 先入先出*/
public class ArrayQueue {int[] queue;/*** 队头*/int head = 0;/*** 队尾*/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(){/*** 队头等于队尾此时表示队列为空*/if (head == tail){return true;}return false;}}
单元测试
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("入队列:");for (int i = 0; i < 10; i++){System.out.print("->" + i);queue.enqueue(i);}System.out.println();System.out.print("出队列:");while (!queue.isEmpty()){System.out.print("->" + queue.dequeue());}}
}
控制台打印
链表实现
java代码实现
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;}
}
单元测试
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("入队列:");for (int i = 0; i < 10; i++){System.out.print("->" + i);queue.enqueue(i);}System.out.println();System.out.print("出队列:");while (!queue.isEmpty()){System.out.print("->" + queue.dequeue());}}
}
控制台打印
LinkedList队列使用
package csdn.dreamzuora.queue;import org.junit.Test;import java.util.LinkedList;/*** Title: 链表当作队列使用* Description:** @version 1.0* @author: weijie* @date: 2020/10/19 16:52*/
public class LinkedListTest {@Testpublic void example(){LinkedList<Integer> queue = new LinkedList<>();/*** 入队*/queue.offer(1);queue.offer(2);queue.offer(3);/*** 出队*/while (!queue.isEmpty()){System.out.println(queue.poll());}}}
优先队列:PriorityQueue使用
package csdn.dreamzuora.queue;import org.junit.Test;import java.util.Comparator;
import java.util.PriorityQueue;/*** Title: 优先队列* 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 + '\'' +'}';}}
}
栈
概念:先入后出
数组实现
java代码实现
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];}}
单元测试
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());}}
}
控制台打印
链表实现
java代码实现
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;}}
单元测试
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);}}
}