-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathlinked_list.rs
59 lines (51 loc) · 1.38 KB
/
linked_list.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
// http://www.reddit.com/r/rust/comments/2jec05/problem_with_implementation_of_linked_list/
// Tested with rust-1.5.0
use std::fmt;
struct Node {
value: u32,
link: Option<Box<Node>>,
}
impl Node {
fn new(value: u32) -> Node {
Node { value: value, link: None, }
}
fn append(&mut self, value: u32) {
match self.link {
Some(ref mut node) => node.append(value),
None => self.link = Some(Box::new(Node::new(value))),
}
}
fn length(&self) -> u32 {
match self.link {
Some(ref node) => node.length() + 1,
None => 1,
}
}
fn insert_after(&mut self, value: u32, after: u32) -> bool {
if self.value == after {
self.link = Some(Box::new(Node { value: value, link: self.link.take() }));
true
}
else {
match self.link {
Some(ref mut node) => node.insert_after(value, after),
None => false,
}
}
}
}
impl fmt::Display for Node {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.link {
Some(ref node) => write!(f, "{} -> {}", self.value, node),
None => write!(f, "{}.", self.value)
}
}
}
fn main() {
let mut root = Node::new(0);
root.append(1);
root.insert_after(2, 1);
println!("Node length: {}", root.length());
println!("Node: {}", root);
}