Skip to content

Commit

Permalink
feat: add dart code for chapter_stack_and_queue and chapter_hashing (#…
Browse files Browse the repository at this point in the history
…445)

* feat: add dart code for chapter stack and queue

* feat: add dart code for chapter_hashing

* Update array_hash_map.dart
  • Loading branch information
gvenusleo authored Mar 29, 2023
1 parent cac38c0 commit 684f27c
Show file tree
Hide file tree
Showing 11 changed files with 975 additions and 0 deletions.
126 changes: 126 additions & 0 deletions codes/dart/chapter_hashing/array_hash_map.dart
Original file line number Diff line number Diff line change
@@ -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"));
}
40 changes: 40 additions & 0 deletions codes/dart/chapter_hashing/hash_map.dart
Original file line number Diff line number Diff line change
@@ -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"));
}
146 changes: 146 additions & 0 deletions codes/dart/chapter_stack_and_queue/array_deque.dart
Original file line number Diff line number Diff line change
@@ -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");
}
Loading

0 comments on commit 684f27c

Please sign in to comment.