mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 03:46:28 +08:00
Add the chapter of stack and queue.
This commit is contained in:
parent
8f8f6319af
commit
ac8d0bcfeb
10 changed files with 454 additions and 8 deletions
|
@ -8,7 +8,7 @@ public class list {
|
|||
// 注意数组的元素类型是 int[] 的包装类 Integer[]
|
||||
Integer[] numbers = new Integer[] { 1, 3, 2, 5, 4 };
|
||||
List<Integer> list = new ArrayList<>(Arrays.asList(numbers));
|
||||
System.out.println("列表 list = " + Arrays.toString(list.toArray()));
|
||||
System.out.println("列表 list = " + list);
|
||||
|
||||
/* 访问元素 */
|
||||
int num = list.get(1);
|
||||
|
@ -16,11 +16,11 @@ public class list {
|
|||
|
||||
/* 更新元素 */
|
||||
list.set(1, 0);
|
||||
System.out.println("将索引 1 处的元素更新为 0 ,得到 list = " + Arrays.toString(list.toArray()));
|
||||
System.out.println("将索引 1 处的元素更新为 0 ,得到 list = " + list);
|
||||
|
||||
/* 清空列表 */
|
||||
list.clear();
|
||||
System.out.println("清空列表后 list = " + Arrays.toString(list.toArray()));
|
||||
System.out.println("清空列表后 list = " + list);
|
||||
|
||||
/* 尾部添加元素 */
|
||||
list.add(1);
|
||||
|
@ -28,15 +28,15 @@ public class list {
|
|||
list.add(2);
|
||||
list.add(5);
|
||||
list.add(4);
|
||||
System.out.println("添加元素后 list = " + Arrays.toString(list.toArray()));
|
||||
System.out.println("添加元素后 list = " + list);
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.add(3, 6);
|
||||
System.out.println("在索引 3 处插入数字 6 ,得到 list = " + Arrays.toString(list.toArray()));
|
||||
System.out.println("在索引 3 处插入数字 6 ,得到 list = " + list);
|
||||
|
||||
/* 删除元素 */
|
||||
list.remove(3);
|
||||
System.out.println("删除索引 3 处的元素,得到 list = " + Arrays.toString(list.toArray()));
|
||||
System.out.println("删除索引 3 处的元素,得到 list = " + list);
|
||||
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
|
@ -53,10 +53,10 @@ public class list {
|
|||
/* 拼接两个列表 */
|
||||
List<Integer> list1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));
|
||||
list.addAll(list1);
|
||||
System.out.println("将列表 list1 拼接到 list 之后,得到 list = " + Arrays.toString(list.toArray()));
|
||||
System.out.println("将列表 list1 拼接到 list 之后,得到 list = " + list);
|
||||
|
||||
/* 排序列表 */
|
||||
Collections.sort(list);
|
||||
System.out.println("排序列表后 list = " + Arrays.toString(list.toArray()));
|
||||
System.out.println("排序列表后 list = " + list);
|
||||
}
|
||||
}
|
||||
|
|
113
codes/java/chapter_stack_and_queue/array_queue.java
Normal file
113
codes/java/chapter_stack_and_queue/array_queue.java
Normal 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()));
|
||||
}
|
||||
}
|
||||
}
|
77
codes/java/chapter_stack_and_queue/array_stack.java
Normal file
77
codes/java/chapter_stack_and_queue/array_stack.java
Normal 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();
|
||||
}
|
||||
}
|
37
codes/java/chapter_stack_and_queue/deque.java
Normal file
37
codes/java/chapter_stack_and_queue/deque.java
Normal 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();
|
||||
}
|
||||
}
|
75
codes/java/chapter_stack_and_queue/linkedlist_queue.java
Normal file
75
codes/java/chapter_stack_and_queue/linkedlist_queue.java
Normal 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();
|
||||
}
|
||||
}
|
72
codes/java/chapter_stack_and_queue/linkedlist_stack.java
Normal file
72
codes/java/chapter_stack_and_queue/linkedlist_stack.java
Normal 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();
|
||||
}
|
||||
}
|
33
codes/java/chapter_stack_and_queue/queue.java
Normal file
33
codes/java/chapter_stack_and_queue/queue.java
Normal 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();
|
||||
}
|
||||
}
|
33
codes/java/chapter_stack_and_queue/stack.java
Normal file
33
codes/java/chapter_stack_and_queue/stack.java
Normal 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();
|
||||
}
|
||||
}
|
|
@ -21,3 +21,4 @@ TODO
|
|||
| 新增:数据与内存 | 2022-11-05 |
|
||||
| 更新:各章节 Java 代码 | 2022-11-06 |
|
||||
| 更新:列表 Java 代码、配图 | 2022-11-07 |
|
||||
| 新增:栈与队列 | 2022-11-09 |
|
||||
|
|
|
@ -135,5 +135,10 @@ nav:
|
|||
- 链表: chapter_array_and_linkedlist/linked_list.md
|
||||
- 列表: chapter_array_and_linkedlist/list.md
|
||||
- 小结: chapter_array_and_linkedlist/summary.md
|
||||
- 栈与队列:
|
||||
- chapter_stack_and_queue/index.md
|
||||
- 栈: chapter_stack_and_queue/stack.md
|
||||
- 队列: chapter_stack_and_queue/queue.md
|
||||
- 小结: chapter_stack_and_queue/summary.md
|
||||
- License:
|
||||
- chapter_license/index.md
|
Loading…
Reference in a new issue