This commit is contained in:
krahets
2023-06-02 02:38:24 +08:00
parent 874e75d92d
commit 2a85d796e6
35 changed files with 2354 additions and 0 deletions

View File

@@ -289,6 +289,12 @@ comments: true
```
=== "Dart"
```dart title="deque.dart"
```
## 5.3.2.   双向队列实现 *
双向队列的实现与队列类似,可以选择链表或数组作为底层数据结构。
@@ -1636,6 +1642,136 @@ comments: true
}
```
=== "Dart"
```dart title="linkedlist_deque.dart"
/* 双向链表节点 */
class ListNode {
int val; // 节点值
ListNode? next; // 后继节点引用(指针)
ListNode? prev; // 前驱节点引用(指针)
ListNode(this.val, {this.next, this.prev});
}
/* 基于双向链表实现的双向对列 */
class LinkedListDeque {
late ListNode? _front; // 头节点 _front
late ListNode? _rear; // 尾节点 _rear
int _queSize = 0; // 双向队列的长度
LinkedListDeque() {
this._front = null;
this._rear = null;
}
/* 获取双向队列长度 */
int size() {
return this._queSize;
}
/* 判断双向队列是否为空 */
bool isEmpty() {
return size() == 0;
}
/* 入队操作 */
void push(int num, bool isFront) {
final ListNode node = ListNode(num);
if (isEmpty()) {
// 若链表为空,则令 _front_rear 都指向 node
_front = _rear = node;
} else if (isFront) {
// 队首入队操作
// 将 node 添加至链表头部
_front!.prev = node;
node.next = _front;
_front = node; // 更新头节点
} else {
// 队尾入队操作
// 将 node 添加至链表尾部
_rear!.next = node;
node.prev = _rear;
_rear = node; // 更新尾节点
}
_queSize++; // 更新队列长度
}
/* 队首入队 */
void pushFirst(int num) {
push(num, true);
}
/* 队尾入队 */
void pushLast(int num) {
push(num, false);
}
/* 出队操作 */
int? pop(bool isFront) {
// 若队列为空,直接返回 null
if (isEmpty()) {
return null;
}
final int val;
if (isFront) {
// 队首出队操作
val = _front!.val; // 暂存头节点值
// 删除头节点
ListNode? fNext = _front!.next;
if (fNext != null) {
fNext.prev = null;
_front!.next = null;
}
_front = fNext; // 更新头节点
} else {
// 队尾出队操作
val = _rear!.val; // 暂存尾节点值
// 删除尾节点
ListNode? rPrev = _rear!.prev;
if (rPrev != null) {
rPrev.next = null;
_rear!.prev = null;
}
_rear = rPrev; // 更新尾节点
}
_queSize--; // 更新队列长度
return val;
}
/* 队首出队 */
int? popFirst() {
return pop(true);
}
/* 队尾出队 */
int? popLast() {
return pop(false);
}
/* 访问队首元素 */
int? peekFirst() {
return _front?.val;
}
/* 访问队尾元素 */
int? peekLast() {
return _rear?.val;
}
/* 返回数组用于打印 */
List<int> toArray() {
ListNode? node = _front;
final List<int> res = [];
for (int i = 0; i < _queSize; i++) {
res.add(node!.val);
node = node.next;
}
return res;
}
}
```
### 基于数组的实现
与基于数组实现队列类似,我们也可以使用环形数组来实现双向队列。在队列的实现基础上,仅需增加“队首入队”和“队尾出队”的方法。
@@ -2650,6 +2786,115 @@ comments: true
[class]{ArrayDeque}-[func]{}
```
=== "Dart"
```dart title="array_deque.dart"
/* 基于环形数组实现的双向队列 */
class ArrayDeque {
late List<int> _nums; // 用于存储双向队列元素的数组
late int _front; // 队首指针,指向队首元素
late int _queSize; // 双向队列长度
/* 构造方法 */
ArrayDeque(int capacity) {
this._nums = List.filled(capacity, 0);
this._front = this._queSize = 0;
}
/* 获取双向队列的容量 */
int capacity() {
return _nums.length;
}
/* 获取双向队列的长度 */
int size() {
return _queSize;
}
/* 判断双向队列是否为空 */
bool isEmpty() {
return _queSize == 0;
}
/* 计算环形数组索引 */
int index(int i) {
// 通过取余操作实现数组首尾相连
// 当 i 越过数组尾部后,回到头部
// 当 i 越过数组头部后,回到尾部
return (i + capacity()) % capacity();
}
/* 队首入队 */
void pushFirst(int num) {
if (_queSize == capacity()) {
throw Exception("双向队列已满");
}
// 队首指针向左移动一位
// 通过取余操作,实现 _front 越过数组头部后回到尾部
_front = index(_front - 1);
// 将 num 添加至队首
_nums[_front] = num;
_queSize++;
}
/* 队尾入队 */
void pushLast(int num) {
if (_queSize == capacity()) {
throw Exception("双向队列已满");
}
// 计算尾指针,指向队尾索引 + 1
int rear = index(_front + _queSize);
// 将 num 添加至队尾
_nums[rear] = num;
_queSize++;
}
/* 队首出队 */
int popFirst() {
int num = peekFirst();
// 队首指针向右移动一位
_front = index(_front + 1);
_queSize--;
return num;
}
/* 队尾出队 */
int popLast() {
int num = peekLast();
_queSize--;
return num;
}
/* 访问队首元素 */
int peekFirst() {
if (isEmpty()) {
throw Exception("双向队列为空");
}
return _nums[_front];
}
/* 访问队尾元素 */
int peekLast() {
if (isEmpty()) {
throw Exception("双向队列为空");
}
// 计算尾元素索引
int last = index(_front + _queSize - 1);
return _nums[last];
}
/* 返回数组用于打印 */
List<int> toArray() {
// 仅转换有效长度范围内的列表元素
List<int> res = List.filled(_queSize, 0);
for (int i = 0, j = _front; i < _queSize; i++, j++) {
res[i] = _nums[index(j)];
}
return res;
}
}
```
## 5.3.3. &nbsp; 双向队列应用
双向队列兼具栈与队列的逻辑,**因此它可以实现这两者的所有应用场景,同时提供更高的自由度**。

View File

@@ -258,6 +258,12 @@ comments: true
```
=== "Dart"
```dart title="queue.dart"
```
## 5.2.2. &nbsp; 队列实现
为了实现队列,我们需要一种数据结构,可以在一端添加元素,并在另一端删除元素。因此,链表和数组都可以用来实现队列。
@@ -993,6 +999,76 @@ comments: true
}
```
=== "Dart"
```dart title="linkedlist_queue.dart"
/* 基于链表实现的队列 */
class LinkedListQueue {
ListNode? _front; // 头节点 _front
ListNode? _rear; // 尾节点 _rear
int _queSize = 0; // 队列长度
LinkedListQueue() {
_front = null;
_rear = null;
}
/* 获取队列的长度 */
int size() {
return _queSize;
}
/* 判断队列是否为空 */
bool isEmpty() {
return _queSize == 0;
}
/* 入队 */
void push(int num) {
// 尾节点后添加 num
final node = ListNode(num);
// 如果队列为空,则令头、尾节点都指向该节点
if (_front == null) {
_front = node;
_rear = node;
} else {
// 如果队列不为空,则将该节点添加到尾节点后
_rear!.next = node;
_rear = node;
}
_queSize++;
}
/* 出队 */
int pop() {
final int num = peek();
// 删除头节点
_front = _front!.next;
_queSize--;
return num;
}
/* 访问队首元素 */
int peek() {
if (_queSize == 0) {
throw Exception('队列为空');
}
return _front!.val;
}
/* 将链表转化为 Array 并返回 */
List<int> toArray() {
ListNode? node = _front;
final List<int> queue = [];
while (node != null) {
queue.add(node.val);
node = node.next;
}
return queue;
}
}
```
### 基于数组的实现
由于数组删除首元素的时间复杂度为 $O(n)$ ,这会导致出队操作效率较低。然而,我们可以采用以下巧妙方法来避免这个问题。
@@ -1759,6 +1835,77 @@ comments: true
}
```
=== "Dart"
```dart title="array_queue.dart"
/* 基于环形数组实现的队列 */
class ArrayQueue {
late List<int> _nums; // 用于储存队列元素的数组
late int _front; // 队首指针,指向队首元素
late int _queSize; // 队列长度
ArrayQueue(int capacity) {
_nums = List.filled(capacity, 0);
_front = _queSize = 0;
}
/* 获取队列的容量 */
int capaCity() {
return _nums.length;
}
/* 获取队列的长度 */
int size() {
return _queSize;
}
/* 判断队列是否为空 */
bool isEmpty() {
return _queSize == 0;
}
/* 入队 */
void push(int num) {
if (_queSize == capaCity()) {
throw Exception("队列已满");
}
// 计算尾指针,指向队尾索引 + 1
// 通过取余操作,实现 rear 越过数组尾部后回到头部
int rear = (_front + _queSize) % capaCity();
// 将 num 添加至队尾
_nums[rear] = num;
_queSize++;
}
/* 出队 */
int pop() {
int num = peek();
// 队首指针向后移动一位,若越过尾部则返回到数组头部
_front = (_front + 1) % capaCity();
_queSize--;
return num;
}
/* 访问队首元素 */
int peek() {
if (isEmpty()) {
throw Exception("队列为空");
}
return _nums[_front];
}
/* 返回 Array */
List<int> toArray() {
// 仅转换有效长度范围内的列表元素
final List<int> res = List.filled(_queSize, 0);
for (int i = 0, j = _front; i < _queSize; i++, j++) {
res[i] = _nums[j % capaCity()];
}
return res;
}
}
```
以上实现的队列仍然具有局限性,即其长度不可变。然而,这个问题不难解决,我们可以将数组替换为动态数组,从而引入扩容机制。有兴趣的同学可以尝试自行实现。
两种实现的对比结论与栈一致,在此不再赘述。

View File

@@ -256,6 +256,12 @@ comments: true
```
=== "Dart"
```dart title="stack.dart"
```
## 5.1.2. &nbsp; 栈的实现
为了深入了解栈的运行机制,我们来尝试自己实现一个栈类。
@@ -902,6 +908,66 @@ comments: true
}
```
=== "Dart"
```dart title="linkedlist_stack.dart"
/* 基于链表类实现的栈 */
class LinkedListStack {
ListNode? _stackPeek; // 将头节点作为栈顶
int _stkSize = 0; // 栈的长度
LinkedListStack() {
_stackPeek = null;
}
/* 获取栈的长度 */
int size() {
return _stkSize;
}
/* 判断栈是否为空 */
bool isEmpty() {
return _stkSize == 0;
}
/* 入栈 */
void push(int num) {
final ListNode node = ListNode(num);
node.next = _stackPeek;
_stackPeek = node;
_stkSize++;
}
/* 出栈 */
int pop() {
final int num = peek();
_stackPeek = _stackPeek!.next;
_stkSize--;
return num;
}
/* 访问栈顶元素 */
int peek() {
if (_stackPeek == null) {
throw Exception("栈为空");
}
return _stackPeek!.val;
}
/* 将链表转化为 List 并返回 */
List<int> toList() {
ListNode? node = _stackPeek;
List<int> list = [];
while (node != null) {
list.add(node.val);
node = node.next;
}
list = list.reversed.toList();
return list;
}
}
```
### 基于数组的实现
在基于「数组」实现栈时,我们可以将数组的尾部作为栈顶。在这样的设计下,入栈与出栈操作就分别对应在数组尾部添加元素与删除元素,时间复杂度都为 $O(1)$ 。
@@ -1409,6 +1475,52 @@ comments: true
}
```
=== "Dart"
```dart title="array_stack.dart"
/* 基于数组实现的栈 */
class ArrayStack {
late List<int> _stack;
ArrayStack() {
_stack = [];
}
/* 获取栈的长度 */
int size() {
return _stack.length;
}
/* 判断栈是否为空 */
bool isEmpty() {
return _stack.isEmpty;
}
/* 入栈 */
void push(int num) {
_stack.add(num);
}
/* 出栈 */
int pop() {
if (isEmpty()) {
throw Exception("栈为空");
}
return _stack.removeLast();
}
/* 访问栈顶元素 */
int peek() {
if (isEmpty()) {
throw Exception("栈为空");
}
return _stack.last;
}
/* 将栈转化为 Array 并返回 */
List<int> toArray() => _stack;
}
```
## 5.1.3. &nbsp; 两种实现对比
### 支持操作