Add the chapter of stack and queue.

This commit is contained in:
krahets
2022-11-10 03:40:57 +08:00
parent 8f8f6319af
commit ac8d0bcfeb
10 changed files with 454 additions and 8 deletions

View File

@@ -0,0 +1,113 @@
package chapter_stack_and_queue;
import java.util.*;
/* 基于环形数组实现的队列 */
class ArrayQueue {
int[] nums; // 用于存储队列元素的数组
int size = 0; // 队列长度(即元素个数)
int front = 0; // 头指针,指向队首
int rear = 0; // 尾指针,指向队尾 + 1
public ArrayQueue(int capacity) {
// 初始化数组
nums = new int[capacity];
}
/* 获取队列的容量 */
public int capacity() {
return nums.length;
}
/* 获取队列的长度 */
public int size() {
int capacity = capacity();
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
return (capacity + rear - front) % capacity;
}
/* 判断队列是否为空 */
public boolean isEmpty() {
return rear - front == 0;
}
/* 入队 */
public void offer(int num) {
if (size() == capacity()) {
System.out.println("队列已满");
return;
}
// 尾结点后添加 num
nums[rear] = num;
// 尾指针向后移动一位,越过尾部后返回到数组头部
rear = (rear + 1) % capacity();
}
/* 出队 */
public int poll() {
// 删除头节点
if (isEmpty())
throw new EmptyStackException();
int num = nums[front];
// 队头指针向后移动,越过尾部后返回到数组头部
front = (front + 1) % capacity();
return num;
}
/* 访问队首元素 */
public int peek() {
// 删除头节点
if (isEmpty())
throw new EmptyStackException();
return nums[front];
}
public int[] toArray() {
int size = size();
int capacity = capacity();
// 仅转换有效长度范围内的列表元素
int[] arr = new int[size];
for (int i = 0, j = front; i < size; i++, j++) {
arr[i] = nums[j % capacity];
}
return arr;
}
}
public class array_queue {
public static void main(String[] args) {
/* 初始化队列 */
int capacity = 10;
ArrayQueue queue = new ArrayQueue(capacity);
/* 元素入队 */
queue.offer(1);
queue.offer(3);
queue.offer(2);
queue.offer(5);
queue.offer(4);
System.out.println("队列 queue = " + Arrays.toString(queue.toArray()));
/* 访问队首元素 */
int peek = queue.peek();
System.out.println("队首元素 peek = " + peek);
/* 元素出队 */
int poll = queue.poll();
System.out.println("出队元素 poll = " + poll + ",出队后 queue = " + Arrays.toString(queue.toArray()));
/* 获取队列的长度 */
int size = queue.size();
System.out.println("队列长度 size = " + size);
/* 判断队列是否为空 */
boolean isEmpty = queue.isEmpty();
/* 测试环形数组 */
for (int i = 0; i < 10; i++) {
queue.offer(i);
queue.poll();
System.out.println("" + i + " 轮入队+出队后 queue = " + Arrays.toString(queue.toArray()));
}
}
}

View File

@@ -0,0 +1,77 @@
package chapter_stack_and_queue;
import java.util.*;
/* 基于数组实现的栈 */
class ArrayStack {
ArrayList<Integer> list;
public ArrayStack() {
// 初始化列表(动态数组)
list = new ArrayList<>();
}
/* 获取栈的长度 */
public int size() {
return list.size();
}
/* 判断栈是否为空 */
public boolean isEmpty() {
return size() == 0;
}
/* 入栈 */
public void push(int num) {
list.add(num);
}
/* 出栈 */
public int pop() {
return list.remove(size() - 1);
}
/* 访问栈顶元素 */
public int peek() {
return list.get(size() - 1);
}
/* 访问索引 index 处元素 */
public int get(int index) {
return list.get(index);
}
/* 将 List 转化为 Array 并返回 */
public Object[] toArray() {
return list.toArray();
}
}
public class array_stack {
public static void main(String[] args) {
/* 初始化栈 */
ArrayStack stack = new ArrayStack();
/* 元素入栈 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
System.out.println("栈 stack = " + Arrays.toString(stack.toArray()));
/* 访问栈顶元素 */
int peek = stack.peek();
System.out.println("栈顶元素 peek = " + peek);
/* 元素出栈 */
int pop = stack.pop();
System.out.println("出栈元素 pop = " + pop + ",出栈后 stack = " + Arrays.toString(stack.toArray()));
/* 获取栈的长度 */
int size = stack.size();
System.out.println("栈的长度 size = " + size);
/* 判断是否为空 */
boolean isEmpty = stack.isEmpty();
}
}

View File

@@ -0,0 +1,37 @@
package chapter_stack_and_queue;
import java.util.*;
public class deque {
public static void main(String[] args) {
/* 初始化队列 */
Deque<Integer> deque = new LinkedList<>();
/* 元素入队 */
deque.offerLast(2);
deque.offerLast(5);
deque.offerLast(4);
deque.offerFirst(3);
deque.offerFirst(1);
System.out.println("队列 deque = " + deque);
/* 访问队首元素 */
int peekFirst = deque.peekFirst();
System.out.println("队首元素 peekFirst = " + peekFirst);
int peekLast = deque.peekLast();
System.out.println("队尾元素 peekLast = " + peekLast);
/* 元素出队 */
int pollFirst = deque.pollFirst();
System.out.println("队首出队元素 pollFirst = " + pollFirst + ",队首出队后 deque = " + deque);
int pollLast = deque.pollLast();
System.out.println("队尾出队元素 pollLast = " + pollLast + ",队尾出队后 deque = " + deque);
/* 获取队列的长度 */
int size = deque.size();
System.out.println("队列长度 size = " + size);
/* 判断队列是否为空 */
boolean isEmpty = deque.isEmpty();
}
}

View File

@@ -0,0 +1,75 @@
package chapter_stack_and_queue;
import java.util.*;
/* 基于链表实现的队列 */
class LinkedListQueue {
LinkedList<Integer> list;
public LinkedListQueue() {
// 初始化链表
list = new LinkedList<>();
}
/* 获取队列的长度 */
public int size() {
return list.size();
}
/* 判断队列是否为空 */
public boolean isEmpty() {
return list.size() == 0;
}
/* 入队 */
public void offer(int num) {
// 尾结点后添加 num
list.addLast(num);
}
/* 出队 */
public int poll() {
// 删除头节点
return list.removeFirst();
}
/* 访问队首元素 */
public int peek() {
return list.getFirst();
}
/* 将 List 转化为 Array 并返回 */
public Object[] toArray() {
return list.toArray();
}
}
public class linkedlist_queue {
public static void main(String[] args) {
/* 初始化队列 */
LinkedListQueue queue = new LinkedListQueue();
/* 元素入队 */
queue.offer(1);
queue.offer(3);
queue.offer(2);
queue.offer(5);
queue.offer(4);
System.out.println("队列 queue = " + Arrays.toString(queue.toArray()));
/* 访问队首元素 */
int peek = queue.peek();
System.out.println("队首元素 peek = " + peek);
/* 元素出队 */
int poll = queue.poll();
System.out.println("出队元素 poll = " + poll + ",出队后 queue = " + Arrays.toString(queue.toArray()));
/* 获取队列的长度 */
int size = queue.size();
System.out.println("队列长度 size = " + size);
/* 判断队列是否为空 */
boolean isEmpty = queue.isEmpty();
}
}

View File

@@ -0,0 +1,72 @@
package chapter_stack_and_queue;
import java.util.*;
/* 基于链表实现的栈 */
class LinkedListStack {
LinkedList<Integer> list;
public LinkedListStack() {
// 初始化链表
list = new LinkedList<>();
}
/* 获取栈的长度 */
public int size() {
return list.size();
}
/* 判断栈是否为空 */
public boolean isEmpty() {
return size() == 0;
}
/* 入栈 */
public void push(int num) {
list.addLast(num);
}
/* 出栈 */
public int pop() {
return list.removeLast();
}
/* 访问栈顶元素 */
public int peek() {
return list.getLast();
}
/* 将 List 转化为 Array 并返回 */
public Object[] toArray() {
return list.toArray();
}
}
public class linkedlist_stack {
public static void main(String[] args) {
/* 初始化栈 */
LinkedListStack stack = new LinkedListStack();
/* 元素入栈 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
System.out.println("栈 stack = " + Arrays.toString(stack.toArray()));
/* 访问栈顶元素 */
int peek = stack.peek();
System.out.println("栈顶元素 peek = " + peek);
/* 元素出栈 */
int pop = stack.pop();
System.out.println("出栈元素 pop = " + pop + ",出栈后 stack = " + Arrays.toString(stack.toArray()));
/* 获取栈的长度 */
int size = stack.size();
System.out.println("栈的长度 size = " + size);
/* 判断是否为空 */
boolean isEmpty = stack.isEmpty();
}
}

View File

@@ -0,0 +1,33 @@
package chapter_stack_and_queue;
import java.util.*;
public class queue {
public static void main(String[] args) {
/* 初始化队列 */
Queue<Integer> queue = new LinkedList<>();
/* 元素入队 */
queue.offer(1);
queue.offer(3);
queue.offer(2);
queue.offer(5);
queue.offer(4);
System.out.println("队列 queue = " + queue);
/* 访问队首元素 */
int peek = queue.peek();
System.out.println("队首元素 peek = " + peek);
/* 元素出队 */
int poll = queue.poll();
System.out.println("出队元素 poll = " + poll + ",出队后 queue = " + queue);
/* 获取队列的长度 */
int size = queue.size();
System.out.println("队列长度 size = " + size);
/* 判断队列是否为空 */
boolean isEmpty = queue.isEmpty();
}
}

View File

@@ -0,0 +1,33 @@
package chapter_stack_and_queue;
import java.util.*;
public class stack {
public static void main(String[] args) {
/* 初始化栈 */
Stack<Integer> stack = new Stack<>();
/* 元素入栈 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
System.out.println("栈 stack = " + stack);
/* 访问栈顶元素 */
int peek = stack.peek();
System.out.println("栈顶元素 peek = " + peek);
/* 元素出栈 */
int pop = stack.pop();
System.out.println("出栈元素 pop = " + pop + ",出栈后 stack = " + stack);
/* 获取栈的长度 */
int size = stack.size();
System.out.println("栈的长度 size = " + size);
/* 判断是否为空 */
boolean isEmpty = stack.isEmpty();
}
}