This commit is contained in:
krahets
2024-03-18 03:11:07 +08:00
parent bc0054a577
commit 54c7448946
48 changed files with 577 additions and 408 deletions

View File

@ -1522,9 +1522,9 @@ The implementation code is as follows:
/* 基于双向链表实现的双向队列 */
#[allow(dead_code)]
pub struct LinkedListDeque<T> {
front: Option<Rc<RefCell<ListNode<T>>>>, // 头节点 front
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾节点 rear
que_size: usize, // 双向队列的长度
front: Option<Rc<RefCell<ListNode<T>>>>, // 头节点 front
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾节点 rear
que_size: usize, // 双向队列的长度
}
impl<T: Copy> LinkedListDeque<T> {
@ -1532,7 +1532,7 @@ The implementation code is as follows:
Self {
front: None,
rear: None,
que_size: 0,
que_size: 0,
}
}
@ -1564,7 +1564,7 @@ The implementation code is as follows:
self.front = Some(node); // 更新头节点
}
}
}
}
// 队尾入队操作
else {
match self.rear.take() {
@ -1597,8 +1597,8 @@ The implementation code is as follows:
/* 出队操作 */
pub fn pop(&mut self, is_front: bool) -> Option<T> {
// 若队列为空,直接返回 None
if self.is_empty() {
return None
if self.is_empty() {
return None;
};
// 队首出队操作
if is_front {
@ -1606,7 +1606,7 @@ The implementation code is as follows:
match old_front.borrow_mut().next.take() {
Some(new_front) => {
new_front.borrow_mut().prev.take();
self.front = Some(new_front); // 更新头节点
self.front = Some(new_front); // 更新头节点
}
None => {
self.rear.take();
@ -1615,15 +1615,14 @@ The implementation code is as follows:
self.que_size -= 1; // 更新队列长度
Rc::try_unwrap(old_front).ok().unwrap().into_inner().val
})
}
}
// 队尾出队操作
else {
self.rear.take().map(|old_rear| {
match old_rear.borrow_mut().prev.take() {
Some(new_rear) => {
new_rear.borrow_mut().next.take();
self.rear = Some(new_rear); // 更新尾节点
self.rear = Some(new_rear); // 更新尾节点
}
None => {
self.front.take();

View File

@ -959,9 +959,9 @@ Below is the code for implementing a queue using a linked list:
/* 基于链表实现的队列 */
#[allow(dead_code)]
pub struct LinkedListQueue<T> {
front: Option<Rc<RefCell<ListNode<T>>>>, // 头节点 front
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾节点 rear
que_size: usize, // 队列的长度
front: Option<Rc<RefCell<ListNode<T>>>>, // 头节点 front
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾节点 rear
que_size: usize, // 队列的长度
}
impl<T: Copy> LinkedListQueue<T> {
@ -969,7 +969,7 @@ Below is the code for implementing a queue using a linked list:
Self {
front: None,
rear: None,
que_size: 0,
que_size: 0,
}
}
@ -1887,10 +1887,10 @@ For a circular array, `front` or `rear` needs to loop back to the start of the a
```rust title="array_queue.rs"
/* 基于环形数组实现的队列 */
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 {

View File

@ -876,8 +876,8 @@ Below is an example code for implementing a stack based on a linked list:
/* 基于链表实现的栈 */
#[allow(dead_code)]
pub struct LinkedListStack<T> {
stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // 将头节点作为栈顶
stk_size: usize, // 栈的长度
stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // 将头节点作为栈顶
stk_size: usize, // 栈的长度
}
impl<T: Copy> LinkedListStack<T> {
@ -1537,7 +1537,9 @@ Since the elements to be pushed onto the stack may continuously increase, we can
impl<T> ArrayStack<T> {
/* 初始化栈 */
fn new() -> ArrayStack<T> {
ArrayStack::<T> { stack: Vec::<T>::new() }
ArrayStack::<T> {
stack: Vec::<T>::new(),
}
}
/* 获取栈的长度 */
@ -1565,7 +1567,9 @@ Since the elements to be pushed onto the stack may continuously increase, we can
/* 访问栈顶元素 */
fn peek(&self) -> Option<&T> {
if self.is_empty() { panic!("栈为空") };
if self.is_empty() {
panic!("栈为空")
};
self.stack.last()
}