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
use ::{List};
impl<T> List<T> {
pub fn front(&self) -> Option<&T> {
self.head.as_ref().map(|node| {
&node.value
})
}
pub fn front_mut(&mut self) -> Option<&mut T> {
self.head.as_mut().map(|node| {
&mut node.value
})
}
pub fn back(&self) -> Option<&T> {
let mut head_link = &self.head;
while let Some(ref node) = *head_link {
if node.next.is_none() {
return Some(&node.value);
}
head_link = &node.next;
}
None
}
pub fn back_mut(&mut self) -> Option<&mut T> {
self.penultimate_link().and_then(|link| {
link.as_mut().map(|node| {
&mut node.value
})
})
}
}