diff --git a/codes/dart/chapter_array_and_linkedlist/array.dart b/codes/dart/chapter_array_and_linkedlist/array.dart index 628fe57b2..64209c115 100644 --- a/codes/dart/chapter_array_and_linkedlist/array.dart +++ b/codes/dart/chapter_array_and_linkedlist/array.dart @@ -86,7 +86,7 @@ int main() { /* 长度扩展 */ nums = Array().extend(nums, 3); - print('将数组长度扩展至 8, 得到nums = $nums'); + print('将数组长度扩展至 8 ,得到 nums = $nums'); /* 插入元素 */ Array().insert(nums, 6, 3); @@ -102,6 +102,6 @@ int main() { /* 查找元素 */ int index = Array().find(nums, 3); print("在 nums 中查找元素 3 ,得到索引 = $index"); - + return 0; } diff --git a/codes/dart/chapter_array_and_linkedlist/linked_list.dart b/codes/dart/chapter_array_and_linkedlist/linked_list.dart index 94553762b..6c6f1859b 100644 --- a/codes/dart/chapter_array_and_linkedlist/linked_list.dart +++ b/codes/dart/chapter_array_and_linkedlist/linked_list.dart @@ -7,7 +7,6 @@ import '../utils/list_node.dart'; import '../utils/print_util.dart'; - class LinkedList { /* 在链表的节点 n0 之后插入节点 P */ void insert(ListNode n0, ListNode P) { @@ -67,10 +66,12 @@ int main() { /* 插入节点 */ LinkedList().insert(n0, ListNode(0)); + print('插入节点后的链表为'); printLinkedList(n0); /* 删除节点 */ LinkedList().remove(n0); + print('删除节点后的链表为'); printLinkedList(n0); /* 访问节点 */ diff --git a/codes/dart/chapter_array_and_linkedlist/list.dart b/codes/dart/chapter_array_and_linkedlist/list.dart index a4853dea6..4cad11d21 100644 --- a/codes/dart/chapter_array_and_linkedlist/list.dart +++ b/codes/dart/chapter_array_and_linkedlist/list.dart @@ -7,12 +7,12 @@ /* Driver Code */ int main() { /* 初始化列表 */ - List list = [1,3,2,5,4]; + List list = [1, 3, 2, 5, 4]; print('列表 list = $list'); /* 访问元素 */ int num = list[1]; - print('访问索引 1 处的元素,得到 num = $num'); + print('访问索引 1 处的元素,得到 num = $num'); /* 更新元素 */ list[1] = 0; @@ -35,7 +35,8 @@ int main() { print('在索引 3 处插入数字 6 ,得到 list = $list'); /* 删除元素 */ - list.remove(3); + list.removeAt(3); + print('删除索引 3 处的元素,得到 list = $list'); /* 通过索引遍历列表 */ int count = 0; @@ -49,13 +50,13 @@ int main() { } /* 拼接两个列表 */ - List list1 = [6,8,7,10,9]; + List list1 = [6, 8, 7, 10, 9]; list.addAll(list1); print('将列表 list1 拼接到 list 之后,得到 list = $list'); /* 排序列表 */ list.sort(); print('排序列表后 list = $list'); - + return 0; -} \ No newline at end of file +} diff --git a/codes/dart/chapter_array_and_linkedlist/my_list.dart b/codes/dart/chapter_array_and_linkedlist/my_list.dart index 13b38f08c..8d580c269 100644 --- a/codes/dart/chapter_array_and_linkedlist/my_list.dart +++ b/codes/dart/chapter_array_and_linkedlist/my_list.dart @@ -103,11 +103,12 @@ int main() { list.add(2); list.add(5); list.add(4); - print('列表 list = ${list.toArray()},容量 = ${list.capacity()},长度 = ${list.size()}'); + print( + '列表 list = ${list.toArray()} ,容量 = ${list.capacity()} ,长度 = ${list.size()}'); /* 中间插入元素 */ list.insert(3, 6); - print('在索引 3 处插入数字 6 ,得到 list = ${list.toArray()}'); + print('在索引 3 处插入数字 6 ,得到 list = ${list.toArray()}'); /* 删除元素 */ list.remove(3); @@ -115,18 +116,19 @@ int main() { /* 访问元素 */ int num = list.get(1); - print('访问索引 1 处的元素,得到 num = $num'); + print('访问索引 1 处的元素,得到 num = $num'); /* 更新元素 */ list.set(1, 0); - print('将索引 1 处的元素更新为 0 ,得到 list = ${list.toArray()}'); + print('将索引 1 处的元素更新为 0 ,得到 list = ${list.toArray()}'); /* 测试扩容机制 */ for (var i = 0; i < 10; i++) { // 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制 list.add(i); } - print('扩容后的列表 list = ${list.toArray()},容量 = ${list.capacity()} ,长度 = ${list.size()}'); - + print( + '扩容后的列表 list = ${list.toArray()} ,容量 = ${list.capacity()} ,长度 = ${list.size()}'); + return 0; } diff --git a/codes/dart/chapter_computational_complexity/space_complexity.dart b/codes/dart/chapter_computational_complexity/space_complexity.dart index 8ce1e9cae..b8e010fcc 100644 --- a/codes/dart/chapter_computational_complexity/space_complexity.dart +++ b/codes/dart/chapter_computational_complexity/space_complexity.dart @@ -75,14 +75,14 @@ void quadratic(int n) { int quadraticRecur(int n) { if (n <= 0) return 0; List nums = List.filled(n, 0); - print('递归 n = $n 中的长度 nums 长度 = ${nums.length}'); + print('递归 n = $n 中的 nums 长度 = ${nums.length}'); return quadraticRecur(n - 1); } /* 指数阶(建立满二叉树) */ TreeNode? buildTree(int n) { if (n == 0) return null; - TreeNode root = TreeNode(n); + TreeNode root = TreeNode(0); root.left = buildTree(n - 1); root.right = buildTree(n - 1); return root; diff --git a/codes/dart/chapter_graph/graph_adjacency_matrix.dart b/codes/dart/chapter_graph/graph_adjacency_matrix.dart index af7d75fe0..b4d2d4793 100644 --- a/codes/dart/chapter_graph/graph_adjacency_matrix.dart +++ b/codes/dart/chapter_graph/graph_adjacency_matrix.dart @@ -85,8 +85,7 @@ class GraphAdjMat { /* 打印邻接矩阵 */ void printAdjMat() { - print("顶点列表 = "); - print(vertices); + print("顶点列表 = $vertices"); print("邻接矩阵 = "); printMatrix(adjMat); } diff --git a/codes/dart/chapter_heap/my_heap.dart b/codes/dart/chapter_heap/my_heap.dart index f45da8994..98ba282fe 100644 --- a/codes/dart/chapter_heap/my_heap.dart +++ b/codes/dart/chapter_heap/my_heap.dart @@ -37,7 +37,7 @@ class MaxHeap { /* 交换元素 */ void _swap(int i, int j) { int tmp = _maxHeap[i]; - _maxHeap[i] = _maxHeap[j];; + _maxHeap[i] = _maxHeap[j]; _maxHeap[j] = tmp; } @@ -127,7 +127,7 @@ void main() { /* 获取堆顶元素 */ int peek = maxHeap.peek(); - print("\n堆顶元素为:$peek"); + print("\n堆顶元素为 $peek"); /* 元素入堆 */ int val = 7; diff --git a/codes/dart/chapter_sorting/bucket_sort.dart b/codes/dart/chapter_sorting/bucket_sort.dart index 812304043..cf9f98b87 100644 --- a/codes/dart/chapter_sorting/bucket_sort.dart +++ b/codes/dart/chapter_sorting/bucket_sort.dart @@ -33,7 +33,7 @@ void bucketSort(List nums) { /* Driver Code*/ void main() { // 设输入数据为浮点数,范围为 [0, 1) - final nums = [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68]; + final nums = [0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37]; bucketSort(nums); - print('桶排序完成后 = $nums'); + print('桶排序完成后 nums = $nums'); } diff --git a/codes/dart/chapter_stack_and_queue/array_deque.dart b/codes/dart/chapter_stack_and_queue/array_deque.dart index b2265c242..c2c751fde 100644 --- a/codes/dart/chapter_stack_and_queue/array_deque.dart +++ b/codes/dart/chapter_stack_and_queue/array_deque.dart @@ -138,7 +138,7 @@ void main() { /* 获取双向队列的长度 */ final int size = deque.size(); - print("双向队列的长度 size = $size"); + print("双向队列长度 size = $size"); /* 判断双向队列是否为空 */ final bool isEmpty = deque.isEmpty(); diff --git a/codes/dart/chapter_stack_and_queue/array_queue.dart b/codes/dart/chapter_stack_and_queue/array_queue.dart index fdc4c6cab..d54bca1de 100644 --- a/codes/dart/chapter_stack_and_queue/array_queue.dart +++ b/codes/dart/chapter_stack_and_queue/array_queue.dart @@ -99,7 +99,7 @@ void main() { /* 判断队列是否为空 */ final bool isEmpty = queue.isEmpty(); - print("队列是否为空 isEmpty = $isEmpty"); + print("队列是否为空 = $isEmpty"); /* 测试环形数组 */ for (int i = 0; i < 10; i++) { diff --git a/codes/dart/chapter_stack_and_queue/deque.dart b/codes/dart/chapter_stack_and_queue/deque.dart index 54c7ed247..efbe8cc3b 100644 --- a/codes/dart/chapter_stack_and_queue/deque.dart +++ b/codes/dart/chapter_stack_and_queue/deque.dart @@ -9,13 +9,9 @@ import 'dart:collection'; void main() { /* 初始化双向队列 */ final Queue deque = Queue(); - - /* 元素入队 */ + deque.addFirst(3); deque.addLast(2); deque.addLast(5); - deque.addLast(4); - deque.addFirst(3); - deque.addFirst(1); print("双向队列 deque = $deque"); /* 访问元素 */ @@ -24,17 +20,24 @@ void main() { final int peekLast = deque.last; print("队尾元素 peekLast = $peekLast"); + /* 元素入队 */ + deque.addLast(4); + print("元素 4 队尾入队后 deque = $deque"); + deque.addFirst(1); + print("元素 1 队首入队后 deque = $deque"); + /* 元素出队 */ - final int popFirst = deque.removeFirst(); - print("队首出队元素 popFirst = $popFirst,队首出队后 deque = $deque"); + final int popLast = deque.removeLast(); - print("队尾出队元素 popLast = $popLast,队尾出队后 deque = $deque"); + print("队尾出队元素 = $popLast,队尾出队后 deque = $deque"); + final int popFirst = deque.removeFirst(); + print("队首出队元素 = $popFirst,队首出队后 deque = $deque"); /* 获取双向队列的长度 */ final int size = deque.length; - print("双向队列的长度 size = $size"); + print("双向队列长度 size = $size"); /* 判断双向队列是否为空 */ final bool isEmpty = deque.isEmpty; - print("双向队列是否为空 isEmpty = $isEmpty"); + print("双向队列是否为空 = $isEmpty"); } diff --git a/codes/dart/chapter_stack_and_queue/queue.dart b/codes/dart/chapter_stack_and_queue/queue.dart index 0182478cf..2c01db5bd 100644 --- a/codes/dart/chapter_stack_and_queue/queue.dart +++ b/codes/dart/chapter_stack_and_queue/queue.dart @@ -25,7 +25,7 @@ void main() { /* 元素出队 */ final int pop = queue.removeFirst(); - print("出队元素 pop = $pop,出队后队列 queue = $queue"); + print("出队元素 pop = $pop,出队后 queue = $queue"); /* 获取队列长度 */ final int size = queue.length; @@ -33,5 +33,5 @@ void main() { /* 判断队列是否为空 */ final bool isEmpty = queue.isEmpty; - print("队列是否为空 isEmpty = $isEmpty"); + print("队列是否为空 = $isEmpty"); } diff --git a/codes/dart/chapter_stack_and_queue/stack.dart b/codes/dart/chapter_stack_and_queue/stack.dart index 4389663b2..4fa9ee71b 100644 --- a/codes/dart/chapter_stack_and_queue/stack.dart +++ b/codes/dart/chapter_stack_and_queue/stack.dart @@ -23,8 +23,7 @@ void main() { /* 元素出栈 */ final int pop = stack.removeLast(); - print("出栈元素 pop = $pop"); - print("出栈后 stack = $stack"); + print("出栈元素 pop = $pop,出栈后 stack = $stack"); /* 获取栈的长度 */ final int size = stack.length; diff --git a/codes/dart/utils/print_util.dart b/codes/dart/utils/print_util.dart index 199bf49b7..1da111c5b 100644 --- a/codes/dart/utils/print_util.dart +++ b/codes/dart/utils/print_util.dart @@ -46,12 +46,12 @@ void printTree(TreeNode? root, [Trunk? prev = null, bool isLeft = false]) { printTree(root.right, trunk, true); if (prev == null) { - trunk.str = '---'; + trunk.str = '———'; } else if (isLeft) { - trunk.str = '/---'; + trunk.str = '/———'; prev_str = ' |'; } else { - trunk.str = '\\---'; + trunk.str = '\\———'; prev.str = prev_str; } showTrunks(trunk);