mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-04 19:20:52 +08:00
Update the book based on the revised second edition (#1014)
* Revised the book * Update the book with the second revised edition * Revise base on the manuscript of the first edition
This commit is contained in:
@@ -53,7 +53,7 @@ impl ArrayDeque {
|
||||
return
|
||||
}
|
||||
// 队首指针向左移动一位
|
||||
// 通过取余操作,实现 front 越过数组头部后回到尾部
|
||||
// 通过取余操作实现 front 越过数组头部后回到尾部
|
||||
self.front = self.index(self.front as i32 - 1);
|
||||
// 将 num 添加至队首
|
||||
self.nums[self.front] = num;
|
||||
@@ -66,7 +66,7 @@ impl ArrayDeque {
|
||||
println!("双向队列已满");
|
||||
return
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
// 计算队尾指针,指向队尾索引 + 1
|
||||
let rear = self.index(self.front as i32 + self.que_size as i32);
|
||||
// 将 num 添加至队尾
|
||||
self.nums[rear] = num;
|
||||
|
||||
@@ -44,8 +44,8 @@ impl ArrayQueue {
|
||||
println!("队列已满");
|
||||
return;
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
||||
// 计算队尾指针,指向队尾索引 + 1
|
||||
// 通过取余操作实现 rear 越过数组尾部后回到头部
|
||||
let rear = (self.front + self.que_size) % self.que_capacity;
|
||||
// 将 num 添加至队尾
|
||||
self.nums[rear as usize] = num;
|
||||
@@ -55,7 +55,7 @@ impl ArrayQueue {
|
||||
/* 出队 */
|
||||
fn pop(&mut self) -> i32 {
|
||||
let num = self.peek();
|
||||
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
||||
// 队首指针向后移动一位,若越过尾部,则返回到数组头部
|
||||
self.front = (self.front + 1) % self.que_capacity;
|
||||
self.que_size -= 1;
|
||||
num
|
||||
|
||||
@@ -39,7 +39,7 @@ impl<T: Copy> LinkedListQueue<T> {
|
||||
|
||||
/* 入队 */
|
||||
pub fn push(&mut self, num: T) {
|
||||
// 尾节点后添加 num
|
||||
// 在尾节点后添加 num
|
||||
let new_rear = ListNode::new(num);
|
||||
match self.rear.take() {
|
||||
// 如果队列不为空,则将该节点添加到尾节点后
|
||||
|
||||
Reference in New Issue
Block a user