mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-04 19:54:20 +00:00
build
This commit is contained in:
@@ -1609,11 +1609,11 @@ comments: true
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
pub fn is_empty(&self) -> bool {
|
||||
return self.size() == 0;
|
||||
return self.que_size == 0;
|
||||
}
|
||||
|
||||
/* 入队操作 */
|
||||
pub fn push(&mut self, num: T, is_front: bool) {
|
||||
fn push(&mut self, num: T, is_front: bool) {
|
||||
let node = ListNode::new(num);
|
||||
// 队首入队操作
|
||||
if is_front {
|
||||
@@ -1661,7 +1661,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 出队操作 */
|
||||
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;
|
||||
@@ -3320,17 +3320,17 @@ comments: true
|
||||
|
||||
```rust title="array_deque.rs"
|
||||
/* 基于环形数组实现的双向队列 */
|
||||
struct ArrayDeque {
|
||||
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,
|
||||
}
|
||||
@@ -3356,11 +3356,11 @@ comments: true
|
||||
// 通过取余操作实现数组首尾相连
|
||||
// 当 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;
|
||||
@@ -3374,7 +3374,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 队尾入队 */
|
||||
pub fn push_last(&mut self, num: i32) {
|
||||
pub fn push_last(&mut self, num: T) {
|
||||
if self.que_size == self.capacity() {
|
||||
println!("双向队列已满");
|
||||
return;
|
||||
@@ -3387,7 +3387,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 队首出队 */
|
||||
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);
|
||||
@@ -3396,14 +3396,14 @@ comments: true
|
||||
}
|
||||
|
||||
/* 队尾出队 */
|
||||
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!("双向队列为空")
|
||||
};
|
||||
@@ -3411,7 +3411,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
fn peek_last(&self) -> i32 {
|
||||
fn peek_last(&self) -> T {
|
||||
if self.is_empty() {
|
||||
panic!("双向队列为空")
|
||||
};
|
||||
@@ -3421,9 +3421,9 @@ comments: true
|
||||
}
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
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)];
|
||||
|
||||
@@ -1036,7 +1036,7 @@ comments: true
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
pub fn is_empty(&self) -> bool {
|
||||
return self.size() == 0;
|
||||
return self.que_size == 0;
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
@@ -2082,18 +2082,18 @@ comments: true
|
||||
|
||||
```rust title="array_queue.rs"
|
||||
/* 基于环形数组实现的队列 */
|
||||
struct ArrayQueue {
|
||||
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,
|
||||
@@ -2116,7 +2116,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
fn push(&mut self, num: i32) {
|
||||
fn push(&mut self, num: T) {
|
||||
if self.que_size == self.capacity() {
|
||||
println!("队列已满");
|
||||
return;
|
||||
@@ -2130,7 +2130,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
fn pop(&mut self) -> i32 {
|
||||
fn pop(&mut self) -> T {
|
||||
let num = self.peek();
|
||||
// 队首指针向后移动一位,若越过尾部,则返回到数组头部
|
||||
self.front = (self.front + 1) % self.que_capacity;
|
||||
@@ -2139,7 +2139,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
fn peek(&self) -> i32 {
|
||||
fn peek(&self) -> T {
|
||||
if self.is_empty() {
|
||||
panic!("index out of bounds");
|
||||
}
|
||||
@@ -2147,10 +2147,10 @@ comments: true
|
||||
}
|
||||
|
||||
/* 返回数组 */
|
||||
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;
|
||||
|
||||
@@ -977,13 +977,17 @@ comments: true
|
||||
}
|
||||
|
||||
/* 将 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> {
|
||||
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())
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user