mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-15 08:26:06 +00:00
refactor: Replace 结点 with 节点 (#452)
* Replace 结点 with 节点 Update the footnotes in the figures * Update mindmap * Reduce the size of the mindmap.png
This commit is contained in:
@@ -10,7 +10,7 @@ use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use list_node::ListNode;
|
||||
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
/* 在链表的节点 n0 之后插入节点 P */
|
||||
#[allow(non_snake_case)]
|
||||
pub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {
|
||||
let n1 = n0.borrow_mut().next.take();
|
||||
@@ -18,7 +18,7 @@ pub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {
|
||||
n0.borrow_mut().next = Some(P);
|
||||
}
|
||||
|
||||
/* 删除链表的结点 n0 之后的首个结点 */
|
||||
/* 删除链表的节点 n0 之后的首个节点 */
|
||||
#[allow(non_snake_case)]
|
||||
pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
|
||||
if n0.borrow().next.is_none() {return};
|
||||
@@ -30,7 +30,7 @@ pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
|
||||
}
|
||||
}
|
||||
|
||||
/* 访问链表中索引为 index 的结点 */
|
||||
/* 访问链表中索引为 index 的节点 */
|
||||
pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {
|
||||
if index <= 0 {return head};
|
||||
if let Some(node) = &head.borrow_mut().next {
|
||||
@@ -39,7 +39,7 @@ pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListN
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 在链表中查找值为 target 的首个结点 */
|
||||
/* 在链表中查找值为 target 的首个节点 */
|
||||
pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {
|
||||
if head.borrow().val == target {return index};
|
||||
if let Some(node) = &head.borrow_mut().next {
|
||||
@@ -51,7 +51,7 @@ pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32)
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* 初始化链表 */
|
||||
// 初始化各个结点
|
||||
// 初始化各个节点
|
||||
let n0 = ListNode::new(1);
|
||||
let n1 = ListNode::new(3);
|
||||
let n2 = ListNode::new(2);
|
||||
@@ -65,21 +65,21 @@ fn main() {
|
||||
print!("初始化的链表为 ");
|
||||
print_util::print_linked_list(&n0);
|
||||
|
||||
/* 插入结点 */
|
||||
/* 插入节点 */
|
||||
insert(&n0, ListNode::new(0));
|
||||
print!("插入结点后的链表为 ");
|
||||
print!("插入节点后的链表为 ");
|
||||
print_util::print_linked_list(&n0);
|
||||
|
||||
/* 删除结点 */
|
||||
/* 删除节点 */
|
||||
remove(&n0);
|
||||
print!("删除结点后的链表为 ");
|
||||
print!("删除节点后的链表为 ");
|
||||
print_util::print_linked_list(&n0);
|
||||
|
||||
/* 访问结点 */
|
||||
/* 访问节点 */
|
||||
let node = access(n0.clone(), 3);
|
||||
println!("链表中索引 3 处的结点的值 = {}", node.borrow().val);
|
||||
println!("链表中索引 3 处的节点的值 = {}", node.borrow().val);
|
||||
|
||||
/* 查找结点 */
|
||||
/* 查找节点 */
|
||||
let index = find(n0.clone(), 2, 0);
|
||||
println!("链表中值为 2 的结点的索引 = {}", index);
|
||||
println!("链表中值为 2 的节点的索引 = {}", index);
|
||||
}
|
||||
|
||||
@@ -9,11 +9,11 @@ include!("../include/include.rs");
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
|
||||
/* 双向链表结点 */
|
||||
/* 双向链表节点 */
|
||||
pub struct ListNode<T> {
|
||||
pub val: T, // 结点值
|
||||
pub next: Option<Rc<RefCell<ListNode<T>>>>, // 后继结点引用(指针)
|
||||
pub prev: Option<Rc<RefCell<ListNode<T>>>>, // 前驱结点引用(指针)
|
||||
pub val: T, // 节点值
|
||||
pub next: Option<Rc<RefCell<ListNode<T>>>>, // 后继节点引用(指针)
|
||||
pub prev: Option<Rc<RefCell<ListNode<T>>>>, // 前驱节点引用(指针)
|
||||
}
|
||||
|
||||
impl<T> ListNode<T> {
|
||||
@@ -29,8 +29,8 @@ impl<T> ListNode<T> {
|
||||
/* 基于双向链表实现的双向队列 */
|
||||
#[allow(dead_code)]
|
||||
pub struct LinkedListDeque<T> {
|
||||
front: Option<Rc<RefCell<ListNode<T>>>>, // 头结点 front
|
||||
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾结点 rear
|
||||
front: Option<Rc<RefCell<ListNode<T>>>>, // 头节点 front
|
||||
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾节点 rear
|
||||
que_size: usize, // 双向队列的长度
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ impl<T: Copy> LinkedListDeque<T> {
|
||||
Some(old_front) => {
|
||||
old_front.borrow_mut().prev = Some(node.clone());
|
||||
node.borrow_mut().next = Some(old_front);
|
||||
self.front = Some(node); // 更新头结点
|
||||
self.front = Some(node); // 更新头节点
|
||||
}
|
||||
None => {
|
||||
self.rear = Some(node.clone());
|
||||
@@ -77,7 +77,7 @@ impl<T: Copy> LinkedListDeque<T> {
|
||||
Some(old_rear) => {
|
||||
old_rear.borrow_mut().next = Some(node.clone());
|
||||
node.borrow_mut().prev = Some(old_rear);
|
||||
self.rear = Some(node); // 更新尾结点
|
||||
self.rear = Some(node); // 更新尾节点
|
||||
}
|
||||
None => {
|
||||
self.front = Some(node.clone());
|
||||
@@ -107,7 +107,7 @@ impl<T: Copy> LinkedListDeque<T> {
|
||||
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();
|
||||
@@ -122,7 +122,7 @@ impl<T: Copy> LinkedListDeque<T> {
|
||||
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();
|
||||
|
||||
@@ -13,8 +13,8 @@ use list_node::ListNode;
|
||||
/* 基于链表实现的队列 */
|
||||
#[allow(dead_code)]
|
||||
pub struct LinkedListQueue<T> {
|
||||
front: Option<Rc<RefCell<ListNode<T>>>>, // 头结点 front
|
||||
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾结点 rear
|
||||
front: Option<Rc<RefCell<ListNode<T>>>>, // 头节点 front
|
||||
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾节点 rear
|
||||
que_size: usize, // 队列的长度
|
||||
}
|
||||
|
||||
@@ -39,15 +39,15 @@ impl<T: Copy> LinkedListQueue<T> {
|
||||
|
||||
/* 入队 */
|
||||
pub fn push(&mut self, num: T) {
|
||||
// 尾结点后添加 num
|
||||
// 尾节点后添加 num
|
||||
let new_rear = ListNode::new(num);
|
||||
match self.rear.take() {
|
||||
// 如果队列不为空,则将该结点添加到尾结点后
|
||||
// 如果队列不为空,则将该节点添加到尾节点后
|
||||
Some(old_rear) => {
|
||||
old_rear.borrow_mut().next = Some(new_rear.clone());
|
||||
self.rear = Some(new_rear);
|
||||
}
|
||||
// 如果队列为空,则令头、尾结点都指向该结点
|
||||
// 如果队列为空,则令头、尾节点都指向该节点
|
||||
None => {
|
||||
self.front = Some(new_rear.clone());
|
||||
self.rear = Some(new_rear);
|
||||
|
||||
@@ -13,7 +13,7 @@ use list_node::ListNode;
|
||||
/* 基于链表实现的栈 */
|
||||
#[allow(dead_code)]
|
||||
pub struct LinkedListStack<T> {
|
||||
stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // 将头结点作为栈顶
|
||||
stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // 将头节点作为栈顶
|
||||
stk_size: usize, // 栈的长度
|
||||
}
|
||||
|
||||
|
||||
@@ -24,16 +24,16 @@ fn main() {
|
||||
println!("\n初始化二叉树\n");
|
||||
print_util::print_tree(&n1);
|
||||
|
||||
// 插入结点与删除结点
|
||||
// 插入节点与删除节点
|
||||
let p = TreeNode::new(0);
|
||||
// 在 n1 -> n2 中间插入结点 P
|
||||
// 在 n1 -> n2 中间插入节点 P
|
||||
p.borrow_mut().left = Some(Rc::clone(&n2));
|
||||
n1.borrow_mut().left = Some(Rc::clone(&p));
|
||||
println!("\n插入结点 P 后\n");
|
||||
println!("\n插入节点 P 后\n");
|
||||
print_util::print_tree(&n1);
|
||||
// 删除结点 P
|
||||
// 删除节点 P
|
||||
drop(p);
|
||||
n1.borrow_mut().left = Some(Rc::clone(&n2));
|
||||
println!("\n删除结点 P 后\n");
|
||||
println!("\n删除节点 P 后\n");
|
||||
print_util::print_tree(&n1);
|
||||
}
|
||||
Reference in New Issue
Block a user