diff --git a/codes/dart/chapter_hashing/array_hash_map.dart b/codes/dart/chapter_hashing/array_hash_map.dart new file mode 100644 index 000000000..ba9512bd3 --- /dev/null +++ b/codes/dart/chapter_hashing/array_hash_map.dart @@ -0,0 +1,126 @@ +/** + * File: array_hash_map.dart + * Created Time: 2023-03-29 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +/* 键值对 int -> String */ +class Entry { + int key; + String val; + Entry(this.key, this.val); +} + +/* 基于数组简易实现的哈希表 */ +class ArrayHashMap { + late List _buckets; + + ArrayHashMap() { + // 初始化数组,包含 100 个桶 + _buckets = List.filled(100, null); + } + + /* 哈希函数 */ + int _hashFunc(int key) { + final int index = key % 100; + return index; + } + + /* 查询操作 */ + String? get(int key) { + final int index = _hashFunc(key); + final Entry? pair = _buckets[index]; + if (pair == null) { + return null; + } + return pair.val; + } + + /* 添加操作 */ + void put(int key, String val) { + final Entry pair = Entry(key, val); + final int index = _hashFunc(key); + _buckets[index] = pair; + } + + /* 删除操作 */ + void remove(int key) { + final int index = _hashFunc(key); + _buckets[index] = null; + } + + /* 获取所有键值对 */ + List entrySet() { + List entrySet = []; + for (final Entry? pair in _buckets) { + if (pair != null) { + entrySet.add(pair); + } + } + return entrySet; + } + + /* 获取所有键 */ + List keySet() { + List keySet = []; + for (final Entry? pair in _buckets) { + if (pair != null) { + keySet.add(pair.key); + } + } + return keySet; + } + + /* 获取所有值 */ + List values() { + List valueSet = []; + for (final Entry? pair in _buckets) { + if (pair != null) { + valueSet.add(pair.val); + } + } + return valueSet; + } + + /* 打印哈希表 */ + void printHashMap() { + for (final Entry kv in entrySet()) { + print("${kv.key} -> ${kv.val}"); + } + } +} + +/* Driver Code */ +void main() { + /* 初始化哈希表 */ + final ArrayHashMap map = ArrayHashMap(); + + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + map.put(12836, "小哈"); + map.put(15937, "小啰"); + map.put(16750, "小算"); + map.put(13276, "小法"); + map.put(10583, "小鸭"); + print("\n添加完成后,哈希表为\nKey -> Value"); + map.printHashMap(); + + /* 查询操作 */ + // 向哈希表输入键 key ,得到值 value + String? name = map.get(15937); + print("\n输入学号 15937 ,查询到姓名 $name"); + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + map.remove(10583); + print("\n删除 10583 后,哈希表为\nKey -> Value"); + map.printHashMap(); + + /* 遍历哈希表 */ + print("\n遍历键值对 Key->Value"); + map.entrySet().forEach((kv) => print("${kv.key} -> ${kv.val}")); + print("\n单独遍历键 Key"); + map.keySet().forEach((key) => print("$key")); + print("\n单独遍历值 Value"); + map.values().forEach((val) => print("$val")); +} diff --git a/codes/dart/chapter_hashing/hash_map.dart b/codes/dart/chapter_hashing/hash_map.dart new file mode 100644 index 000000000..d174251c4 --- /dev/null +++ b/codes/dart/chapter_hashing/hash_map.dart @@ -0,0 +1,40 @@ +/** + * File: hash_map.dart + * Created Time: 2023-03-29 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +/* Driver Code */ +void main() { + /* 初始化哈希表 */ + final Map map = {}; + + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + map[12836] = "小哈"; + map[15937] = "小啰"; + map[16750] = "小算"; + map[13276] = "小法"; + map[10583] = "小鸭"; + print("\n添加完成后,哈希表为\nKey -> Value"); + map.forEach((key, value) => print("$key -> $value")); + + /* 查询操作 */ + // 向哈希表输入键 key ,得到值 value + final String? name = map[15937]; + print("\n输入学号 15937 ,查询到姓名 $name"); + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + map.remove(10583); + print("\n删除 10583 后,哈希表为\nKey -> Value"); + map.forEach((key, value) => print("$key -> $value")); + + /* 遍历哈希表 */ + print("\n遍历键值对 Key->Value"); + map.forEach((key, value) => print("$key -> $value")); + print("\n单独遍历键 Key"); + map.forEach((key, value) => print("$key")); + print("\n单独遍历值 Value"); + map.forEach((key, value) => print("$value")); +} diff --git a/codes/dart/chapter_stack_and_queue/array_deque.dart b/codes/dart/chapter_stack_and_queue/array_deque.dart new file mode 100644 index 000000000..b2265c242 --- /dev/null +++ b/codes/dart/chapter_stack_and_queue/array_deque.dart @@ -0,0 +1,146 @@ +/** + * File: array_deque.dart + * Created Time: 2023-03-28 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +/* 基于环形数组实现的双向队列 */ +class ArrayDeque { + late List _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 toArray() { + // 仅转换有效长度范围内的列表元素 + List res = List.filled(_queSize, 0); + for (int i = 0, j = _front; i < _queSize; i++, j++) { + res[i] = _nums[index(j)]; + } + return res; + } +} + +/* Driver Code */ +void main() { + /* 初始化双向队列 */ + final ArrayDeque deque = ArrayDeque(10); + deque.pushLast(3); + deque.pushLast(2); + deque.pushLast(5); + print("双向队列 deque = ${deque.toArray()}"); + + /* 访问元素 */ + final int peekFirst = deque.peekFirst(); + print("队首元素 peekFirst = $peekFirst"); + final int peekLast = deque.peekLast(); + print("队尾元素 peekLast = $peekLast"); + + /* 元素入队 */ + deque.pushLast(4); + print("元素 4 队尾入队后 deque = ${deque.toArray()}"); + deque.pushFirst(1); + print("元素 1 队首入队后 deque = ${deque.toArray()}"); + + /* 元素出队 */ + final int popLast = deque.popLast(); + print("队尾出队元素 = $popLast,队尾出队后 deque = ${deque.toArray()}"); + final int popFirst = deque.popFirst(); + print("队首出队元素 = $popFirst,队首出队后 deque = ${deque.toArray()}"); + + /* 获取双向队列的长度 */ + final int size = deque.size(); + print("双向队列的长度 size = $size"); + + /* 判断双向队列是否为空 */ + final bool isEmpty = deque.isEmpty(); + print("双向队列是否为空 = $isEmpty"); +} diff --git a/codes/dart/chapter_stack_and_queue/array_queue.dart b/codes/dart/chapter_stack_and_queue/array_queue.dart new file mode 100644 index 000000000..fdc4c6cab --- /dev/null +++ b/codes/dart/chapter_stack_and_queue/array_queue.dart @@ -0,0 +1,110 @@ +/** + * File: array_queue.dart + * Created Time: 2023-03-28 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +/* 基于环形数组实现的队列 */ +class ArrayQueue { + late List _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 toArray() { + // 仅转换有效长度范围内的列表元素 + final List res = List.filled(_queSize, 0); + for (int i = 0, j = _front; i < _queSize; i++, j++) { + res[i] = _nums[j % capaCity()]; + } + return res; + } +} + +/* Driver Code */ +void main() { + /* 初始化队列 */ + final int capacity = 10; + final ArrayQueue queue = ArrayQueue(capacity); + + /* 元素入队 */ + queue.push(1); + queue.push(3); + queue.push(2); + queue.push(5); + queue.push(4); + print("队列 queue = ${queue.toArray()}"); + + /* 访问队首元素 */ + final int peek = queue.peek(); + print("队首元素 peek = $peek"); + + /* 元素出队 */ + final int pop = queue.pop(); + print("出队元素 pop = $pop,出队后 queue = ${queue.toArray()}"); + + /* 获取队列长度 */ + final int size = queue.size(); + print("队列长度 size = $size"); + + /* 判断队列是否为空 */ + final bool isEmpty = queue.isEmpty(); + print("队列是否为空 isEmpty = $isEmpty"); + + /* 测试环形数组 */ + for (int i = 0; i < 10; i++) { + queue.push(i); + queue.pop(); + print("第 $i 轮入队 + 出队后 queue = ${queue.toArray()}"); + } +} diff --git a/codes/dart/chapter_stack_and_queue/array_stack.dart b/codes/dart/chapter_stack_and_queue/array_stack.dart new file mode 100644 index 000000000..f8621c537 --- /dev/null +++ b/codes/dart/chapter_stack_and_queue/array_stack.dart @@ -0,0 +1,77 @@ +/** + * File: array_stack.dart + * Created Time: 2023-03-28 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +/* 基于数组实现的栈 */ +class ArrayStack { + late List _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 toArray() => _stack; +} + +/* Driver Code */ +void main() { + /* 初始化栈 */ + final ArrayStack stack = ArrayStack(); + + /* 元素入栈 */ + stack.push(1); + stack.push(3); + stack.push(2); + stack.push(5); + stack.push(4); + print("栈 stack = ${stack.toArray()}"); + + /* 访问栈顶元素 */ + final int peek = stack.peek(); + print("栈顶元素 peek = $peek"); + + /* 元素出栈 */ + final int pop = stack.pop(); + print("出栈元素 pop = $pop,出栈后 stack = ${stack.toArray()}"); + + /* 获取栈的长度 */ + final int size = stack.size(); + print("栈的长度 size = $size"); + + /* 判断是否为空 */ + final bool isEmpty = stack.isEmpty(); + print("栈是否为空 = $isEmpty"); +} diff --git a/codes/dart/chapter_stack_and_queue/deque.dart b/codes/dart/chapter_stack_and_queue/deque.dart new file mode 100644 index 000000000..54c7ed247 --- /dev/null +++ b/codes/dart/chapter_stack_and_queue/deque.dart @@ -0,0 +1,40 @@ +/** + * File: deque.dart + * Created Time: 2023-03-28 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +import 'dart:collection'; + +void main() { + /* 初始化双向队列 */ + final Queue deque = Queue(); + + /* 元素入队 */ + deque.addLast(2); + deque.addLast(5); + deque.addLast(4); + deque.addFirst(3); + deque.addFirst(1); + print("双向队列 deque = $deque"); + + /* 访问元素 */ + final int peekFirst = deque.first; + print("队首元素 peekFirst = $peekFirst"); + final int peekLast = deque.last; + print("队尾元素 peekLast = $peekLast"); + + /* 元素出队 */ + final int popFirst = deque.removeFirst(); + print("队首出队元素 popFirst = $popFirst,队首出队后 deque = $deque"); + final int popLast = deque.removeLast(); + print("队尾出队元素 popLast = $popLast,队尾出队后 deque = $deque"); + + /* 获取双向队列的长度 */ + final int size = deque.length; + print("双向队列的长度 size = $size"); + + /* 判断双向队列是否为空 */ + final bool isEmpty = deque.isEmpty; + print("双向队列是否为空 isEmpty = $isEmpty"); +} diff --git a/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart b/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart new file mode 100644 index 000000000..146535c81 --- /dev/null +++ b/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart @@ -0,0 +1,167 @@ +/** + * File: linkedlist_deque.dart + * Created Time: 2023-03-28 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +/* 双向链表结点 */ +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 toArray() { + ListNode? node = _front; + final List res = []; + for (int i = 0; i < _queSize; i++) { + res.add(node!.val); + node = node.next; + } + return res; + } +} + +/* Driver Code */ +void main() { + /* 初始化双向队列 */ + final LinkedListDeque deque = LinkedListDeque(); + deque.pushLast(3); + deque.pushLast(2); + deque.pushLast(5); + print("双向队列 deque = ${deque.toArray()}"); + + /* 访问元素 */ + int? peekFirst = deque.peekFirst(); + print("队首元素 peekFirst = $peekFirst"); + int? peekLast = deque.peekLast(); + print("队尾元素 peekLast = $peekLast"); + + /* 元素入队 */ + deque.pushLast(4); + print("元素 4 队尾入队后 deque = ${deque.toArray()}"); + deque.pushFirst(1); + print("元素 1 队首入队后 deque = ${deque.toArray()}"); + + /* 元素出队 */ + int? popLast = deque.popLast(); + print("队尾出队元素 = $popLast,队尾出队后 deque = ${deque.toArray()}"); + int? popFirst = deque.popFirst(); + print("队首出队元素 = $popFirst,队首出队后 deque = ${deque.toArray()}"); + + /* 获取双向队列的长度 */ + int size = deque.size(); + print("双向队列长度 size = $size"); + + /* 判断双向队列是否为空 */ + bool isEmpty = deque.isEmpty(); + print("双向队列是否为空 = $isEmpty"); +} diff --git a/codes/dart/chapter_stack_and_queue/linkedlist_queue.dart b/codes/dart/chapter_stack_and_queue/linkedlist_queue.dart new file mode 100644 index 000000000..a143fc89c --- /dev/null +++ b/codes/dart/chapter_stack_and_queue/linkedlist_queue.dart @@ -0,0 +1,103 @@ +/** + * File: linkedlist_queue.dart + * Created Time: 2023-03-28 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +import '../utils/list_node.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 toArray() { + ListNode? node = _front; + final List queue = []; + while (node != null) { + queue.add(node.val); + node = node.next; + } + return queue; + } +} + +/* Driver Code */ +void main() { + /* 初始化队列 */ + final queue = LinkedListQueue(); + + /* 元素入队 */ + queue.push(1); + queue.push(3); + queue.push(2); + queue.push(5); + queue.push(4); + print("队列 queue = ${queue.toArray()}"); + + /* 访问队首元素 */ + final int peek = queue.peek(); + print("队首元素 peek = $peek"); + + /* 元素出队 */ + final int pop = queue.pop(); + print("出队元素 pop = $pop,出队后 queue = ${queue.toArray()}"); + + /* 获取队列的长度 */ + final int size = queue.size(); + print("队列长度 size = $size"); + + /* 判断队列是否为空 */ + final bool isEmpty = queue.isEmpty(); + print("队列是否为空 = $isEmpty"); +} diff --git a/codes/dart/chapter_stack_and_queue/linkedlist_stack.dart b/codes/dart/chapter_stack_and_queue/linkedlist_stack.dart new file mode 100644 index 000000000..a1dbd46b7 --- /dev/null +++ b/codes/dart/chapter_stack_and_queue/linkedlist_stack.dart @@ -0,0 +1,93 @@ +/** + * File: linkedlist_stack.dart + * Created Time: 2023-03-27 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +import '../utils/list_node.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 toList() { + ListNode? node = _stackPeek; + List list = []; + while (node != null) { + list.add(node.val); + node = node.next; + } + list = list.reversed.toList(); + return list; + } +} + +/* Driver Code */ +void main() { + /* 初始化栈 */ + final LinkedListStack stack = LinkedListStack(); + + /* 元素入栈 */ + stack.push(1); + stack.push(3); + stack.push(2); + stack.push(5); + stack.push(4); + print("栈 stack = ${stack.toList()}"); + + /* 访问栈顶元素 */ + final int peek = stack.peek(); + print("栈顶元素 peek = $peek"); + + /* 元素出栈 */ + final int pop = stack.pop(); + print("出栈元素 pop = $pop,出栈后 stack = ${stack.toList()}"); + + /* 获取栈的长度 */ + final int size = stack.size(); + print("栈的长度 size = $size"); + + /* 判断是否为空 */ + final bool isEmpty = stack.isEmpty(); + print("栈是否为空 = $isEmpty"); +} diff --git a/codes/dart/chapter_stack_and_queue/queue.dart b/codes/dart/chapter_stack_and_queue/queue.dart new file mode 100644 index 000000000..115cb5fc8 --- /dev/null +++ b/codes/dart/chapter_stack_and_queue/queue.dart @@ -0,0 +1,37 @@ +/** + * File: queue:dart + * Created Time: 2023-03-28 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +import 'dart:collection'; + +void main() { + /* 初始化队列 */ + // 在 Dart 中,一般将双向队列类 Queue 看作队列使用 + final Queue queue = Queue(); + + /* 元素入队 */ + queue.add(1); + queue.add(3); + queue.add(2); + queue.add(5); + queue.add(4); + print("队列 queue = $queue"); + + /* 访问队首元素 */ + final int peek = queue.first; + print("队首元素 peek = $peek"); + + /* 元素出队 */ + final int pop = queue.removeFirst(); + print("出队元素 pop = $pop,出队后队列 queue = $queue"); + + /* 获取队列长度 */ + final int size = queue.length; + print("队列长度 size = $size"); + + /* 判断队列是否为空 */ + final bool isEmpty = queue.isEmpty; + print("队列是否为空 isEmpty = $isEmpty"); +} diff --git a/codes/dart/chapter_stack_and_queue/stack.dart b/codes/dart/chapter_stack_and_queue/stack.dart new file mode 100644 index 000000000..4389663b2 --- /dev/null +++ b/codes/dart/chapter_stack_and_queue/stack.dart @@ -0,0 +1,36 @@ +/** + * File: stack.dart + * Created Time: 2023-03-27 + * Author: liuyuxin (gvenusleo@gmail.com) + */ + +void main() { + /* 初始化栈 */ + // Dart 没有内置的栈类,可以把 List 当作栈来使用 + final List stack = []; + + /* 元素入栈 */ + stack.add(1); + stack.add(3); + stack.add(2); + stack.add(5); + stack.add(4); + print("栈 stack = $stack"); + + /* 访问栈顶元素 */ + final int peek = stack.last; + print("栈顶元素 peek = $peek"); + + /* 元素出栈 */ + final int pop = stack.removeLast(); + print("出栈元素 pop = $pop"); + print("出栈后 stack = $stack"); + + /* 获取栈的长度 */ + final int size = stack.length; + print("栈的长度 size = $size"); + + /* 判断是否为空 */ + final bool isEmpty = stack.isEmpty; + print("栈是否为空 = $isEmpty"); +}