mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 16:19:46 +08: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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user