Skip to content

Commit b0c147b

Browse files
authored
Polish rust (#1777)
* Polish rust * Update array queue and linkedlist queue * Update linkedlist deque * make array deque generic
1 parent e8dc473 commit b0c147b

File tree

8 files changed

+41
-47
lines changed

8 files changed

+41
-47
lines changed

codes/rust/chapter_array_and_linkedlist/my_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl MyList {
9090
panic!("索引越界")
9191
};
9292
let num = self.arr[index];
93-
// 将将索引 index 之后的元素都向前移动一位
93+
// 将索引 index 之后的元素都向前移动一位
9494
for j in index..self.size - 1 {
9595
self.arr[j] = self.arr[j + 1];
9696
}

codes/rust/chapter_heap/heap.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,12 @@ fn test_push_max(heap: &mut BinaryHeap<i32>, val: i32) {
1313
println!("\n元素 {} 入堆后", val);
1414
print_util::print_heap(heap.iter().map(|&val| val).collect());
1515
}
16-
fn test_push_min(heap: &mut BinaryHeap<Reverse<i32>>, val: i32) {
17-
heap.push(Reverse(val)); // 元素入堆
18-
println!("\n元素 {} 入堆后", val);
19-
print_util::print_heap(heap.iter().map(|&val| val.0).collect());
20-
}
2116

2217
fn test_pop_max(heap: &mut BinaryHeap<i32>) {
2318
let val = heap.pop().unwrap();
2419
println!("\n堆顶元素 {} 出堆后", val);
2520
print_util::print_heap(heap.iter().map(|&val| val).collect());
2621
}
27-
fn test_pop_min(heap: &mut BinaryHeap<Reverse<i32>>) {
28-
let val = heap.pop().unwrap().0;
29-
println!("\n堆顶元素 {} 出堆后", val);
30-
print_util::print_heap(heap.iter().map(|&val| val.0).collect());
31-
}
3222

3323
/* Driver Code */
3424
fn main() {

codes/rust/chapter_stack_and_queue/array_deque.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
*/
66
use hello_algo_rust::include::print_util;
77
/* 基于环形数组实现的双向队列 */
8-
struct ArrayDeque {
9-
nums: Vec<i32>, // 用于存储双向队列元素的数组
8+
struct ArrayDeque<T> {
9+
nums: Vec<T>, // 用于存储双向队列元素的数组
1010
front: usize, // 队首指针,指向队首元素
1111
que_size: usize, // 双向队列长度
1212
}
1313

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

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

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

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

8383
/* 队尾出队 */
84-
fn pop_last(&mut self) -> i32 {
84+
fn pop_last(&mut self) -> T {
8585
let num = self.peek_last();
8686
self.que_size -= 1;
8787
num
8888
}
8989

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

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

108108
/* 返回数组用于打印 */
109-
fn to_array(&self) -> Vec<i32> {
109+
fn to_array(&self) -> Vec<T> {
110110
// 仅转换有效长度范围内的列表元素
111-
let mut res = vec![0; self.que_size];
111+
let mut res = vec![T::default(); self.que_size];
112112
let mut j = self.front;
113113
for i in 0..self.que_size {
114114
res[i] = self.nums[self.index(j as i32)];

codes/rust/chapter_stack_and_queue/array_queue.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
*/
66

77
/* 基于环形数组实现的队列 */
8-
struct ArrayQueue {
9-
nums: Vec<i32>, // 用于存储队列元素的数组
8+
struct ArrayQueue<T> {
9+
nums: Vec<T>, // 用于存储队列元素的数组
1010
front: i32, // 队首指针,指向队首元素
1111
que_size: i32, // 队列长度
1212
que_capacity: i32, // 队列容量
1313
}
1414

15-
impl ArrayQueue {
15+
impl<T: Copy + Default> ArrayQueue<T> {
1616
/* 构造方法 */
17-
fn new(capacity: i32) -> ArrayQueue {
17+
fn new(capacity: i32) -> ArrayQueue<T> {
1818
ArrayQueue {
19-
nums: vec![0; capacity as usize],
19+
nums: vec![T::default(); capacity as usize],
2020
front: 0,
2121
que_size: 0,
2222
que_capacity: capacity,
@@ -39,7 +39,7 @@ impl ArrayQueue {
3939
}
4040

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

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

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

7272
/* 返回数组 */
73-
fn to_vector(&self) -> Vec<i32> {
73+
fn to_vector(&self) -> Vec<T> {
7474
let cap = self.que_capacity;
7575
let mut j = self.front;
76-
let mut arr = vec![0; self.que_size as usize];
76+
let mut arr = vec![T::default(); cap as usize];
7777
for i in 0..self.que_size {
7878
arr[i as usize] = self.nums[(j % cap) as usize];
7979
j += 1;

codes/rust/chapter_stack_and_queue/linkedlist_deque.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ impl<T: Copy> LinkedListDeque<T> {
5050

5151
/* 判断双向队列是否为空 */
5252
pub fn is_empty(&self) -> bool {
53-
return self.size() == 0;
53+
return self.que_size == 0;
5454
}
5555

5656
/* 入队操作 */
57-
pub fn push(&mut self, num: T, is_front: bool) {
57+
fn push(&mut self, num: T, is_front: bool) {
5858
let node = ListNode::new(num);
5959
// 队首入队操作
6060
if is_front {
@@ -102,7 +102,7 @@ impl<T: Copy> LinkedListDeque<T> {
102102
}
103103

104104
/* 出队操作 */
105-
pub fn pop(&mut self, is_front: bool) -> Option<T> {
105+
fn pop(&mut self, is_front: bool) -> Option<T> {
106106
// 若队列为空,直接返回 None
107107
if self.is_empty() {
108108
return None;

codes/rust/chapter_stack_and_queue/linkedlist_queue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<T: Copy> LinkedListQueue<T> {
3333

3434
/* 判断队列是否为空 */
3535
pub fn is_empty(&self) -> bool {
36-
return self.size() == 0;
36+
return self.que_size == 0;
3737
}
3838

3939
/* 入队 */

codes/rust/chapter_stack_and_queue/linkedlist_stack.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ impl<T: Copy> LinkedListStack<T> {
5858
}
5959

6060
/* 将 List 转化为 Array 并返回 */
61-
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
62-
if let Some(node) = head {
63-
let mut nums = self.to_array(node.borrow().next.as_ref());
64-
nums.push(node.borrow().val);
65-
return nums;
61+
pub fn to_array(&self) -> Vec<T> {
62+
fn _to_array<T: Sized + Copy>(head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
63+
if let Some(node) = head {
64+
let mut nums = _to_array(node.borrow().next.as_ref());
65+
nums.push(node.borrow().val);
66+
return nums;
67+
}
68+
return Vec::new();
6669
}
67-
return Vec::new();
70+
71+
_to_array(self.peek())
6872
}
6973
}
7074

@@ -80,7 +84,7 @@ fn main() {
8084
stack.push(5);
8185
stack.push(4);
8286
print!("栈 stack = ");
83-
print_util::print_array(&stack.to_array(stack.peek()));
87+
print_util::print_array(&stack.to_array());
8488

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

9498
/* 获取栈的长度 */
9599
let size = stack.size();

codes/rust/src/include/tree_node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl TreeNode {
3434
macro_rules! op_vec {
3535
( $( $x:expr ),* ) => {
3636
vec![
37-
$( Option::from($x).map(|x| x) ),*
37+
$(Option::from($x)),*
3838
]
3939
};
4040
}

0 commit comments

Comments
 (0)