Skip to content

Commit c73e844

Browse files
committedJun 24, 2023
update
1 parent 1d6a1e1 commit c73e844

File tree

18 files changed

+175
-175
lines changed

18 files changed

+175
-175
lines changed
 

‎rust/chapter_array_and_linkedlist/linked_list.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ use std::rc::Rc;
1010
use std::cell::RefCell;
1111
use list_node::ListNode;
1212

13-
/* 在链表的结点 n0 之后插入结点 P */
13+
/* 在链表的节点 n0 之后插入节点 P */
1414
#[allow(non_snake_case)]
1515
pub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {
1616
let n1 = n0.borrow_mut().next.take();
1717
P.borrow_mut().next = n1;
1818
n0.borrow_mut().next = Some(P);
1919
}
2020

21-
/* 删除链表的结点 n0 之后的首个结点 */
21+
/* 删除链表的节点 n0 之后的首个节点 */
2222
#[allow(non_snake_case)]
2323
pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
2424
if n0.borrow().next.is_none() {return};
@@ -30,7 +30,7 @@ pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
3030
}
3131
}
3232

33-
/* 访问链表中索引为 index 的结点 */
33+
/* 访问链表中索引为 index 的节点 */
3434
pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {
3535
if index <= 0 {return head};
3636
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
3939
return head;
4040
}
4141

42-
/* 在链表中查找值为 target 的首个结点 */
42+
/* 在链表中查找值为 target 的首个节点 */
4343
pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {
4444
if head.borrow().val == target {return index};
4545
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)
5151
/* Driver Code */
5252
fn main() {
5353
/* 初始化链表 */
54-
// 初始化各个结点
54+
// 初始化各个节点
5555
let n0 = ListNode::new(1);
5656
let n1 = ListNode::new(3);
5757
let n2 = ListNode::new(2);
@@ -65,21 +65,21 @@ fn main() {
6565
print!("初始化的链表为");
6666
print_util::print_linked_list(&n0);
6767

68-
/* 插入结点 */
68+
/* 插入节点 */
6969
insert(&n0, ListNode::new(0));
70-
print!("插入结点后的链表为");
70+
print!("插入节点后的链表为");
7171
print_util::print_linked_list(&n0);
7272

73-
/* 删除结点 */
73+
/* 删除节点 */
7474
remove(&n0);
75-
print!("删除结点后的链表为");
75+
print!("删除节点后的链表为");
7676
print_util::print_linked_list(&n0);
7777

78-
/* 访问结点 */
78+
/* 访问节点 */
7979
let node = access(n0.clone(), 3);
80-
println!("链表中索引 3 处的结点的值 = {}", node.borrow().val);
80+
println!("链表中索引 3 处的节点的值 = {}", node.borrow().val);
8181

82-
/* 查找结点 */
82+
/* 查找节点 */
8383
let index = find(n0.clone(), 2, 0);
84-
println!("链表中值为 2 的结点的索引 = {}", index);
84+
println!("链表中值为 2 的节点的索引 = {}", index);
8585
}

‎rust/chapter_stack_and_queue/linkedlist_deque.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ include!("../include/include.rs");
99
use std::rc::Rc;
1010
use std::cell::RefCell;
1111

12-
/* 双向链表结点 */
12+
/* 双向链表节点 */
1313
pub struct ListNode<T> {
14-
pub val: T, // 结点值
15-
pub next: Option<Rc<RefCell<ListNode<T>>>>, // 后继结点引用(指针)
16-
pub prev: Option<Rc<RefCell<ListNode<T>>>>, // 前驱结点引用(指针)
14+
pub val: T, // 节点值
15+
pub next: Option<Rc<RefCell<ListNode<T>>>>, // 后继节点引用(指针)
16+
pub prev: Option<Rc<RefCell<ListNode<T>>>>, // 前驱节点引用(指针)
1717
}
1818

1919
impl<T> ListNode<T> {
@@ -29,8 +29,8 @@ impl<T> ListNode<T> {
2929
/* 基于双向链表实现的双向队列 */
3030
#[allow(dead_code)]
3131
pub struct LinkedListDeque<T> {
32-
front: Option<Rc<RefCell<ListNode<T>>>>, // 头结点 front
33-
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾结点 rear
32+
front: Option<Rc<RefCell<ListNode<T>>>>, // 头节点 front
33+
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾节点 rear
3434
que_size: usize, // 双向队列的长度
3535
}
3636

@@ -63,7 +63,7 @@ impl<T: Copy> LinkedListDeque<T> {
6363
Some(old_front) => {
6464
old_front.borrow_mut().prev = Some(node.clone());
6565
node.borrow_mut().next = Some(old_front);
66-
self.front = Some(node); // 更新头结点
66+
self.front = Some(node); // 更新头节点
6767
}
6868
None => {
6969
self.rear = Some(node.clone());
@@ -77,7 +77,7 @@ impl<T: Copy> LinkedListDeque<T> {
7777
Some(old_rear) => {
7878
old_rear.borrow_mut().next = Some(node.clone());
7979
node.borrow_mut().prev = Some(old_rear);
80-
self.rear = Some(node); // 更新尾结点
80+
self.rear = Some(node); // 更新尾节点
8181
}
8282
None => {
8383
self.front = Some(node.clone());
@@ -107,7 +107,7 @@ impl<T: Copy> LinkedListDeque<T> {
107107
match old_front.borrow_mut().next.take() {
108108
Some(new_front) => {
109109
new_front.borrow_mut().prev.take();
110-
self.front = Some(new_front); // 更新头结点
110+
self.front = Some(new_front); // 更新头节点
111111
}
112112
None => {
113113
self.rear.take();
@@ -122,7 +122,7 @@ impl<T: Copy> LinkedListDeque<T> {
122122
match old_rear.borrow_mut().prev.take() {
123123
Some(new_rear) => {
124124
new_rear.borrow_mut().next.take();
125-
self.rear = Some(new_rear); // 更新尾结点
125+
self.rear = Some(new_rear); // 更新尾节点
126126
}
127127
None => {
128128
self.front.take();

‎rust/chapter_stack_and_queue/linkedlist_queue.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use list_node::ListNode;
1313
/* 基于链表实现的队列 */
1414
#[allow(dead_code)]
1515
pub struct LinkedListQueue<T> {
16-
front: Option<Rc<RefCell<ListNode<T>>>>, // 头结点 front
17-
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾结点 rear
16+
front: Option<Rc<RefCell<ListNode<T>>>>, // 头节点 front
17+
rear: Option<Rc<RefCell<ListNode<T>>>>, // 尾节点 rear
1818
que_size: usize, // 队列的长度
1919
}
2020

@@ -39,15 +39,15 @@ impl<T: Copy> LinkedListQueue<T> {
3939

4040
/* 入队 */
4141
pub fn push(&mut self, num: T) {
42-
// 尾结点后添加 num
42+
// 尾节点后添加 num
4343
let new_rear = ListNode::new(num);
4444
match self.rear.take() {
45-
// 如果队列不为空,则将该结点添加到尾结点后
45+
// 如果队列不为空,则将该节点添加到尾节点后
4646
Some(old_rear) => {
4747
old_rear.borrow_mut().next = Some(new_rear.clone());
4848
self.rear = Some(new_rear);
4949
}
50-
// 如果队列为空,则令头、尾结点都指向该结点
50+
// 如果队列为空,则令头、尾节点都指向该节点
5151
None => {
5252
self.front = Some(new_rear.clone());
5353
self.rear = Some(new_rear);

‎rust/chapter_stack_and_queue/linkedlist_stack.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use list_node::ListNode;
1313
/* 基于链表实现的栈 */
1414
#[allow(dead_code)]
1515
pub struct LinkedListStack<T> {
16-
stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // 将头结点作为栈顶
16+
stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // 将头节点作为栈顶
1717
stk_size: usize, // 栈的长度
1818
}
1919

‎zig/chapter_array_and_linkedlist/linked_list.zig

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
const std = @import("std");
66
const inc = @import("include");
77

8-
// 在链表的结点 n0 之后插入结点 P
8+
// 在链表的节点 n0 之后插入节点 P
99
pub fn insert(n0: ?*inc.ListNode(i32), P: ?*inc.ListNode(i32)) void {
1010
var n1 = n0.?.next;
1111
n0.?.next = P;
1212
P.?.next = n1;
1313
}
1414

15-
// 删除链表的结点 n0 之后的首个结点
15+
// 删除链表的节点 n0 之后的首个节点
1616
pub fn remove(n0: ?*inc.ListNode(i32)) void {
1717
if (n0.?.next == null) return;
1818
// n0 -> P -> n1
@@ -21,7 +21,7 @@ pub fn remove(n0: ?*inc.ListNode(i32)) void {
2121
n0.?.next = n1;
2222
}
2323

24-
// 访问链表中索引为 index 的结点
24+
// 访问链表中索引为 index 的节点
2525
pub fn access(node: ?*inc.ListNode(i32), index: i32) ?*inc.ListNode(i32) {
2626
var head = node;
2727
var i: i32 = 0;
@@ -32,7 +32,7 @@ pub fn access(node: ?*inc.ListNode(i32), index: i32) ?*inc.ListNode(i32) {
3232
return head;
3333
}
3434

35-
// 在链表中查找值为 target 的首个结点
35+
// 在链表中查找值为 target 的首个节点
3636
pub fn find(node: ?*inc.ListNode(i32), target: i32) i32 {
3737
var head = node;
3838
var index: i32 = 0;
@@ -47,7 +47,7 @@ pub fn find(node: ?*inc.ListNode(i32), target: i32) i32 {
4747
// Driver Code
4848
pub fn main() !void {
4949
// 初始化链表
50-
// 初始化各个结点
50+
// 初始化各个节点
5151
var n0 = inc.ListNode(i32){.val = 1};
5252
var n1 = inc.ListNode(i32){.val = 3};
5353
var n2 = inc.ListNode(i32){.val = 2};
@@ -61,24 +61,24 @@ pub fn main() !void {
6161
std.debug.print("初始化的链表为", .{});
6262
try inc.PrintUtil.printLinkedList(i32, &n0);
6363

64-
// 插入结点
64+
// 插入节点
6565
var tmp = inc.ListNode(i32){.val = 0};
6666
insert(&n0, &tmp);
67-
std.debug.print("插入结点后的链表为", .{});
67+
std.debug.print("插入节点后的链表为", .{});
6868
try inc.PrintUtil.printLinkedList(i32, &n0);
6969

70-
// 删除结点
70+
// 删除节点
7171
remove(&n0);
72-
std.debug.print("删除结点后的链表为", .{});
72+
std.debug.print("删除节点后的链表为", .{});
7373
try inc.PrintUtil.printLinkedList(i32, &n0);
7474

75-
// 访问结点
75+
// 访问节点
7676
var node = access(&n0, 3);
77-
std.debug.print("链表中索引 3 处的结点的值 = {}\n", .{node.?.val});
77+
std.debug.print("链表中索引 3 处的节点的值 = {}\n", .{node.?.val});
7878

79-
// 查找结点
79+
// 查找节点
8080
var index = find(&n0, 2);
81-
std.debug.print("链表中值为 2 的结点的索引 = {}\n", .{index});
81+
std.debug.print("链表中值为 2 的节点的索引 = {}\n", .{index});
8282

8383
_ = try std.io.getStdIn().reader().readByte();
8484
}

‎zig/chapter_heap/my_heap.zig

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn MaxHeap(comptime T: type) type {
1818
self.max_heap = std.ArrayList(T).init(allocator);
1919
// 将列表元素原封不动添加进堆
2020
try self.max_heap.?.appendSlice(nums);
21-
// 堆化除叶结点以外的其他所有结点
21+
// 堆化除叶节点以外的其他所有节点
2222
var i: usize = parent(self.size() - 1) + 1;
2323
while (i > 0) : (i -= 1) {
2424
try self.siftDown(i - 1);
@@ -30,17 +30,17 @@ pub fn MaxHeap(comptime T: type) type {
3030
if (self.max_heap != null) self.max_heap.?.deinit();
3131
}
3232

33-
// 获取左子结点索引
33+
// 获取左子节点索引
3434
fn left(i: usize) usize {
3535
return 2 * i + 1;
3636
}
3737

38-
// 获取右子结点索引
38+
// 获取右子节点索引
3939
fn right(i: usize) usize {
4040
return 2 * i + 2;
4141
}
4242

43-
// 获取父结点索引
43+
// 获取父节点索引
4444
fn parent(i: usize) usize {
4545
// return (i - 1) / 2; // 向下整除
4646
return @divFloor(i - 1, 2);
@@ -70,21 +70,21 @@ pub fn MaxHeap(comptime T: type) type {
7070

7171
// 元素入堆
7272
pub fn push(self: *Self, val: T) !void {
73-
// 添加结点
73+
// 添加节点
7474
try self.max_heap.?.append(val);
7575
// 从底至顶堆化
7676
try self.siftUp(self.size() - 1);
7777
}
7878

79-
// 从结点 i 开始,从底至顶堆化
79+
// 从节点 i 开始,从底至顶堆化
8080
fn siftUp(self: *Self, i_: usize) !void {
8181
var i = i_;
8282
while (true) {
83-
// 获取结点 i 的父结点
83+
// 获取节点 i 的父节点
8484
var p = parent(i);
85-
// 当“越过根结点”或“结点无需修复”时,结束堆化
85+
// 当“越过根节点”或“节点无需修复”时,结束堆化
8686
if (p < 0 or self.max_heap.?.items[i] <= self.max_heap.?.items[p]) break;
87-
// 交换两结点
87+
// 交换两节点
8888
try self.swap(i, p);
8989
// 循环向上堆化
9090
i = p;
@@ -95,29 +95,29 @@ pub fn MaxHeap(comptime T: type) type {
9595
pub fn pop(self: *Self) !T {
9696
// 判断处理
9797
if (self.isEmpty()) unreachable;
98-
// 交换根结点与最右叶结点(即交换首元素与尾元素)
98+
// 交换根节点与最右叶节点(即交换首元素与尾元素)
9999
try self.swap(0, self.size() - 1);
100-
// 删除结点
100+
// 删除节点
101101
var val = self.max_heap.?.pop();
102102
// 从顶至底堆化
103103
try self.siftDown(0);
104104
// 返回堆顶元素
105105
return val;
106106
}
107107

108-
// 从结点 i 开始,从顶至底堆化
108+
// 从节点 i 开始,从顶至底堆化
109109
fn siftDown(self: *Self, i_: usize) !void {
110110
var i = i_;
111111
while (true) {
112-
// 判断结点 i, l, r 中值最大的结点,记为 ma
112+
// 判断节点 i, l, r 中值最大的节点,记为 ma
113113
var l = left(i);
114114
var r = right(i);
115115
var ma = i;
116116
if (l < self.size() and self.max_heap.?.items[l] > self.max_heap.?.items[ma]) ma = l;
117117
if (r < self.size() and self.max_heap.?.items[r] > self.max_heap.?.items[ma]) ma = r;
118-
// 若结点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
118+
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
119119
if (ma == i) break;
120-
// 交换两结点
120+
// 交换两节点
121121
try self.swap(i, ma);
122122
// 循环向下堆化
123123
i = ma;

‎zig/chapter_searching/hashing_search.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn hashingSearch(comptime T: type, map: std.AutoHashMap(T, T), target: T) T {
1515

1616
// 哈希查找(数组)
1717
fn hashingSearch1(comptime T: type, map: std.AutoHashMap(T, *inc.ListNode(T)), target: T) ?*inc.ListNode(T) {
18-
// 哈希表的 key: 目标结点值,value: 结点对象
18+
// 哈希表的 key: 目标节点值,value: 节点对象
1919
// 若哈希表中无此 key ,返回 null
2020
if (map.getKey(target) == null) return null;
2121
return map.get(target);
@@ -49,7 +49,7 @@ pub fn main() !void {
4949
head = head.?.next;
5050
}
5151
var node = hashingSearch1(i32, map1, target);
52-
std.debug.print("目标结点值 3 的对应结点对象为 ", .{});
52+
std.debug.print("目标节点值 3 的对应节点对象为 ", .{});
5353
try inc.PrintUtil.printLinkedList(i32, node);
5454

5555
_ = try std.io.getStdIn().reader().readByte();

‎zig/chapter_searching/linear_search.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn linearSearchLinkedList(comptime T: type, node: ?*inc.ListNode(T), target:
2323
var head = node;
2424
// 遍历链表
2525
while (head != null) {
26-
// 找到目标结点,返回之
26+
// 找到目标节点,返回之
2727
if (head.?.val == target) return head;
2828
head = head.?.next;
2929
}
@@ -47,7 +47,7 @@ pub fn main() !void {
4747
const mem_allocator = mem_arena.allocator();
4848
var head = try inc.ListUtil.listToLinkedList(i32, mem_allocator, nums);
4949
var node = linearSearchLinkedList(i32, head, target);
50-
std.debug.print("目标结点值 3 的对应结点对象为 ", .{});
50+
std.debug.print("目标节点值 3 的对应节点对象为 ", .{});
5151
try inc.PrintUtil.printLinkedList(i32, node);
5252

5353
_ = try std.io.getStdIn().reader().readByte();

‎zig/chapter_stack_and_queue/array_queue.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn ArrayQueue(comptime T: type) type {
5858
// 计算尾指针,指向队尾索引 + 1
5959
// 通过取余操作,实现 rear 越过数组尾部后回到头部
6060
var rear = (self.front + self.queSize) % self.capacity();
61-
// 尾结点后添加 num
61+
// 尾节点后添加 num
6262
self.nums[rear] = num;
6363
self.queSize += 1;
6464
}

0 commit comments

Comments
 (0)
Please sign in to comment.