Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codes/rust/chapter_array_and_linkedlist/my_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl MyList {
panic!("索引越界")
};
let num = self.arr[index];
// 将将索引 index 之后的元素都向前移动一位
// 将索引 index 之后的元素都向前移动一位

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

for j in index..self.size - 1 {
self.arr[j] = self.arr[j + 1];
}
Expand Down
10 changes: 0 additions & 10 deletions codes/rust/chapter_heap/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,12 @@ fn test_push_max(heap: &mut BinaryHeap<i32>, val: i32) {
println!("\n元素 {} 入堆后", val);
print_util::print_heap(heap.iter().map(|&val| val).collect());
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used function

fn test_push_min(heap: &mut BinaryHeap<Reverse<i32>>, val: i32) {
heap.push(Reverse(val)); // 元素入堆
println!("\n元素 {} 入堆后", val);
print_util::print_heap(heap.iter().map(|&val| val.0).collect());
}

fn test_pop_max(heap: &mut BinaryHeap<i32>) {
let val = heap.pop().unwrap();
println!("\n堆顶元素 {} 出堆后", val);
print_util::print_heap(heap.iter().map(|&val| val).collect());
}
fn test_pop_min(heap: &mut BinaryHeap<Reverse<i32>>) {
let val = heap.pop().unwrap().0;
println!("\n堆顶元素 {} 出堆后", val);
print_util::print_heap(heap.iter().map(|&val| val.0).collect());
}

/* Driver Code */
fn main() {
Expand Down
26 changes: 13 additions & 13 deletions codes/rust/chapter_stack_and_queue/array_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
*/
use hello_algo_rust::include::print_util;
/* 基于环形数组实现的双向队列 */
struct ArrayDeque {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make array deque generic

nums: Vec<i32>, // 用于存储双向队列元素的数组
struct ArrayDeque<T> {
nums: Vec<T>, // 用于存储双向队列元素的数组
front: usize, // 队首指针,指向队首元素
que_size: usize, // 双向队列长度
}

impl ArrayDeque {
impl<T: Copy + Default> ArrayDeque<T> {
/* 构造方法 */
pub fn new(capacity: usize) -> Self {
Self {
nums: vec![0; capacity],
nums: vec![T::default(); capacity],
front: 0,
que_size: 0,
}
Expand All @@ -41,11 +41,11 @@ impl ArrayDeque {
// 通过取余操作实现数组首尾相连
// 当 i 越过数组尾部后,回到头部
// 当 i 越过数组头部后,回到尾部
return ((i + self.capacity() as i32) % self.capacity() as i32) as usize;
((i + self.capacity() as i32) % self.capacity() as i32) as usize
}

/* 队首入队 */
pub fn push_first(&mut self, num: i32) {
pub fn push_first(&mut self, num: T) {
if self.que_size == self.capacity() {
println!("双向队列已满");
return;
Expand All @@ -59,7 +59,7 @@ impl ArrayDeque {
}

/* 队尾入队 */
pub fn push_last(&mut self, num: i32) {
pub fn push_last(&mut self, num: T) {
if self.que_size == self.capacity() {
println!("双向队列已满");
return;
Expand All @@ -72,7 +72,7 @@ impl ArrayDeque {
}

/* 队首出队 */
fn pop_first(&mut self) -> i32 {
fn pop_first(&mut self) -> T {
let num = self.peek_first();
// 队首指针向后移动一位
self.front = self.index(self.front as i32 + 1);
Expand All @@ -81,22 +81,22 @@ impl ArrayDeque {
}

/* 队尾出队 */
fn pop_last(&mut self) -> i32 {
fn pop_last(&mut self) -> T {
let num = self.peek_last();
self.que_size -= 1;
num
}

/* 访问队首元素 */
fn peek_first(&self) -> i32 {
fn peek_first(&self) -> T {
if self.is_empty() {
panic!("双向队列为空")
};
self.nums[self.front]
}

/* 访问队尾元素 */
fn peek_last(&self) -> i32 {
fn peek_last(&self) -> T {
if self.is_empty() {
panic!("双向队列为空")
};
Expand All @@ -106,9 +106,9 @@ impl ArrayDeque {
}

/* 返回数组用于打印 */
fn to_array(&self) -> Vec<i32> {
fn to_array(&self) -> Vec<T> {
// 仅转换有效长度范围内的列表元素
let mut res = vec![0; self.que_size];
let mut res = vec![T::default(); self.que_size];
let mut j = self.front;
for i in 0..self.que_size {
res[i] = self.nums[self.index(j as i32)];
Expand Down
20 changes: 10 additions & 10 deletions codes/rust/chapter_stack_and_queue/array_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
*/

/* 基于环形数组实现的队列 */
struct ArrayQueue {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make queue generic, same as linkedlist queue

nums: Vec<i32>, // 用于存储队列元素的数组
struct ArrayQueue<T> {
nums: Vec<T>, // 用于存储队列元素的数组
front: i32, // 队首指针,指向队首元素
que_size: i32, // 队列长度
que_capacity: i32, // 队列容量
}

impl ArrayQueue {
impl<T: Copy + Default> ArrayQueue<T> {
/* 构造方法 */
fn new(capacity: i32) -> ArrayQueue {
fn new(capacity: i32) -> ArrayQueue<T> {
ArrayQueue {
nums: vec![0; capacity as usize],
nums: vec![T::default(); capacity as usize],
front: 0,
que_size: 0,
que_capacity: capacity,
Expand All @@ -39,7 +39,7 @@ impl ArrayQueue {
}

/* 入队 */
fn push(&mut self, num: i32) {
fn push(&mut self, num: T) {
if self.que_size == self.capacity() {
println!("队列已满");
return;
Expand All @@ -53,7 +53,7 @@ impl ArrayQueue {
}

/* 出队 */
fn pop(&mut self) -> i32 {
fn pop(&mut self) -> T {
let num = self.peek();
// 队首指针向后移动一位,若越过尾部,则返回到数组头部
self.front = (self.front + 1) % self.que_capacity;
Expand All @@ -62,18 +62,18 @@ impl ArrayQueue {
}

/* 访问队首元素 */
fn peek(&self) -> i32 {
fn peek(&self) -> T {
if self.is_empty() {
panic!("index out of bounds");
}
self.nums[self.front as usize]
}

/* 返回数组 */
fn to_vector(&self) -> Vec<i32> {
fn to_vector(&self) -> Vec<T> {
let cap = self.que_capacity;
let mut j = self.front;
let mut arr = vec![0; self.que_size as usize];
let mut arr = vec![T::default(); cap as usize];
for i in 0..self.que_size {
arr[i as usize] = self.nums[(j % cap) as usize];
j += 1;
Expand Down
6 changes: 3 additions & 3 deletions codes/rust/chapter_stack_and_queue/linkedlist_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ impl<T: Copy> LinkedListDeque<T> {

/* 判断双向队列是否为空 */
pub fn is_empty(&self) -> bool {
return self.size() == 0;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as CPP

return self.que_size == 0;
}

/* 入队操作 */
pub fn push(&mut self, num: T, is_front: bool) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

helper method, no need to export as public API

fn push(&mut self, num: T, is_front: bool) {
let node = ListNode::new(num);
// 队首入队操作
if is_front {
Expand Down Expand Up @@ -102,7 +102,7 @@ impl<T: Copy> LinkedListDeque<T> {
}

/* 出队操作 */
pub fn pop(&mut self, is_front: bool) -> Option<T> {
fn pop(&mut self, is_front: bool) -> Option<T> {
// 若队列为空,直接返回 None
if self.is_empty() {
return None;
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/chapter_stack_and_queue/linkedlist_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<T: Copy> LinkedListQueue<T> {

/* 判断队列是否为空 */
pub fn is_empty(&self) -> bool {
return self.size() == 0;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as cpp

return self.que_size == 0;
}

/* 入队 */
Expand Down
20 changes: 12 additions & 8 deletions codes/rust/chapter_stack_and_queue/linkedlist_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ impl<T: Copy> LinkedListStack<T> {
}

/* 将 List 转化为 Array 并返回 */
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
if let Some(node) = head {
let mut nums = self.to_array(node.borrow().next.as_ref());
nums.push(node.borrow().val);
return nums;
pub fn to_array(&self) -> Vec<T> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make method signature same as cpp, no extra arg for head

fn _to_array<T: Sized + Copy>(head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
if let Some(node) = head {
let mut nums = _to_array(node.borrow().next.as_ref());
nums.push(node.borrow().val);
return nums;
}
return Vec::new();
}
return Vec::new();

_to_array(self.peek())
}
}

Expand All @@ -80,7 +84,7 @@ fn main() {
stack.push(5);
stack.push(4);
print!("栈 stack = ");
print_util::print_array(&stack.to_array(stack.peek()));
print_util::print_array(&stack.to_array());

/* 访问栈顶元素 */
let peek = stack.peek().unwrap().borrow().val;
Expand All @@ -89,7 +93,7 @@ fn main() {
/* 元素出栈 */
let pop = stack.pop().unwrap();
print!("\n出栈元素 pop = {},出栈后 stack = ", pop);
print_util::print_array(&stack.to_array(stack.peek()));
print_util::print_array(&stack.to_array());

/* 获取栈的长度 */
let size = stack.size();
Expand Down
2 changes: 1 addition & 1 deletion codes/rust/src/include/tree_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl TreeNode {
macro_rules! op_vec {
( $( $x:expr ),* ) => {
vec![
$( Option::from($x).map(|x| x) ),*
$(Option::from($x)),*

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless map

]
};
}
Expand Down