mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-09 21:55:32 +08:00
fix: check the rust codes and fix them (#653)
* fix: check the rust codes and fix it * Update binary_tree_bfs.rs --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -4,15 +4,16 @@
|
||||
* Author: WSL0809 (wslzzy@outlook.com)
|
||||
*/
|
||||
|
||||
|
||||
/* 基于环形数组实现的队列 */
|
||||
struct ArrayQueue {
|
||||
nums: Vec<i32>,
|
||||
front: i32,
|
||||
que_size: i32,
|
||||
que_capacity: i32,
|
||||
nums: Vec<i32>, // 用于存储队列元素的数组
|
||||
front: i32, // 队首指针,指向队首元素
|
||||
que_size: i32, // 队列长度
|
||||
que_capacity: i32, // 队列容量
|
||||
}
|
||||
|
||||
impl ArrayQueue {
|
||||
/* 构造方法 */
|
||||
fn new(capacity: i32) -> ArrayQueue {
|
||||
ArrayQueue {
|
||||
nums: vec![0; capacity as usize],
|
||||
@@ -22,31 +23,53 @@ impl ArrayQueue {
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取队列的容量 */
|
||||
fn capacity(&self) -> i32 {
|
||||
self.que_capacity
|
||||
}
|
||||
|
||||
/* 获取队列的长度 */
|
||||
fn size(&self) -> i32 {
|
||||
self.que_size
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
fn is_empty(&self) -> bool {
|
||||
self.que_size == 0
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
fn push(&mut self, num: i32) {
|
||||
if self.que_size == self.que_capacity {
|
||||
if self.que_size == self.capacity() {
|
||||
println!("队列已满");
|
||||
return;
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
||||
let rear = (self.front + self.que_size) % self.que_capacity;
|
||||
// self.nums.insert(rear as usize, num);
|
||||
// 将 num 添加至队尾
|
||||
self.nums[rear as usize] = num;
|
||||
self.que_size += 1;
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
fn pop(&mut self) -> i32 {
|
||||
let num = self.peek();
|
||||
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
||||
self.front = (self.front + 1) % self.que_capacity;
|
||||
self.que_size -= 1;
|
||||
num
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
fn peek(&self) -> i32 {
|
||||
if self.is_empty() {
|
||||
panic!("index out of bounds");
|
||||
}
|
||||
self.nums[self.front as usize]
|
||||
}
|
||||
|
||||
/* 返回数组 */
|
||||
fn to_vector(&self) -> Vec<i32> {
|
||||
let cap = self.que_capacity;
|
||||
let mut j = self.front;
|
||||
@@ -57,16 +80,14 @@ impl ArrayQueue {
|
||||
}
|
||||
arr
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.size() == 0
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
/* 初始化队列 */
|
||||
let capacity = 10;
|
||||
let mut queue = ArrayQueue::new(capacity);
|
||||
|
||||
/* 元素入队 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
@@ -74,9 +95,11 @@ fn main() {
|
||||
queue.push(4);
|
||||
println!("队列 queue = {:?}", queue.to_vector());
|
||||
|
||||
/* 访问队首元素 */
|
||||
let peek = queue.peek();
|
||||
println!("队首元素 peek = {}", peek);
|
||||
|
||||
/* 元素出队 */
|
||||
let pop = queue.pop();
|
||||
println!(
|
||||
"出队元素 pop = {:?},出队后 queue = {:?}",
|
||||
@@ -84,12 +107,15 @@ fn main() {
|
||||
queue.to_vector()
|
||||
);
|
||||
|
||||
/* 获取队列的长度 */
|
||||
let size = queue.size();
|
||||
println!("队列长度 size = {}", size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
let is_empty = queue.is_empty();
|
||||
println!("队列是否为空 = {}", is_empty);
|
||||
|
||||
/* 测试环形数组 */
|
||||
for i in 0..10 {
|
||||
queue.push(i);
|
||||
queue.pop();
|
||||
|
||||
Reference in New Issue
Block a user