From ea5677b3c614402260eefc2802efae4e2c720736 Mon Sep 17 00:00:00 2001 From: WangSL <48207171+WSL0809@users.noreply.github.com> Date: Sat, 11 Feb 2023 18:03:49 +0800 Subject: [PATCH] Add array_queue.rs in chapter_stack_and_queue (#359) * array_stack.rs * array_queue.rs * Update Cargo.toml * Create array_queue.rs * Delete array_queue.rs * Add array_queue.rs in chapter_stack_and queue * Update array_queue.rs --------- Co-authored-by: wsl --- codes/rust/Cargo.toml | 7 +- .../chapter_stack_and_queue/array_queue.rs | 98 +++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 codes/rust/chapter_stack_and_queue/array_queue.rs diff --git a/codes/rust/Cargo.toml b/codes/rust/Cargo.toml index 2775ec93f..3d329b766 100644 --- a/codes/rust/Cargo.toml +++ b/codes/rust/Cargo.toml @@ -64,5 +64,10 @@ path = "chapter_sorting/bubble_sort.rs" name = "array_stack" path = "chapter_stack_and_queue/array_stack.rs" +# Run Command: cargo run --bin array_queue +[[bin]] +name = "array_queue" +path = "chapter_stack_and_queue/array_queue.rs" + [dependencies] -rand = "0.8.5" \ No newline at end of file +rand = "0.8.5" diff --git a/codes/rust/chapter_stack_and_queue/array_queue.rs b/codes/rust/chapter_stack_and_queue/array_queue.rs new file mode 100644 index 000000000..87b958dfd --- /dev/null +++ b/codes/rust/chapter_stack_and_queue/array_queue.rs @@ -0,0 +1,98 @@ +/* + * File: array_queue.rs + * Created Time: 2023-02-06 + * Author: WSL0809 (wslzzy@outlook.com) + */ + + +struct ArrayQueue { + nums: Vec, + front: i32, + que_size: i32, + que_capacity: i32, +} + +impl ArrayQueue { + fn new(capacity: i32) -> ArrayQueue { + ArrayQueue { + nums: vec![0; capacity as usize], + front: 0, + que_size: 0, + que_capacity: capacity, + } + } + + fn size(&self) -> i32 { + self.que_size + } + + fn push(&mut self, num: i32) { + if self.que_size == self.que_capacity { + return; + } + let rear = (self.front + self.que_size) % self.que_capacity; + // self.nums.insert(rear as usize, num); + self.nums[rear as usize] = num; + self.que_size += 1; + } + + fn poll(&mut self) -> i32 { + let num = self.peek(); + self.front = (self.front + 1) % self.que_capacity; + self.que_size -= 1; + num + } + + fn peek(&self) -> i32 { + self.nums[self.front as usize] + } + + fn to_vector(&self) -> Vec { + let cap = self.que_capacity; + let mut j = self.front; + let mut arr = vec![0; self.que_size as usize]; + for i in 0..self.que_size { + arr[i as usize] = self.nums[(j % cap) as usize]; + j += 1; + } + 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); + queue.push(5); + queue.push(4); + println!("队列 queue = {:?}", queue.to_vector()); + + let peek = queue.peek(); + println!("队首元素 peek = {}", peek); + + let poll = queue.poll(); + println!( + "出队元素 poll = {:?},出队后 queue = {:?}", + poll, + 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.poll(); + println!("第 {:?} 轮入队 + 出队后 queue = {:?}", i, queue.to_vector()); + } +}