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 <wslzzy@qq.com>
pull/361/head
WangSL 2 years ago committed by GitHub
parent ab0b432fcc
commit ea5677b3c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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"
rand = "0.8.5"

@ -0,0 +1,98 @@
/*
* File: array_queue.rs
* Created Time: 2023-02-06
* Author: WSL0809 (wslzzy@outlook.com)
*/
struct ArrayQueue {
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],
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<i32> {
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());
}
}
Loading…
Cancel
Save