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.dartpull/446/head
parent
cac38c0c93
commit
684f27cf5e
@ -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,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");
|
||||
}
|
Loading…
Reference in new issue