mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
feat: modify some Dart codes and add Dart code blocks to the docs (#543)
This commit is contained in:
@@ -286,7 +286,30 @@
|
||||
=== "Dart"
|
||||
|
||||
```dart title="deque.dart"
|
||||
/* 初始化双向队列 */
|
||||
// 在 Dart 中,Queue 被定义为双向队列
|
||||
Queue<int> deque = Queue<int>();
|
||||
|
||||
/* 元素入队 */
|
||||
deque.addLast(2); // 添加至队尾
|
||||
deque.addLast(5);
|
||||
deque.addLast(4);
|
||||
deque.addFirst(3); // 添加至队首
|
||||
deque.addFirst(1);
|
||||
|
||||
/* 访问元素 */
|
||||
int peekFirst = deque.first; // 队首元素
|
||||
int peekLast = deque.last; // 队尾元素
|
||||
|
||||
/* 元素出队 */
|
||||
int popFirst = deque.removeFirst(); // 队首元素出队
|
||||
int popLast = deque.removeLast(); // 队尾元素出队
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
int size = deque.length;
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
bool isEmpty = deque.isEmpty;W
|
||||
```
|
||||
|
||||
## 双向队列实现 *
|
||||
|
||||
@@ -255,7 +255,28 @@
|
||||
=== "Dart"
|
||||
|
||||
```dart title="queue.dart"
|
||||
/* 初始化队列 */
|
||||
// 在 Dart 中,队列类 Qeque 是双向队列,也可作为队列使用
|
||||
Queue<int> queue = Queue();
|
||||
|
||||
/* 元素入队 */
|
||||
queue.add(1);
|
||||
queue.add(3);
|
||||
queue.add(2);
|
||||
queue.add(5);
|
||||
queue.add(4);
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peek = queue.first;
|
||||
|
||||
/* 元素出队 */
|
||||
int pop = queue.removeFirst();
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int size = queue.length;
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool isEmpty = queue.isEmpty;
|
||||
```
|
||||
|
||||
## 队列实现
|
||||
|
||||
@@ -253,7 +253,28 @@
|
||||
=== "Dart"
|
||||
|
||||
```dart title="stack.dart"
|
||||
/* 初始化栈 */
|
||||
// Dart 没有内置的栈类,可以把 List 当作栈来使用
|
||||
List<int> stack = [];
|
||||
|
||||
/* 元素入栈 */
|
||||
stack.add(1);
|
||||
stack.add(3);
|
||||
stack.add(2);
|
||||
stack.add(5);
|
||||
stack.add(4);
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int peek = stack.last;
|
||||
|
||||
/* 元素出栈 */
|
||||
int pop = stack.removeLast();
|
||||
|
||||
/* 获取栈的长度 */
|
||||
int size = stack.length;
|
||||
|
||||
/* 判断是否为空 */
|
||||
bool isEmpty = stack.isEmpty;
|
||||
```
|
||||
|
||||
## 栈的实现
|
||||
|
||||
Reference in New Issue
Block a user