-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsingly_linked.rs
68 lines (58 loc) · 1.38 KB
/
singly_linked.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 定义节点结构体
#[derive(Debug)]
struct Node {
data: i32,
next: Option<Box<Node>>,
}
// 定义链表结构体
#[derive(Debug)]
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
// 初始化一个空链表
fn new() -> Self {
LinkedList { head: None }
}
// 打印链表
fn print_list(&self) {
let mut current = &self.head;
while let Some(node) = current {
print!("{} -> ", node.data);
current = &node.next;
}
println!("NULL");
}
// 插入新节点到链表尾部
fn append_node(&mut self, data: i32) {
let new_node = Box::new(Node {
data,
next: None,
});
if self.head.is_none() {
self.head = Some(new_node);
return;
}
let mut current = &mut self.head;
while let Some(node) = current {
if node.next.is_none() {
node.next = Some(new_node);
break;
}
current = &mut node.next;
}
}
}
fn main() {
let mut list = LinkedList::new();
// 插入节点
list.append_node(1);
list.append_node(2);
list.append_node(3);
list.print_list(); // 输出:1 -> 2 -> 3 -> NULL
}
/*
jarry@MacBook-Pro linked % rustc singly_linked.rs
jarry@MacBook-Pro linked % ./singly_linked
1 -> 2 -> 3 -> NULL
*/