mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-24 02:21:30 +08:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
146
en/codes/dart/chapter_stack_and_queue/array_deque.dart
Normal file
146
en/codes/dart/chapter_stack_and_queue/array_deque.dart
Normal file
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* File: array_deque.dart
|
||||
* Created Time: 2023-03-28
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
/* Double-ended queue based on circular array implementation */
|
||||
class ArrayDeque {
|
||||
late List<int> _nums; // Array for storing double-ended queue elements
|
||||
late int _front; // Front pointer, points to the front of the queue element
|
||||
late int _queSize; // Double-ended queue length
|
||||
|
||||
/* Constructor */
|
||||
ArrayDeque(int capacity) {
|
||||
this._nums = List.filled(capacity, 0);
|
||||
this._front = this._queSize = 0;
|
||||
}
|
||||
|
||||
/* Get the capacity of the double-ended queue */
|
||||
int capacity() {
|
||||
return _nums.length;
|
||||
}
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size() {
|
||||
return _queSize;
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
bool isEmpty() {
|
||||
return _queSize == 0;
|
||||
}
|
||||
|
||||
/* Calculate circular array index */
|
||||
int index(int i) {
|
||||
// Use modulo operation to wrap the array head and tail together
|
||||
// When i passes the tail of the array, return to the head
|
||||
// When i passes the head of the array, return to the tail
|
||||
return (i + capacity()) % capacity();
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
void pushFirst(int _num) {
|
||||
if (_queSize == capacity()) {
|
||||
throw Exception("Double-ended queue is full");
|
||||
}
|
||||
// Use modulo operation to wrap front around to the tail after passing the head of the array
|
||||
// Use modulo operation to wrap _front from array head back to tail
|
||||
_front = index(_front - 1);
|
||||
// Add _num to queue front
|
||||
_nums[_front] = _num;
|
||||
_queSize++;
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
void pushLast(int _num) {
|
||||
if (_queSize == capacity()) {
|
||||
throw Exception("Double-ended queue is full");
|
||||
}
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
int rear = index(_front + _queSize);
|
||||
// Add _num to queue rear
|
||||
_nums[rear] = _num;
|
||||
_queSize++;
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
int popFirst() {
|
||||
int _num = peekFirst();
|
||||
// Move front pointer right by one
|
||||
_front = index(_front + 1);
|
||||
_queSize--;
|
||||
return _num;
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
int popLast() {
|
||||
int _num = peekLast();
|
||||
_queSize--;
|
||||
return _num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
int peekFirst() {
|
||||
if (isEmpty()) {
|
||||
throw Exception("Deque is empty");
|
||||
}
|
||||
return _nums[_front];
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int peekLast() {
|
||||
if (isEmpty()) {
|
||||
throw Exception("Deque is empty");
|
||||
}
|
||||
// Initialize double-ended queue
|
||||
int last = index(_front + _queSize - 1);
|
||||
return _nums[last];
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
List<int> toArray() {
|
||||
// Elements enqueue
|
||||
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() {
|
||||
/* Get the length of the double-ended queue */
|
||||
final ArrayDeque deque = ArrayDeque(10);
|
||||
deque.pushLast(3);
|
||||
deque.pushLast(2);
|
||||
deque.pushLast(5);
|
||||
print("Deque deque = ${deque.toArray()}");
|
||||
|
||||
/* Update element */
|
||||
final int peekFirst = deque.peekFirst();
|
||||
print("Front element peekFirst = $peekFirst");
|
||||
final int peekLast = deque.peekLast();
|
||||
print("Rear element peekLast = $peekLast");
|
||||
|
||||
/* Elements enqueue */
|
||||
deque.pushLast(4);
|
||||
print("After element 4 enqueues at rear, deque = ${deque.toArray()}");
|
||||
deque.pushFirst(1);
|
||||
print("After element 1 enqueues at front, deque = ${deque.toArray()}");
|
||||
|
||||
/* Element dequeue */
|
||||
final int popLast = deque.popLast();
|
||||
print("Dequeue rear element = $popLast, after rear dequeue deque = ${deque.toArray()}");
|
||||
final int popFirst = deque.popFirst();
|
||||
print("Dequeue front element = $popFirst, after front dequeue deque = ${deque.toArray()}");
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
final int size = deque.size();
|
||||
print("Deque length size = $size");
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
final bool isEmpty = deque.isEmpty();
|
||||
print("Is deque empty = $isEmpty");
|
||||
}
|
||||
110
en/codes/dart/chapter_stack_and_queue/array_queue.dart
Normal file
110
en/codes/dart/chapter_stack_and_queue/array_queue.dart
Normal file
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* File: array_queue.dart
|
||||
* Created Time: 2023-03-28
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
/* Queue based on circular array implementation */
|
||||
class ArrayQueue {
|
||||
late List<int> _nums; // Array for storing queue elements
|
||||
late int _front; // Front pointer, points to the front of the queue element
|
||||
late int _queSize; // Queue length
|
||||
|
||||
ArrayQueue(int capacity) {
|
||||
_nums = List.filled(capacity, 0);
|
||||
_front = _queSize = 0;
|
||||
}
|
||||
|
||||
/* Get the capacity of the queue */
|
||||
int capaCity() {
|
||||
return _nums.length;
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size() {
|
||||
return _queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
bool isEmpty() {
|
||||
return _queSize == 0;
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
void push(int _num) {
|
||||
if (_queSize == capaCity()) {
|
||||
throw Exception("Queue is full");
|
||||
}
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
// Add num to the rear of the queue
|
||||
int rear = (_front + _queSize) % capaCity();
|
||||
// Add _num to queue rear
|
||||
_nums[rear] = _num;
|
||||
_queSize++;
|
||||
}
|
||||
|
||||
/* Dequeue */
|
||||
int pop() {
|
||||
int _num = peek();
|
||||
// Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
_front = (_front + 1) % capaCity();
|
||||
_queSize--;
|
||||
return _num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
int peek() {
|
||||
if (isEmpty()) {
|
||||
throw Exception("Queue is empty");
|
||||
}
|
||||
return _nums[_front];
|
||||
}
|
||||
|
||||
/* Return Array */
|
||||
List<int> toArray() {
|
||||
// Elements enqueue
|
||||
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() {
|
||||
/* Access front of the queue element */
|
||||
final int capacity = 10;
|
||||
final ArrayQueue queue = ArrayQueue(capacity);
|
||||
|
||||
/* Elements enqueue */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
print("Queue queue = ${queue.toArray()}");
|
||||
|
||||
/* Return list for printing */
|
||||
final int peek = queue.peek();
|
||||
print("Front element peek = $peek");
|
||||
|
||||
/* Element dequeue */
|
||||
final int pop = queue.pop();
|
||||
print("Dequeue element pop = $pop, after dequeue queue = ${queue.toArray()}");
|
||||
|
||||
/* Get queue length */
|
||||
final int size = queue.size();
|
||||
print("Queue length size = $size");
|
||||
|
||||
/* Check if the queue is empty */
|
||||
final bool isEmpty = queue.isEmpty();
|
||||
print("Is queue empty = $isEmpty");
|
||||
|
||||
/* Test circular array */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue.push(i);
|
||||
queue.pop();
|
||||
print("After round $i enqueue + dequeue, queue = ${queue.toArray()}");
|
||||
}
|
||||
}
|
||||
77
en/codes/dart/chapter_stack_and_queue/array_stack.dart
Normal file
77
en/codes/dart/chapter_stack_and_queue/array_stack.dart
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* File: array_stack.dart
|
||||
* Created Time: 2023-03-28
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
/* Stack based on array implementation */
|
||||
class ArrayStack {
|
||||
late List<int> _stack;
|
||||
ArrayStack() {
|
||||
_stack = [];
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size() {
|
||||
return _stack.length;
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
bool isEmpty() {
|
||||
return _stack.isEmpty;
|
||||
}
|
||||
|
||||
/* Push */
|
||||
void push(int _num) {
|
||||
_stack.add(_num);
|
||||
}
|
||||
|
||||
/* Pop */
|
||||
int pop() {
|
||||
if (isEmpty()) {
|
||||
throw Exception("Stack is empty");
|
||||
}
|
||||
return _stack.removeLast();
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
int peek() {
|
||||
if (isEmpty()) {
|
||||
throw Exception("Stack is empty");
|
||||
}
|
||||
return _stack.last;
|
||||
}
|
||||
|
||||
/* Convert stack to Array and return */
|
||||
List<int> toArray() => _stack;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
/* Access top of the stack element */
|
||||
final ArrayStack stack = ArrayStack();
|
||||
|
||||
/* Elements push onto stack */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
print("Stack stack = ${stack.toArray()}");
|
||||
|
||||
/* Return list for printing */
|
||||
final int peek = stack.peek();
|
||||
print("Top element peek = $peek");
|
||||
|
||||
/* Element pop from stack */
|
||||
final int pop = stack.pop();
|
||||
print("Pop element pop = $pop, after pop stack = ${stack.toArray()}");
|
||||
|
||||
/* Get the length of the stack */
|
||||
final int size = stack.size();
|
||||
print("Stack length size = $size");
|
||||
|
||||
/* Check if empty */
|
||||
final bool isEmpty = stack.isEmpty();
|
||||
print("Is stack empty = $isEmpty");
|
||||
}
|
||||
42
en/codes/dart/chapter_stack_and_queue/deque.dart
Normal file
42
en/codes/dart/chapter_stack_and_queue/deque.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* File: deque.dart
|
||||
* Created Time: 2023-03-28
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
import 'dart:collection';
|
||||
|
||||
void main() {
|
||||
/* Get the length of the double-ended queue */
|
||||
final Queue<int> deque = Queue();
|
||||
deque.addFirst(3);
|
||||
deque.addLast(2);
|
||||
deque.addLast(5);
|
||||
print("Deque deque = $deque");
|
||||
|
||||
/* Update element */
|
||||
final int peekFirst = deque.first;
|
||||
print("Front element peekFirst = $peekFirst");
|
||||
final int peekLast = deque.last;
|
||||
print("Rear element peekLast = $peekLast");
|
||||
|
||||
/* Elements enqueue */
|
||||
deque.addLast(4);
|
||||
print("After element 4 enqueues at rear, deque = $deque");
|
||||
deque.addFirst(1);
|
||||
print("After element 1 enqueues at front, deque = $deque");
|
||||
|
||||
/* Element dequeue */
|
||||
final int popLast = deque.removeLast();
|
||||
print("Dequeue rear element = $popLast, after rear dequeue deque = $deque");
|
||||
final int popFirst = deque.removeFirst();
|
||||
print("Dequeue front element = $popFirst, after front dequeue deque = $deque");
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
final int size = deque.length;
|
||||
print("Deque length size = $size");
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
final bool isEmpty = deque.isEmpty;
|
||||
print("Is deque empty = $isEmpty");
|
||||
}
|
||||
167
en/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart
Normal file
167
en/codes/dart/chapter_stack_and_queue/linkedlist_deque.dart
Normal file
@@ -0,0 +1,167 @@
|
||||
/**
|
||||
* File: linkedlist_deque.dart
|
||||
* Created Time: 2023-03-28
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
/* Doubly linked list node */
|
||||
class ListNode {
|
||||
int val; // Node value
|
||||
ListNode? next; // Successor node reference
|
||||
ListNode? prev; // Predecessor node reference
|
||||
|
||||
ListNode(this.val, {this.next, this.prev});
|
||||
}
|
||||
|
||||
/* Deque implemented based on doubly linked list */
|
||||
class LinkedListDeque {
|
||||
late ListNode? _front; // Head node _front
|
||||
late ListNode? _rear; // Tail node _rear
|
||||
int _queSize = 0; // Length of the double-ended queue
|
||||
|
||||
LinkedListDeque() {
|
||||
this._front = null;
|
||||
this._rear = null;
|
||||
}
|
||||
|
||||
/* Get deque length */
|
||||
int size() {
|
||||
return this._queSize;
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* Enqueue operation */
|
||||
void push(int _num, bool isFront) {
|
||||
final ListNode node = ListNode(_num);
|
||||
if (isEmpty()) {
|
||||
// If list is empty, let both _front and _rear point to node
|
||||
_front = _rear = node;
|
||||
} else if (isFront) {
|
||||
// Front of the queue enqueue operation
|
||||
// Add node to the head of the linked list
|
||||
_front!.prev = node;
|
||||
node.next = _front;
|
||||
_front = node; // Update head node
|
||||
} else {
|
||||
// Rear of the queue enqueue operation
|
||||
// Add node to the tail of the linked list
|
||||
_rear!.next = node;
|
||||
node.prev = _rear;
|
||||
_rear = node; // Update tail node
|
||||
}
|
||||
_queSize++; // Update queue length
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
void pushFirst(int _num) {
|
||||
push(_num, true);
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
void pushLast(int _num) {
|
||||
push(_num, false);
|
||||
}
|
||||
|
||||
/* Dequeue operation */
|
||||
int? pop(bool isFront) {
|
||||
// If queue is empty, return null directly
|
||||
if (isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
final int val;
|
||||
if (isFront) {
|
||||
// Temporarily store head node value
|
||||
val = _front!.val; // Delete head node
|
||||
// Delete head node
|
||||
ListNode? fNext = _front!.next;
|
||||
if (fNext != null) {
|
||||
fNext.prev = null;
|
||||
_front!.next = null;
|
||||
}
|
||||
_front = fNext; // Update head node
|
||||
} else {
|
||||
// Temporarily store tail node value
|
||||
val = _rear!.val; // Delete tail node
|
||||
// Update tail node
|
||||
ListNode? rPrev = _rear!.prev;
|
||||
if (rPrev != null) {
|
||||
rPrev.next = null;
|
||||
_rear!.prev = null;
|
||||
}
|
||||
_rear = rPrev; // Update tail node
|
||||
}
|
||||
_queSize--; // Update queue length
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
int? popFirst() {
|
||||
return pop(true);
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
int? popLast() {
|
||||
return pop(false);
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
int? peekFirst() {
|
||||
return _front?.val;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int? peekLast() {
|
||||
return _rear?.val;
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
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() {
|
||||
/* Get the length of the double-ended queue */
|
||||
final LinkedListDeque deque = LinkedListDeque();
|
||||
deque.pushLast(3);
|
||||
deque.pushLast(2);
|
||||
deque.pushLast(5);
|
||||
print("Deque deque = ${deque.toArray()}");
|
||||
|
||||
/* Update element */
|
||||
int? peekFirst = deque.peekFirst();
|
||||
print("Front element peekFirst = $peekFirst");
|
||||
int? peekLast = deque.peekLast();
|
||||
print("Rear element peekLast = $peekLast");
|
||||
|
||||
/* Elements enqueue */
|
||||
deque.pushLast(4);
|
||||
print("After element 4 enqueues at rear, deque = ${deque.toArray()}");
|
||||
deque.pushFirst(1);
|
||||
print("After element 1 enqueues at front, deque = ${deque.toArray()}");
|
||||
|
||||
/* Element dequeue */
|
||||
int? popLast = deque.popLast();
|
||||
print("Dequeue rear element = $popLast, after rear dequeue deque = ${deque.toArray()}");
|
||||
int? popFirst = deque.popFirst();
|
||||
print("Dequeue front element = $popFirst, after front dequeue deque = ${deque.toArray()}");
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size = deque.size();
|
||||
print("Deque length size = $size");
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
bool isEmpty = deque.isEmpty();
|
||||
print("Is deque empty = $isEmpty");
|
||||
}
|
||||
103
en/codes/dart/chapter_stack_and_queue/linkedlist_queue.dart
Normal file
103
en/codes/dart/chapter_stack_and_queue/linkedlist_queue.dart
Normal file
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* File: linkedlist_queue.dart
|
||||
* Created Time: 2023-03-28
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
import '../utils/list_node.dart';
|
||||
|
||||
/* Queue based on linked list implementation */
|
||||
class LinkedListQueue {
|
||||
ListNode? _front; // Head node _front
|
||||
ListNode? _rear; // Tail node _rear
|
||||
int _queSize = 0; // Queue length
|
||||
|
||||
LinkedListQueue() {
|
||||
_front = null;
|
||||
_rear = null;
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size() {
|
||||
return _queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
bool isEmpty() {
|
||||
return _queSize == 0;
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
void push(int _num) {
|
||||
// Add _num after tail node
|
||||
final node = ListNode(_num);
|
||||
// If the queue is empty, make both front and rear point to the node
|
||||
if (_front == null) {
|
||||
_front = node;
|
||||
_rear = node;
|
||||
} else {
|
||||
// If the queue is not empty, add the node after the tail node
|
||||
_rear!.next = node;
|
||||
_rear = node;
|
||||
}
|
||||
_queSize++;
|
||||
}
|
||||
|
||||
/* Dequeue */
|
||||
int pop() {
|
||||
final int _num = peek();
|
||||
// Delete head node
|
||||
_front = _front!.next;
|
||||
_queSize--;
|
||||
return _num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
int peek() {
|
||||
if (_queSize == 0) {
|
||||
throw Exception('Queue is empty');
|
||||
}
|
||||
return _front!.val;
|
||||
}
|
||||
|
||||
/* Convert linked list to Array and return */
|
||||
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() {
|
||||
/* Access front of the queue element */
|
||||
final queue = LinkedListQueue();
|
||||
|
||||
/* Elements enqueue */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
print("Queue queue = ${queue.toArray()}");
|
||||
|
||||
/* Return list for printing */
|
||||
final int peek = queue.peek();
|
||||
print("Front element peek = $peek");
|
||||
|
||||
/* Element dequeue */
|
||||
final int pop = queue.pop();
|
||||
print("Dequeue element pop = $pop, after dequeue queue = ${queue.toArray()}");
|
||||
|
||||
/* Get the length of the queue */
|
||||
final int size = queue.size();
|
||||
print("Queue length size = $size");
|
||||
|
||||
/* Check if the queue is empty */
|
||||
final bool isEmpty = queue.isEmpty();
|
||||
print("Is queue empty = $isEmpty");
|
||||
}
|
||||
93
en/codes/dart/chapter_stack_and_queue/linkedlist_stack.dart
Normal file
93
en/codes/dart/chapter_stack_and_queue/linkedlist_stack.dart
Normal file
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* File: linkedlist_stack.dart
|
||||
* Created Time: 2023-03-27
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
import '../utils/list_node.dart';
|
||||
|
||||
/* Stack implemented based on linked list class */
|
||||
class LinkedListStack {
|
||||
ListNode? _stackPeek; // Use head node as stack top
|
||||
int _stkSize = 0; // Stack length
|
||||
|
||||
LinkedListStack() {
|
||||
_stackPeek = null;
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size() {
|
||||
return _stkSize;
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
bool isEmpty() {
|
||||
return _stkSize == 0;
|
||||
}
|
||||
|
||||
/* Push */
|
||||
void push(int _num) {
|
||||
final ListNode node = ListNode(_num);
|
||||
node.next = _stackPeek;
|
||||
_stackPeek = node;
|
||||
_stkSize++;
|
||||
}
|
||||
|
||||
/* Pop */
|
||||
int pop() {
|
||||
final int _num = peek();
|
||||
_stackPeek = _stackPeek!.next;
|
||||
_stkSize--;
|
||||
return _num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
int peek() {
|
||||
if (_stackPeek == null) {
|
||||
throw Exception("Stack is empty");
|
||||
}
|
||||
return _stackPeek!.val;
|
||||
}
|
||||
|
||||
/* Convert linked list to List and return */
|
||||
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() {
|
||||
/* Access top of the stack element */
|
||||
final LinkedListStack stack = LinkedListStack();
|
||||
|
||||
/* Elements push onto stack */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
print("Stack stack = ${stack.toList()}");
|
||||
|
||||
/* Return list for printing */
|
||||
final int peek = stack.peek();
|
||||
print("Top element peek = $peek");
|
||||
|
||||
/* Element pop from stack */
|
||||
final int pop = stack.pop();
|
||||
print("Pop element pop = $pop, after pop stack = ${stack.toList()}");
|
||||
|
||||
/* Get the length of the stack */
|
||||
final int size = stack.size();
|
||||
print("Stack length size = $size");
|
||||
|
||||
/* Check if empty */
|
||||
final bool isEmpty = stack.isEmpty();
|
||||
print("Is stack empty = $isEmpty");
|
||||
}
|
||||
37
en/codes/dart/chapter_stack_and_queue/queue.dart
Normal file
37
en/codes/dart/chapter_stack_and_queue/queue.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* File: queue.dart
|
||||
* Created Time: 2023-03-28
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
import 'dart:collection';
|
||||
|
||||
void main() {
|
||||
/* Access front of the queue element */
|
||||
// In Dart, generally use Queue class as queue
|
||||
final Queue<int> queue = Queue();
|
||||
|
||||
/* Elements enqueue */
|
||||
queue.add(1);
|
||||
queue.add(3);
|
||||
queue.add(2);
|
||||
queue.add(5);
|
||||
queue.add(4);
|
||||
print("Queue queue = $queue");
|
||||
|
||||
/* Return list for printing */
|
||||
final int peek = queue.first;
|
||||
print("Front element peek = $peek");
|
||||
|
||||
/* Element dequeue */
|
||||
final int pop = queue.removeFirst();
|
||||
print("Dequeue element pop = $pop, after dequeue queue = $queue");
|
||||
|
||||
/* Get queue length */
|
||||
final int size = queue.length;
|
||||
print("Queue length size = $size");
|
||||
|
||||
/* Check if the queue is empty */
|
||||
final bool isEmpty = queue.isEmpty;
|
||||
print("Is queue empty = $isEmpty");
|
||||
}
|
||||
35
en/codes/dart/chapter_stack_and_queue/stack.dart
Normal file
35
en/codes/dart/chapter_stack_and_queue/stack.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* File: stack.dart
|
||||
* Created Time: 2023-03-27
|
||||
* Author: liuyuxin (gvenusleo@gmail.com)
|
||||
*/
|
||||
|
||||
void main() {
|
||||
/* Access top of the stack element */
|
||||
// Dart has no built-in stack class, can use List as stack
|
||||
final List<int> stack = [];
|
||||
|
||||
/* Elements push onto stack */
|
||||
stack.add(1);
|
||||
stack.add(3);
|
||||
stack.add(2);
|
||||
stack.add(5);
|
||||
stack.add(4);
|
||||
print("Stack stack = $stack");
|
||||
|
||||
/* Return list for printing */
|
||||
final int peek = stack.last;
|
||||
print("Top element peek = $peek");
|
||||
|
||||
/* Element pop from stack */
|
||||
final int pop = stack.removeLast();
|
||||
print("Pop element pop = $pop, after pop stack = $stack");
|
||||
|
||||
/* Get the length of the stack */
|
||||
final int size = stack.length;
|
||||
print("Stack length size = $size");
|
||||
|
||||
/* Check if empty */
|
||||
final bool isEmpty = stack.isEmpty;
|
||||
print("Is stack empty = $isEmpty");
|
||||
}
|
||||
Reference in New Issue
Block a user