Modify 。** to **。 for better visualization.

This commit is contained in:
Yudong Jin
2023-01-07 20:34:32 +08:00
parent fc4d7e5e3b
commit 694ea4f665
22 changed files with 252 additions and 220 deletions

View File

@@ -12,7 +12,7 @@ comments: true
## 双向队列常用操作
双向队列的常用操作见下表方法名需根据编程语言设定来具体确定
双向队列的常用操作见下表方法命名以 Java 为例)
<p align="center"> Table. 双向队列的常用操作 </p>
@@ -38,25 +38,25 @@ comments: true
```java title="deque.java"
/* 初始化双向队列 */
Deque<Integer> deque = new LinkedList<>();
/* 元素入队 */
deque.offerLast(2); // 添加至队尾
deque.offerLast(5);
deque.offerLast(4);
deque.offerFirst(3); // 添加至队首
deque.offerFirst(1);
/* 访问元素 */
int peekFirst = deque.peekFirst(); // 队首元素
int peekLast = deque.peekLast(); // 队尾元素
/* 元素出队 */
int pollFirst = deque.pollFirst(); // 队首元素出队
int pollLast = deque.pollLast(); // 队尾元素出队
/* 获取双向队列的长度 */
int size = deque.size();
/* 判断双向队列是否为空 */
boolean isEmpty = deque.isEmpty();
```
@@ -66,25 +66,25 @@ comments: true
```cpp title="deque.cpp"
/* 初始化双向队列 */
deque<int> deque;
/* 元素入队 */
deque.push_back(2); // 添加至队尾
deque.push_back(5);
deque.push_back(4);
deque.push_front(3); // 添加至队首
deque.push_front(1);
/* 访问元素 */
int front = deque.front(); // 队首元素
int back = deque.back(); // 队尾元素
/* 元素出队 */
deque.pop_front(); // 队首元素出队
deque.pop_back(); // 队尾元素出队
/* 获取双向队列的长度 */
int size = deque.size();
/* 判断双向队列是否为空 */
bool empty = deque.empty();
```
@@ -94,25 +94,25 @@ comments: true
```python title="deque.py"
""" 初始化双向队列 """
duque = deque()
""" 元素入队 """
duque.append(2) # 添加至队尾
duque.append(5)
duque.append(4)
duque.appendleft(3) # 添加至队首
duque.appendleft(1)
""" 访问元素 """
front = duque[0] # 队首元素
rear = duque[-1] # 队尾元素
""" 元素出队 """
pop_front = duque.popleft() # 队首元素出队
pop_rear = duque.pop() # 队尾元素出队
""" 获取双向队列的长度 """
size = len(duque)
""" 判断双向队列是否为空 """
is_empty = len(duque) == 0
```
@@ -123,25 +123,25 @@ comments: true
/* 初始化双向队列 */
// 在 Go 中,将 list 作为双向队列使用
deque := list.New()
/* 元素入队 */
deque.PushBack(2) // 添加至队尾
deque.PushBack(5)
deque.PushBack(4)
deque.PushFront(3) // 添加至队首
deque.PushFront(1)
/* 访问元素 */
front := deque.Front() // 队首元素
rear := deque.Back() // 队尾元素
/* 元素出队 */
deque.Remove(front) // 队首元素出队
deque.Remove(rear) // 队尾元素出队
/* 获取双向队列的长度 */
size := deque.Len()
/* 判断双向队列是否为空 */
isEmpty := deque.Len() == 0
```
@@ -149,19 +149,19 @@ comments: true
=== "JavaScript"
```js title="deque.js"
```
=== "TypeScript"
```typescript title="deque.ts"
```
=== "C"
```c title="deque.c"
```
=== "C#"
@@ -170,25 +170,25 @@ comments: true
/* 初始化双向队列 */
// 在 C# 中,将链表 LinkedList 看作双向队列来使用
LinkedList<int> deque = new LinkedList<int>();
/* 元素入队 */
deque.AddLast(2); // 添加至队尾
deque.AddLast(5);
deque.AddLast(4);
deque.AddFirst(3); // 添加至队首
deque.AddFirst(1);
/* 访问元素 */
int peekFirst = deque.First.Value; // 队首元素
int peekLast = deque.Last.Value; // 队尾元素
/* 元素出队 */
deque.RemoveFirst(); // 队首元素出队
deque.RemoveLast(); // 队尾元素出队
/* 获取双向队列的长度 */
int size = deque.Count;
/* 判断双向队列是否为空 */
bool isEmpty = deque.Count == 0;
```

View File

@@ -14,7 +14,7 @@ comments: true
## 队列常用操作
队列的常用操作见下表方法命名需根据编程语言的设定来具体确定
队列的常用操作见下表方法命名以 Java 为例)
<p align="center"> Table. 队列的常用操作 </p>
@@ -37,23 +37,23 @@ comments: true
```java title="queue.java"
/* 初始化队列 */
Queue<Integer> queue = new LinkedList<>();
/* 元素入队 */
queue.offer(1);
queue.offer(3);
queue.offer(2);
queue.offer(5);
queue.offer(4);
/* 访问队首元素 */
int peek = queue.peek();
/* 元素出队 */
int poll = queue.poll();
/* 获取队列的长度 */
int size = queue.size();
/* 判断队列是否为空 */
boolean isEmpty = queue.isEmpty();
```
@@ -91,23 +91,23 @@ comments: true
# 在 Python 中,我们一般将双向队列类 deque 看作队列使用
# 虽然 queue.Queue() 是纯正的队列类,但不太好用,因此不建议
que = collections.deque()
""" 元素入队 """
que.append(1)
que.append(3)
que.append(2)
que.append(5)
que.append(4)
""" 访问队首元素 """
front = que[0];
""" 元素出队 """
pop = que.popleft()
""" 获取队列的长度 """
size = len(que)
""" 判断队列是否为空 """
is_empty = len(que) == 0
```
@@ -118,24 +118,24 @@ comments: true
/* 初始化队列 */
// 在 Go 中,将 list 作为队列来使用
queue := list.New()
/* 元素入队 */
queue.PushBack(1)
queue.PushBack(3)
queue.PushBack(2)
queue.PushBack(5)
queue.PushBack(4)
/* 访问队首元素 */
peek := queue.Front()
/* 元素出队 */
poll := queue.Front()
queue.Remove(poll)
/* 获取队列的长度 */
size := queue.Len()
/* 判断队列是否为空 */
isEmpty := queue.Len() == 0
```
@@ -146,24 +146,24 @@ comments: true
/* 初始化队列 */
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
const queue = [];
/* 元素入队 */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
/* 访问队首元素 */
const peek = queue[0];
/* 元素出队 */
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
const poll = queue.shift();
/* 获取队列的长度 */
const size = queue.length;
/* 判断队列是否为空 */
const empty = queue.length === 0;
```
@@ -174,24 +174,24 @@ comments: true
/* 初始化队列 */
// TypeScript 没有内置的队列,可以把 Array 当作队列来使用
const queue: number[] = [];
/* 元素入队 */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
/* 访问队首元素 */
const peek = queue[0];
/* 元素出队 */
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
const poll = queue.shift();
/* 获取队列的长度 */
const size = queue.length;
/* 判断队列是否为空 */
const empty = queue.length === 0;
```
@@ -199,7 +199,7 @@ comments: true
=== "C"
```c title="queue.c"
```
=== "C#"
@@ -207,23 +207,23 @@ comments: true
```csharp title="queue.cs"
/* 初始化队列 */
Queue<int> queue = new();
/* 元素入队 */
queue.Enqueue(1);
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(5);
queue.Enqueue(4);
/* 访问队首元素 */
int peek = queue.Peek();
/* 元素出队 */
int poll = queue.Dequeue();
/* 获取队列的长度 */
int size = queue.Count();
/* 判断队列是否为空 */
bool isEmpty = queue.Count() == 0;
```
@@ -243,7 +243,7 @@ comments: true
class LinkedListQueue {
private ListNode front, rear; // 头结点 front ,尾结点 rear
private int queSize = 0;
public LinkedListQueue() {
front = null;
rear = null;
@@ -296,7 +296,7 @@ comments: true
private:
ListNode *front, *rear; // 头结点 front ,尾结点 rear
int queSize;
public:
LinkedListQueue() {
front = nullptr;
@@ -355,15 +355,15 @@ comments: true
self.__front = None # 头结点 front
self.__rear = None # 尾结点 rear
self.__size = 0
""" 获取队列的长度 """
def size(self):
return self.__size
""" 判断队列是否为空 """
def is_empty(self):
return not self.__front
""" 入队 """
def push(self, num):
# 尾结点后添加 num
@@ -377,7 +377,7 @@ comments: true
self.__rear.next = node
self.__rear = node
self.__size += 1
""" 出队 """
def poll(self):
num = self.peek()
@@ -385,7 +385,7 @@ comments: true
self.__front = self.__front.next
self.__size -= 1
return num
""" 访问队首元素 """
def peek(self):
if self.size() == 0:
@@ -548,7 +548,7 @@ comments: true
=== "C"
```c title="linkedlist_queue.c"
```
=== "C#"
@@ -630,7 +630,7 @@ comments: true
private int[] nums; // 用于存储队列元素的数组
private int front = 0; // 头指针,指向队首
private int rear = 0; // 尾指针,指向队尾 + 1
public ArrayQueue(int capacity) {
// 初始化数组
nums = new int[capacity];
@@ -686,7 +686,7 @@ comments: true
int cap; // 队列容量
int front = 0; // 头指针,指向队首
int rear = 0; // 尾指针,指向队尾 + 1
public:
ArrayQueue(int capacity) {
// 初始化数组
@@ -741,20 +741,20 @@ comments: true
self.__nums = [0] * size # 用于存储队列元素的数组
self.__front = 0 # 头指针,指向队首
self.__rear = 0 # 尾指针,指向队尾 + 1
""" 获取队列的容量 """
def capacity(self):
return len(self.__nums)
""" 获取队列的长度 """
def size(self):
# 由于将数组看作为环形,可能 rear < front ,因此需要取余数
return (self.capacity() + self.__rear - self.__front) % self.capacity()
""" 判断队列是否为空 """
def is_empty(self):
return (self.__rear - self.__front) == 0
""" 入队 """
def push(self, val):
if self.size() == self.capacity():
@@ -764,21 +764,21 @@ comments: true
self.__nums[self.__rear] = val
# 尾指针向后移动一位,越过尾部后返回到数组头部
self.__rear = (self.__rear + 1) % self.capacity()
""" 出队 """
def poll(self):
num = self.peek()
# 队头指针向后移动一位,若越过尾部则返回到数组头部
self.__front = (self.__front + 1) % self.capacity()
return num
""" 访问队首元素 """
def peek(self):
if self.is_empty():
print("队列为空")
return False
return self.__nums[self.__front]
""" 返回列表用于打印 """
def to_list(self):
res = [0] * self.size()
@@ -950,7 +950,7 @@ comments: true
=== "C"
```c title="array_queue.c"
```
=== "C#"
@@ -1017,5 +1017,5 @@ comments: true
## 队列典型应用
- **淘宝订单** 购物者下单后,订单就被加入到队列之中,随后系统再根据顺序依次处理队列中的订单。在双十一时,在短时间内会产生海量的订单,如何处理「高并发」则是工程师们需要重点思考的问题。
- **各种待办事项** 例如打印机的任务队列、餐厅的出餐队列等等。
- **淘宝订单**购物者下单后,订单就被加入到队列之中,随后系统再根据顺序依次处理队列中的订单。在双十一时,在短时间内会产生海量的订单,如何处理「高并发」则是工程师们需要重点思考的问题。
- **各种待办事项**例如打印机的任务队列、餐厅的出餐队列等等。

View File

@@ -16,7 +16,7 @@ comments: true
## 栈常用操作
栈的常用操作见下表方法名需根据编程语言设定来具体确定
栈的常用操作见下表方法命名以 Java 为例)
<p align="center"> Table. 栈的常用操作 </p>
@@ -40,23 +40,23 @@ comments: true
/* 初始化栈 */
// 在 Java 中,推荐将 LinkedList 当作栈来使用
LinkedList<Integer> stack = new LinkedList<>();
/* 元素入栈 */
stack.addLast(1);
stack.addLast(3);
stack.addLast(2);
stack.addLast(5);
stack.addLast(4);
/* 访问栈顶元素 */
int peek = stack.peekLast();
/* 元素出栈 */
int pop = stack.removeLast();
/* 获取栈的长度 */
int size = stack.size();
/* 判断是否为空 */
boolean isEmpty = stack.isEmpty();
```
@@ -66,23 +66,23 @@ comments: true
```cpp title="stack.cpp"
/* 初始化栈 */
stack<int> stack;
/* 元素入栈 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
/* 访问栈顶元素 */
int top = stack.top();
/* 元素出栈 */
stack.pop();
/* 获取栈的长度 */
int size = stack.size();
/* 判断是否为空 */
bool empty = stack.empty();
```
@@ -93,23 +93,23 @@ comments: true
""" 初始化栈 """
# Python 没有内置的栈类,可以把 List 当作栈来使用
stack = []
""" 元素入栈 """
stack.append(1)
stack.append(3)
stack.append(2)
stack.append(5)
stack.append(4)
""" 访问栈顶元素 """
peek = stack[-1]
""" 元素出栈 """
pop = stack.pop()
""" 获取栈的长度 """
size = len(stack)
""" 判断是否为空 """
is_empty = len(stack) == 0
```
@@ -120,24 +120,24 @@ comments: true
/* 初始化栈 */
// 在 Go 中,推荐将 Slice 当作栈来使用
var stack []int
/* 元素入栈 */
stack = append(stack, 1)
stack = append(stack, 3)
stack = append(stack, 2)
stack = append(stack, 5)
stack = append(stack, 4)
/* 访问栈顶元素 */
peek := stack[len(stack)-1]
/* 元素出栈 */
pop := stack[len(stack)-1]
stack = stack[:len(stack)-1]
/* 获取栈的长度 */
size := len(stack)
/* 判断是否为空 */
isEmpty := len(stack) == 0
```
@@ -148,23 +148,23 @@ comments: true
/* 初始化栈 */
// Javascript 没有内置的栈类,可以把 Array 当作栈来使用
const stack = [];
/* 元素入栈 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
/* 访问栈顶元素 */
const peek = stack[stack.length-1];
/* 元素出栈 */
const pop = stack.pop();
/* 获取栈的长度 */
const size = stack.length;
/* 判断是否为空 */
const is_empty = stack.length === 0;
```
@@ -175,23 +175,23 @@ comments: true
/* 初始化栈 */
// Typescript 没有内置的栈类,可以把 Array 当作栈来使用
const stack: number[] = [];
/* 元素入栈 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
/* 访问栈顶元素 */
const peek = stack[stack.length - 1];
/* 元素出栈 */
const pop = stack.pop();
/* 获取栈的长度 */
const size = stack.length;
/* 判断是否为空 */
const is_empty = stack.length === 0;
```
@@ -199,7 +199,7 @@ comments: true
=== "C"
```c title="stack.c"
```
=== "C#"
@@ -207,23 +207,23 @@ comments: true
```csharp title="stack.cs"
/* 初始化栈 */
Stack<int> stack = new ();
/* 元素入栈 */
stack.Push(1);
stack.Push(3);
stack.Push(2);
stack.Push(5);
stack.Push(4);
/* 访问栈顶元素 */
int peek = stack.Peek();
/* 元素出栈 */
int pop = stack.Pop();
/* 获取栈的长度 */
int size = stack.Count();
/* 判断是否为空 */
bool isEmpty = stack.Count()==0;
```
@@ -291,7 +291,7 @@ comments: true
private:
ListNode* stackTop; // 将头结点作为栈顶
int stkSize; // 栈的长度
public:
LinkedListStack() {
stackTop = nullptr;
@@ -338,29 +338,29 @@ comments: true
def __init__(self):
self.__peek = None
self.__size = 0
""" 获取栈的长度 """
def size(self):
return self.__size
""" 判断栈是否为空 """
def is_empty(self):
return not self.__peek
""" 入栈 """
def push(self, val):
node = ListNode(val)
node.next = self.__peek
self.__peek = node
self.__size += 1
""" 出栈 """
def pop(self):
num = self.peek()
self.__peek = self.__peek.next
self.__size -= 1
return num
""" 访问栈顶元素 """
def peek(self):
# 判空处理
@@ -420,21 +420,21 @@ comments: true
class LinkedListStack {
#stackPeek; // 将头结点作为栈顶
#stkSize = 0; // 栈的长度
constructor() {
this.#stackPeek = null;
}
/* 获取栈的长度 */
get size() {
return this.#stkSize;
}
/* 判断栈是否为空 */
isEmpty() {
return this.size == 0;
}
/* 入栈 */
push(num) {
const node = new ListNode(num);
@@ -442,7 +442,7 @@ comments: true
this.#stackPeek = node;
this.#stkSize++;
}
/* 出栈 */
pop() {
const num = this.peek();
@@ -453,7 +453,7 @@ comments: true
this.#stkSize--;
return num;
}
/* 访问栈顶元素 */
peek() {
if (!this.#stackPeek) {
@@ -461,7 +461,7 @@ comments: true
}
return this.#stackPeek.val;
}
/* 将链表转化为 Array 并返回 */
toArray() {
let node = this.#stackPeek;
@@ -482,21 +482,21 @@ comments: true
class LinkedListStack {
private stackPeek: ListNode | null; // 将头结点作为栈顶
private stkSize: number = 0; // 栈的长度
constructor() {
this.stackPeek = null;
}
/* 获取栈的长度 */
get size(): number {
return this.stkSize;
}
/* 判断栈是否为空 */
isEmpty(): boolean {
return this.size == 0;
}
/* 入栈 */
push(num: number): void {
const node = new ListNode(num);
@@ -504,7 +504,7 @@ comments: true
this.stackPeek = node;
this.stkSize++;
}
/* 出栈 */
pop(): number {
const num = this.peek();
@@ -515,7 +515,7 @@ comments: true
this.stkSize--;
return num;
}
/* 访问栈顶元素 */
peek(): number {
if (!this.stackPeek) {
@@ -523,7 +523,7 @@ comments: true
}
return this.stackPeek.val;
}
/* 将链表转化为 Array 并返回 */
toArray(): number[] {
let node = this.stackPeek;
@@ -540,7 +540,7 @@ comments: true
=== "C"
```c title="linkedlist_stack.c"
```
=== "C#"
@@ -676,24 +676,24 @@ comments: true
class ArrayStack:
def __init__(self):
self.__stack = []
""" 获取栈的长度 """
def size(self):
return len(self.__stack)
""" 判断栈是否为空 """
def is_empty(self):
return self.__stack == []
""" 入栈 """
def push(self, item):
self.__stack.append(item)
""" 出栈 """
def pop(self):
assert not self.is_empty(), "栈为空"
return self.__stack.pop()
""" 访问栈顶元素 """
def peek(self):
assert not self.is_empty(), "栈为空"
@@ -821,7 +821,7 @@ comments: true
=== "C"
```c title="array_stack.c"
```
=== "C#"
@@ -876,5 +876,5 @@ comments: true
## 栈典型应用
- **浏览器中的后退与前进、软件中的撤销与反撤销** 每当我们打开新的网页,浏览器就讲上一个网页执行入栈,这样我们就可以通过「后退」操作来回到上一页面,后退操作实际上是在执行出栈。如果要同时支持后退和前进,那么则需要两个栈来配合实现。
- **程序内存管理** 每当调用函数时,系统就会在栈顶添加一个栈帧,用来记录函数的上下文信息。在递归函数中,向下递推会不断执行入栈,向上回溯阶段时出栈。
- **浏览器中的后退与前进、软件中的撤销与反撤销**每当我们打开新的网页,浏览器就讲上一个网页执行入栈,这样我们就可以通过「后退」操作来回到上一页面,后退操作实际上是在执行出栈。如果要同时支持后退和前进,那么则需要两个栈来配合实现。
- **程序内存管理**每当调用函数时,系统就会在栈顶添加一个栈帧,用来记录函数的上下文信息。在递归函数中,向下递推会不断执行入栈,向上回溯阶段时出栈。