Retranslate the complete English edition from Simplified Chinese (#2010)

* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
This commit is contained in:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -4,11 +4,11 @@
* Author: codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::print_util;
/* Double-ended queue based on circular array implementation */
/* Deque implemented with a circular array */
struct ArrayDeque<T> {
nums: Vec<T>, // Array for storing double-ended queue elements
front: usize, // Front pointer, points to the front of the queue element
que_size: usize, // Double-ended queue length
nums: Vec<T>, // Array for storing deque elements
front: usize, // Front index, identifying the first element
que_size: usize, // Deque size
}
impl<T: Copy + Default> ArrayDeque<T> {
@@ -21,73 +21,73 @@ impl<T: Copy + Default> ArrayDeque<T> {
}
}
/* Get the capacity of the double-ended queue */
/* Get the deque capacity */
pub fn capacity(&self) -> usize {
self.nums.len()
}
/* Get the length of the double-ended queue */
/* Get the deque size */
pub fn size(&self) -> usize {
self.que_size
}
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
pub fn is_empty(&self) -> bool {
self.que_size == 0
}
/* Calculate circular array index */
/* Calculate an index in the circular array */
fn index(&self, i: i32) -> usize {
// 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
// Use modulo to connect the end of the array to its beginning
// When i passes the end of the array, wrap around to the beginning
// When i passes the beginning of the array, wrap around to the end
((i + self.capacity() as i32) % self.capacity() as i32) as usize
}
/* Front of the queue enqueue */
/* Enqueue at the front */
pub fn push_first(&mut self, num: T) {
if self.que_size == self.capacity() {
println!("Double-ended queue is full");
println!("Deque is full");
return;
}
// Use modulo operation to wrap front around to the tail after passing the head of the array
// Add num to the front of the queue
// Move the front index one position to the left
// Use modulo to wrap front to the end when it passes the beginning of the array
self.front = self.index(self.front as i32 - 1);
// Add num to front of queue
// Add num to the front
self.nums[self.front] = num;
self.que_size += 1;
}
/* Rear of the queue enqueue */
/* Enqueue at the rear */
pub fn push_last(&mut self, num: T) {
if self.que_size == self.capacity() {
println!("Double-ended queue is full");
println!("Deque is full");
return;
}
// Use modulo operation to wrap rear around to the head after passing the tail of the array
// Calculate the rear index, one position past the last element
let rear = self.index(self.front as i32 + self.que_size as i32);
// Front pointer moves one position backward
// Add num to the rear
self.nums[rear] = num;
self.que_size += 1;
}
/* Rear of the queue dequeue */
/* Dequeue from the front */
fn pop_first(&mut self) -> T {
let num = self.peek_first();
// Move front pointer backward by one position
// Advance the front index by one position
self.front = self.index(self.front as i32 + 1);
self.que_size -= 1;
num
}
/* Access rear of the queue element */
/* Dequeue from the rear */
fn pop_last(&mut self) -> T {
let num = self.peek_last();
self.que_size -= 1;
num
}
/* Return list for printing */
/* Access the front element */
fn peek_first(&self) -> T {
if self.is_empty() {
panic!("Deque is empty")
@@ -95,19 +95,19 @@ impl<T: Copy + Default> ArrayDeque<T> {
self.nums[self.front]
}
/* Driver Code */
/* Access the rear element */
fn peek_last(&self) -> T {
if self.is_empty() {
panic!("Deque is empty")
};
// Initialize double-ended queue
// Calculate the index of the last element
let last = self.index(self.front as i32 + self.que_size as i32 - 1);
self.nums[last]
}
/* Return array for printing */
/* Return a vector for printing */
fn to_array(&self) -> Vec<T> {
// Elements enqueue
// Convert only the list elements within the occupied range
let mut res = vec![T::default(); self.que_size];
let mut j = self.front;
for i in 0..self.que_size {
@@ -118,43 +118,43 @@ impl<T: Copy + Default> ArrayDeque<T> {
}
}
/* Driver Code */
/* Driver code */
fn main() {
/* Get the length of the double-ended queue */
/* Initialize the deque */
let mut deque = ArrayDeque::new(10);
deque.push_last(3);
deque.push_last(2);
deque.push_last(5);
print!("Double-ended queue deque = ");
print!("Deque deque = ");
print_util::print_array(&deque.to_array());
/* Update element */
/* Access an element */
let peek_first = deque.peek_first();
print!("\nFront element peek_first = {}", peek_first);
let peek_last = deque.peek_last();
print!("\nRear element peek_last = {}", peek_last);
/* Elements enqueue */
/* Enqueue elements */
deque.push_last(4);
print!("\nAfter element 4 enqueues at rear, deque = ");
print!("\nAfter enqueuing element 4 at the rear, deque = ");
print_util::print_array(&deque.to_array());
deque.push_first(1);
print!("\nAfter element 1 enqueues at front, deque = ");
print!("\nAfter enqueuing element 1 at the front, deque = ");
print_util::print_array(&deque.to_array());
/* Element dequeue */
/* Dequeue an element */
let pop_last = deque.pop_last();
print!("\nDequeue rear element = {}, after dequeue deque = ", pop_last);
print!("\nElement removed from the rear = {}; after removal, deque = ", pop_last);
print_util::print_array(&deque.to_array());
let pop_first = deque.pop_first();
print!("\nDequeue front element = {}, after dequeue deque = ", pop_first);
print!("\nElement removed from the front = {}; after removal, deque = ", pop_first);
print_util::print_array(&deque.to_array());
/* Get the length of the double-ended queue */
/* Get the deque size */
let size = deque.size();
print!("\nDeque length size = {}", size);
print!("\nDeque size = {}", size);
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
let is_empty = deque.is_empty();
print!("\nIs deque empty = {}", is_empty);
print!("\nIs the deque empty? {}", is_empty);
}

View File

@@ -4,11 +4,11 @@
* Author: WSL0809 (wslzzy@outlook.com)
*/
/* Queue based on circular array implementation */
/* Queue implemented with a circular array */
struct ArrayQueue<T> {
nums: Vec<T>, // Array for storing queue elements
front: i32, // Front pointer, points to the front of the queue element
que_size: i32, // Queue length
nums: Vec<T>, // Array that stores the queue elements
front: i32, // Front index, identifying the first element
que_size: i32, // Queue size
que_capacity: i32, // Queue capacity
}
@@ -23,17 +23,17 @@ impl<T: Copy + Default> ArrayQueue<T> {
}
}
/* Get the capacity of the queue */
/* Get the queue capacity */
fn capacity(&self) -> i32 {
self.que_capacity
}
/* Get the length of the queue */
/* Get the queue size */
fn size(&self) -> i32 {
self.que_size
}
/* Check if the queue is empty */
/* Check whether the queue is empty */
fn is_empty(&self) -> bool {
self.que_size == 0
}
@@ -44,10 +44,10 @@ impl<T: Copy + Default> ArrayQueue<T> {
println!("Queue is full");
return;
}
// 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
// Calculate the rear index, one position past the last element
// Use modulo to wrap rear to the beginning when it passes the end of the array
let rear = (self.front + self.que_size) % self.que_capacity;
// Front pointer moves one position backward
// Add num to the rear
self.nums[rear as usize] = num;
self.que_size += 1;
}
@@ -55,21 +55,21 @@ impl<T: Copy + Default> ArrayQueue<T> {
/* Dequeue */
fn pop(&mut self) -> T {
let num = self.peek();
// Move front pointer backward by one position, if it passes the tail, return to array head
// Advance the front index by one position, wrapping to the beginning if it passes the end
self.front = (self.front + 1) % self.que_capacity;
self.que_size -= 1;
num
}
/* Return list for printing */
/* Access the front element */
fn peek(&self) -> T {
if self.is_empty() {
panic!("index out of bounds");
panic!("Index out of bounds");
}
self.nums[self.front as usize]
}
/* Return array */
/* Return a vector */
fn to_vector(&self) -> Vec<T> {
let cap = self.que_capacity;
let mut j = self.front;
@@ -82,13 +82,13 @@ impl<T: Copy + Default> ArrayQueue<T> {
}
}
/* Driver Code */
/* Driver code */
fn main() {
/* Access front of the queue element */
/* Initialize the queue */
let capacity = 10;
let mut queue = ArrayQueue::new(capacity);
/* Elements enqueue */
/* Enqueue elements */
queue.push(1);
queue.push(3);
queue.push(2);
@@ -96,30 +96,30 @@ fn main() {
queue.push(4);
println!("Queue queue = {:?}", queue.to_vector());
/* Return list for printing */
/* Access the front element */
let peek = queue.peek();
println!("Front element peek = {}", peek);
/* Element dequeue */
/* Dequeue an element */
let pop = queue.pop();
println!(
"Dequeue element pop = {:?}, after dequeue queue = {:?}",
"Dequeued element pop = {:?}; after dequeueing, queue = {:?}",
pop,
queue.to_vector()
);
/* Get the length of the queue */
/* Get the queue size */
let size = queue.size();
println!("Queue length size = {}", size);
println!("Queue size = {}", size);
/* Check if the queue is empty */
/* Check whether the queue is empty */
let is_empty = queue.is_empty();
println!("Is queue empty = {}", is_empty);
println!("Is the queue empty? {}", is_empty);
/* Test circular array */
/* Test the circular array */
for i in 0..10 {
queue.push(i);
queue.pop();
println!("After round {:?} of enqueue + dequeue, queue = {:?}", i, queue.to_vector());
println!("After enqueueing and dequeueing in round {:?}, queue = {:?}", i, queue.to_vector());
}
}

View File

@@ -6,25 +6,25 @@
use hello_algo_rust::include::print_util;
/* Stack based on array implementation */
/* Stack implemented with an array */
struct ArrayStack<T> {
stack: Vec<T>,
}
impl<T> ArrayStack<T> {
/* Access top of the stack element */
/* Initialize the stack */
fn new() -> ArrayStack<T> {
ArrayStack::<T> {
stack: Vec::<T>::new(),
}
}
/* Get the length of the stack */
/* Get the stack size */
fn size(&self) -> usize {
self.stack.len()
}
/* Check if the stack is empty */
/* Check whether the stack is empty */
fn is_empty(&self) -> bool {
self.size() == 0
}
@@ -39,7 +39,7 @@ impl<T> ArrayStack<T> {
self.stack.pop()
}
/* Return list for printing */
/* Access the top element */
fn peek(&self) -> Option<&T> {
if self.is_empty() {
panic!("Stack is empty")
@@ -47,18 +47,18 @@ impl<T> ArrayStack<T> {
self.stack.last()
}
/* Return &Vec */
/* Return a reference to the vector */
fn to_array(&self) -> &Vec<T> {
&self.stack
}
}
/* Driver Code */
/* Driver code */
fn main() {
// Access top of the stack element
// Initialize the stack
let mut stack = ArrayStack::<i32>::new();
// Elements push onto stack
// Push elements onto the stack
stack.push(1);
stack.push(3);
stack.push(2);
@@ -67,20 +67,20 @@ fn main() {
print!("Stack stack = ");
print_util::print_array(stack.to_array());
// Return list for printing
//Access the top element
let peek = stack.peek().unwrap();
print!("\nTop element peek = {}", peek);
// Element pop from stack
// Pop an element from the stack
let pop = stack.pop().unwrap();
print!("\nPop element pop = {pop}, after pop stack = ");
print!("\nPopped element pop = {pop}; after popping, stack = ");
print_util::print_array(stack.to_array());
// Get the length of the stack
// Get the stack size
let size = stack.size();
print!("\nStack length size = {size}");
print!("\nStack size = {size}");
// Check if empty
// Check whether it is empty
let is_empty = stack.is_empty();
print!("\nIs stack empty = {is_empty}");
print!("\nIs the stack empty? {is_empty}");
}

View File

@@ -7,43 +7,43 @@
use hello_algo_rust::include::print_util;
use std::collections::VecDeque;
/* Driver Code */
/* Driver code */
pub fn main() {
// Get the length of the double-ended queue
// Initialize the deque
let mut deque: VecDeque<i32> = VecDeque::new();
deque.push_back(3);
deque.push_back(2);
deque.push_back(5);
print!("Double-ended queue deque = ");
print!("Deque deque = ");
print_util::print_queue(&deque);
// Update element
// Access an element
let peek_first = deque.front().unwrap();
print!("\nFront element peekFirst = {peek_first}");
print!("\nFront element peek_first = {peek_first}");
let peek_last = deque.back().unwrap();
print!("\nRear element peekLast = {peek_last}");
print!("\nRear element peek_last = {peek_last}");
/* Elements enqueue */
/* Enqueue elements */
deque.push_back(4);
print!("\nAfter element 4 enqueues at rear, deque = ");
print!("\nAfter enqueuing element 4 at the rear, deque = ");
print_util::print_queue(&deque);
deque.push_front(1);
print!("\nAfter element 1 enqueues at front, deque = ");
print!("\nAfter enqueuing element 1 at the front, deque = ");
print_util::print_queue(&deque);
// Element dequeue
// Dequeue an element
let pop_last = deque.pop_back().unwrap();
print!("\nDequeue rear element = {pop_last}, after dequeue deque = ");
print!("\nElement removed from the rear = {pop_last}; after removal, deque = ");
print_util::print_queue(&deque);
let pop_first = deque.pop_front().unwrap();
print!("\nDequeue front element = {pop_first}, after dequeue deque = ");
print!("\nElement removed from the front = {pop_first}; after removal, deque = ");
print_util::print_queue(&deque);
// Get the length of the double-ended queue
// Get the deque size
let size = deque.len();
print!("\nDeque length size = {size}");
print!("\nDeque size = {size}");
// Check if the double-ended queue is empty
// Check whether the deque is empty
let is_empty = deque.is_empty();
print!("\nIs deque empty = {is_empty}");
print!("\nIs the deque empty? {is_empty}");
}

View File

@@ -12,8 +12,8 @@ use std::rc::Rc;
/* Doubly linked list node */
pub struct ListNode<T> {
pub val: T, // Node value
pub next: Option<Rc<RefCell<ListNode<T>>>>, // Successor node pointer
pub prev: Option<Rc<RefCell<ListNode<T>>>>, // Predecessor node pointer
pub next: Option<Rc<RefCell<ListNode<T>>>>, // Pointer to the next node
pub prev: Option<Rc<RefCell<ListNode<T>>>>, // Pointer to the previous node
}
impl<T> ListNode<T> {
@@ -26,12 +26,12 @@ impl<T> ListNode<T> {
}
}
/* Double-ended queue based on doubly linked list implementation */
/* Deque implemented with a doubly linked list */
#[allow(dead_code)]
pub struct LinkedListDeque<T> {
front: Option<Rc<RefCell<ListNode<T>>>>, // Head node front
rear: Option<Rc<RefCell<ListNode<T>>>>, // Tail node rear
que_size: usize, // Length of the double-ended queue
que_size: usize, // Deque size
}
impl<T: Copy> LinkedListDeque<T> {
@@ -43,12 +43,12 @@ impl<T: Copy> LinkedListDeque<T> {
}
}
/* Get the length of the double-ended queue */
/* Get the deque size */
pub fn size(&self) -> usize {
return self.que_size;
}
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
pub fn is_empty(&self) -> bool {
return self.que_size == 0;
}
@@ -56,7 +56,7 @@ impl<T: Copy> LinkedListDeque<T> {
/* Enqueue operation */
fn push(&mut self, num: T, is_front: bool) {
let node = ListNode::new(num);
// Front of the queue enqueue operation
// Enqueue at the front
if is_front {
match self.front.take() {
// If the linked list is empty, make both front and rear point to node
@@ -68,11 +68,11 @@ impl<T: Copy> LinkedListDeque<T> {
Some(old_front) => {
old_front.borrow_mut().prev = Some(node.clone());
node.borrow_mut().next = Some(old_front);
self.front = Some(node); // Update head node
self.front = Some(node); // Update the head node
}
}
}
// Rear of the queue enqueue operation
// Enqueue at the rear
else {
match self.rear.take() {
// If the linked list is empty, make both front and rear point to node
@@ -84,84 +84,84 @@ impl<T: Copy> LinkedListDeque<T> {
Some(old_rear) => {
old_rear.borrow_mut().next = Some(node.clone());
node.borrow_mut().prev = Some(old_rear);
self.rear = Some(node); // Update tail node
self.rear = Some(node); // Update the tail node
}
}
}
self.que_size += 1; // Update queue length
self.que_size += 1; // Update the deque size
}
/* Front of the queue enqueue */
/* Enqueue at the front */
pub fn push_first(&mut self, num: T) {
self.push(num, true);
}
/* Rear of the queue enqueue */
/* Enqueue at the rear */
pub fn push_last(&mut self, num: T) {
self.push(num, false);
}
/* Dequeue operation */
fn pop(&mut self, is_front: bool) -> Option<T> {
// If queue is empty, return None directly
// If the deque is empty, return None immediately
if self.is_empty() {
return None;
};
// Temporarily store head node value
// Dequeue from the front
if is_front {
self.front.take().map(|old_front| {
match old_front.borrow_mut().next.take() {
Some(new_front) => {
new_front.borrow_mut().prev.take();
self.front = Some(new_front); // Update head node
self.front = Some(new_front); // Update the head node
}
None => {
self.rear.take();
}
}
self.que_size -= 1; // Update queue length
self.que_size -= 1; // Update the deque size
old_front.borrow().val
})
}
// Temporarily store tail node value
// Dequeue from the rear
else {
self.rear.take().map(|old_rear| {
match old_rear.borrow_mut().prev.take() {
Some(new_rear) => {
new_rear.borrow_mut().next.take();
self.rear = Some(new_rear); // Update tail node
self.rear = Some(new_rear); // Update the tail node
}
None => {
self.front.take();
}
}
self.que_size -= 1; // Update queue length
self.que_size -= 1; // Update the deque size
old_rear.borrow().val
})
}
}
/* Rear of the queue dequeue */
/* Dequeue from the front */
pub fn pop_first(&mut self) -> Option<T> {
return self.pop(true);
}
/* Access rear of the queue element */
/* Dequeue from the rear */
pub fn pop_last(&mut self) -> Option<T> {
return self.pop(false);
}
/* Return list for printing */
/* Access the front element */
pub fn peek_first(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {
self.front.as_ref()
}
/* Driver Code */
/* Access the rear element */
pub fn peek_last(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {
self.rear.as_ref()
}
/* Return array for printing */
/* Return a vector for printing */
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
let mut res: Vec<T> = Vec::new();
fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {
@@ -176,43 +176,43 @@ impl<T: Copy> LinkedListDeque<T> {
}
}
/* Driver Code */
/* Driver code */
fn main() {
/* Get the length of the double-ended queue */
/* Initialize the deque */
let mut deque = LinkedListDeque::new();
deque.push_last(3);
deque.push_last(2);
deque.push_last(5);
print!("Double-ended queue deque = ");
print!("Deque deque = ");
print_util::print_array(&deque.to_array(deque.peek_first()));
/* Update element */
/* Access an element */
let peek_first = deque.peek_first().unwrap().borrow().val;
print!("\nFront element peek_first = {}", peek_first);
let peek_last = deque.peek_last().unwrap().borrow().val;
print!("\nRear element peek_last = {}", peek_last);
/* Elements enqueue */
/* Enqueue elements */
deque.push_last(4);
print!("\nAfter element 4 enqueues at rear, deque = ");
print!("\nAfter enqueuing element 4 at the rear, deque = ");
print_util::print_array(&deque.to_array(deque.peek_first()));
deque.push_first(1);
print!("\nAfter element 1 enqueues at front, deque = ");
print!("\nAfter enqueuing element 1 at the front, deque = ");
print_util::print_array(&deque.to_array(deque.peek_first()));
/* Element dequeue */
/* Dequeue an element */
let pop_last = deque.pop_last().unwrap();
print!("\nDequeue rear element = {}, after dequeue deque = ", pop_last);
print!("\nElement removed from the rear = {}; after removal, deque = ", pop_last);
print_util::print_array(&deque.to_array(deque.peek_first()));
let pop_first = deque.pop_first().unwrap();
print!("\nDequeue front element = {}, after dequeue deque = ", pop_first);
print!("\nElement removed from the front = {}; after removal, deque = ", pop_first);
print_util::print_array(&deque.to_array(deque.peek_first()));
/* Get the length of the double-ended queue */
/* Get the deque size */
let size = deque.size();
print!("\nDeque length size = {}", size);
print!("\nDeque size = {}", size);
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
let is_empty = deque.is_empty();
print!("\nIs deque empty = {}", is_empty);
print!("\nIs the deque empty? {}", is_empty);
}

View File

@@ -9,12 +9,12 @@ use hello_algo_rust::include::{print_util, ListNode};
use std::cell::RefCell;
use std::rc::Rc;
/* Queue based on linked list implementation */
/* Queue implemented with a linked list */
#[allow(dead_code)]
pub struct LinkedListQueue<T> {
front: Option<Rc<RefCell<ListNode<T>>>>, // Head node front
rear: Option<Rc<RefCell<ListNode<T>>>>, // Tail node rear
que_size: usize, // Queue length
que_size: usize, // Queue size
}
impl<T: Copy> LinkedListQueue<T> {
@@ -26,12 +26,12 @@ impl<T: Copy> LinkedListQueue<T> {
}
}
/* Get the length of the queue */
/* Get the queue size */
pub fn size(&self) -> usize {
return self.que_size;
}
/* Check if the queue is empty */
/* Check whether the queue is empty */
pub fn is_empty(&self) -> bool {
return self.que_size == 0;
}
@@ -41,12 +41,12 @@ impl<T: Copy> LinkedListQueue<T> {
// Add num after the tail node
let new_rear = ListNode::new(num);
match self.rear.take() {
// If the queue is not empty, add the node after the tail node
// If the queue is not empty, add this node after the tail node
Some(old_rear) => {
old_rear.borrow_mut().next = Some(new_rear.clone());
self.rear = Some(new_rear);
}
// If the queue is empty, make both front and rear point to the node
// If the queue is empty, make both the head and tail point to this node
None => {
self.front = Some(new_rear.clone());
self.rear = Some(new_rear);
@@ -71,12 +71,12 @@ impl<T: Copy> LinkedListQueue<T> {
})
}
/* Return list for printing */
/* Access the front element */
pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {
self.front.as_ref()
}
/* Convert linked list to Array and return */
/* Convert the linked list to a vector and return it */
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
let mut res: Vec<T> = Vec::new();
@@ -93,12 +93,12 @@ impl<T: Copy> LinkedListQueue<T> {
}
}
/* Driver Code */
/* Driver code */
fn main() {
/* Access front of the queue element */
/* Initialize the queue */
let mut queue = LinkedListQueue::new();
/* Elements enqueue */
/* Enqueue elements */
queue.push(1);
queue.push(3);
queue.push(2);
@@ -107,20 +107,20 @@ fn main() {
print!("Queue queue = ");
print_util::print_array(&queue.to_array(queue.peek()));
/* Return list for printing */
/* Access the front element */
let peek = queue.peek().unwrap().borrow().val;
print!("\nFront element peek = {}", peek);
/* Element dequeue */
/* Dequeue an element */
let pop = queue.pop().unwrap();
print!("\nDequeue element pop = {}, after dequeue queue = ", pop);
print!("\nDequeued element pop = {}; after dequeueing, queue = ", pop);
print_util::print_array(&queue.to_array(queue.peek()));
/* Get the length of the queue */
/* Get the queue size */
let size = queue.size();
print!("\nQueue length size = {}", size);
print!("\nQueue size = {}", size);
/* Check if the queue is empty */
/* Check whether the queue is empty */
let is_empty = queue.is_empty();
print!("\nIs queue empty = {}", is_empty);
print!("\nIs the queue empty? {}", is_empty);
}

View File

@@ -9,11 +9,11 @@ use hello_algo_rust::include::{print_util, ListNode};
use std::cell::RefCell;
use std::rc::Rc;
/* Stack based on linked list implementation */
/* Stack implemented with a linked list */
#[allow(dead_code)]
pub struct LinkedListStack<T> {
stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // Use head node as stack top
stk_size: usize, // Stack length
stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // Use the head node as the stack top
stk_size: usize, // Stack size
}
impl<T: Copy> LinkedListStack<T> {
@@ -24,12 +24,12 @@ impl<T: Copy> LinkedListStack<T> {
}
}
/* Get the length of the stack */
/* Get the stack size */
pub fn size(&self) -> usize {
return self.stk_size;
}
/* Check if the stack is empty */
/* Check whether the stack is empty */
pub fn is_empty(&self) -> bool {
return self.size() == 0;
}
@@ -52,12 +52,12 @@ impl<T: Copy> LinkedListStack<T> {
})
}
/* Return list for printing */
/* Access the top element */
pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {
self.stack_peek.as_ref()
}
/* Convert List to Array and return */
/* Convert the linked list to a vector and return it */
pub fn to_array(&self) -> Vec<T> {
fn _to_array<T: Sized + Copy>(head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
if let Some(node) = head {
@@ -72,12 +72,12 @@ impl<T: Copy> LinkedListStack<T> {
}
}
/* Driver Code */
/* Driver code */
fn main() {
/* Access top of the stack element */
/* Initialize the stack */
let mut stack = LinkedListStack::new();
/* Elements push onto stack */
/* Push elements onto the stack */
stack.push(1);
stack.push(3);
stack.push(2);
@@ -86,20 +86,20 @@ fn main() {
print!("Stack stack = ");
print_util::print_array(&stack.to_array());
/* Return list for printing */
/* Access the top element */
let peek = stack.peek().unwrap().borrow().val;
print!("\nTop element peek = {}", peek);
/* Element pop from stack */
/* Pop an element from the stack */
let pop = stack.pop().unwrap();
print!("\nPop element pop = {}, after pop stack = ", pop);
print!("\nPopped element pop = {}; after popping, stack = ", pop);
print_util::print_array(&stack.to_array());
/* Get the length of the stack */
/* Get the stack size */
let size = stack.size();
print!("\nStack length size = {}", size);
print!("\nStack size = {}", size);
/* Check if empty */
/* Check whether it is empty */
let is_empty = stack.is_empty();
print!("\nIs stack empty = {}", is_empty);
print!("\nIs the stack empty? {}", is_empty);
}

View File

@@ -8,12 +8,12 @@ use hello_algo_rust::include::print_util;
use std::collections::VecDeque;
/* Driver Code */
/* Driver code */
pub fn main() {
// Access front of the queue element
// Initialize the queue
let mut queue: VecDeque<i32> = VecDeque::new();
// Elements enqueue
// Enqueue elements
queue.push_back(1);
queue.push_back(3);
queue.push_back(2);
@@ -22,20 +22,20 @@ pub fn main() {
print!("Queue queue = ");
print_util::print_queue(&queue);
// Return list for printing
// Access the front element
let peek = queue.front().unwrap();
println!("\nFront element peek = {peek}");
// Element dequeue
// Dequeue an element
let pop = queue.pop_front().unwrap();
print!("Dequeue element pop = {pop}, after dequeue queue = ");
print!("Dequeued element pop = {pop}; after dequeueing, queue = ");
print_util::print_queue(&queue);
// Get the length of the queue
// Get the queue size
let size = queue.len();
print!("\nQueue length size = {size}");
print!("\nQueue size = {size}");
// Check if the queue is empty
// Check whether the queue is empty
let is_empty = queue.is_empty();
print!("\nIs queue empty = {is_empty}");
print!("\nIs the queue empty? {is_empty}");
}

View File

@@ -6,13 +6,13 @@
use hello_algo_rust::include::print_util;
/* Driver Code */
/* Driver code */
pub fn main() {
// Access top of the stack element
// In Rust, it's recommended to use Vec as a stack
// Initialize the stack
// In Rust, Vec is the recommended type for implementing a stack
let mut stack: Vec<i32> = Vec::new();
// Elements push onto stack
// Push elements onto the stack
stack.push(1);
stack.push(3);
stack.push(2);
@@ -21,20 +21,20 @@ pub fn main() {
print!("Stack stack = ");
print_util::print_array(&stack);
// Return list for printing
// Access the top element
let peek = stack.last().unwrap();
print!("\nTop element peek = {peek}");
// Element pop from stack
// Pop an element from the stack
let pop = stack.pop().unwrap();
print!("\nPop element pop = {pop}, after pop stack = ");
print!("\nPopped element pop = {pop}; after popping, stack = ");
print_util::print_array(&stack);
// Get the length of the stack
// Get the stack size
let size = stack.len();
print!("\nStack length size = {size}");
print!("\nStack size = {size}");
// Check if the stack is empty
// Check whether the stack is empty
let is_empty = stack.is_empty();
print!("\nIs stack empty = {is_empty}");
print!("\nIs the stack empty? {is_empty}");
}