feat: add dart code for chapter_stack_and_queue and chapter_hashing (#445)

* feat: add dart code for chapter stack and queue

* feat: add dart code for chapter_hashing

* Update array_hash_map.dart
pull/446/head
liuyuxin 2 years ago committed by GitHub
parent cac38c0c93
commit 684f27cf5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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<Entry?> _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<Entry> entrySet() {
List<Entry> entrySet = [];
for (final Entry? pair in _buckets) {
if (pair != null) {
entrySet.add(pair);
}
}
return entrySet;
}
/* 获取所有键 */
List<int> keySet() {
List<int> keySet = [];
for (final Entry? pair in _buckets) {
if (pair != null) {
keySet.add(pair.key);
}
}
return keySet;
}
/* 获取所有值 */
List<String> values() {
List<String> 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"));
}

@ -0,0 +1,40 @@
/**
* File: hash_map.dart
* Created Time: 2023-03-29
* Author: liuyuxin (gvenusleo@gmail.com)
*/
/* Driver Code */
void main() {
/* 初始化哈希表 */
final Map<int, String> 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"));
}

@ -0,0 +1,146 @@
/**
* File: array_deque.dart
* Created Time: 2023-03-28
* Author: liuyuxin (gvenusleo@gmail.com)
*/
/* 基于环形数组实现的双向队列 */
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;
}
}
/* 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");
}

@ -0,0 +1,110 @@
/**
* File: array_queue.dart
* Created Time: 2023-03-28
* Author: liuyuxin (gvenusleo@gmail.com)
*/
/* 基于环形数组实现的队列 */
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;
}
}
/* 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()}");
}
}

@ -0,0 +1,77 @@
/**
* File: array_stack.dart
* Created Time: 2023-03-28
* Author: liuyuxin (gvenusleo@gmail.com)
*/
/* 基于数组实现的栈 */
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;
}
/* 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");
}

@ -0,0 +1,40 @@
/**
* File: deque.dart
* Created Time: 2023-03-28
* Author: liuyuxin (gvenusleo@gmail.com)
*/
import 'dart:collection';
void main() {
/* 初始化双向队列 */
final Queue<int> 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");
}

@ -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<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;
}
}
/* 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");
}

@ -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<int> toArray() {
ListNode? node = _front;
final List<int> 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");
}

@ -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<int> toList() {
ListNode? node = _stackPeek;
List<int> 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");
}

@ -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<int> 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");
}

@ -0,0 +1,36 @@
/**
* File: stack.dart
* Created Time: 2023-03-27
* Author: liuyuxin (gvenusleo@gmail.com)
*/
void main() {
/* 初始化栈 */
// Dart List 使
final List<int> 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");
}
Loading…
Cancel
Save