@@ -16,41 +16,29 @@ mod tests;
16
16
17
17
#[ derive( Clone ) ]
18
18
pub struct TinyList < T : PartialEq > {
19
- head : Option < Element < T > >
19
+ head : Option < Element < T > > ,
20
20
}
21
21
22
22
impl < T : PartialEq > TinyList < T > {
23
23
#[ inline]
24
24
pub fn new ( ) -> TinyList < T > {
25
- TinyList {
26
- head : None
27
- }
25
+ TinyList { head : None }
28
26
}
29
27
30
28
#[ inline]
31
29
pub fn new_single ( data : T ) -> TinyList < T > {
32
- TinyList {
33
- head : Some ( Element {
34
- data,
35
- next : None ,
36
- } )
37
- }
30
+ TinyList { head : Some ( Element { data, next : None } ) }
38
31
}
39
32
40
33
#[ inline]
41
34
pub fn insert ( & mut self , data : T ) {
42
- self . head = Some ( Element {
43
- data,
44
- next : self . head . take ( ) . map ( Box :: new)
45
- } ) ;
35
+ self . head = Some ( Element { data, next : self . head . take ( ) . map ( Box :: new) } ) ;
46
36
}
47
37
48
38
#[ inline]
49
39
pub fn remove ( & mut self , data : & T ) -> bool {
50
40
self . head = match self . head {
51
- Some ( ref mut head) if head. data == * data => {
52
- head. next . take ( ) . map ( |x| * x)
53
- }
41
+ Some ( ref mut head) if head. data == * data => head. next . take ( ) . map ( |x| * x) ,
54
42
Some ( ref mut head) => return head. remove_next ( data) ,
55
43
None => return false ,
56
44
} ;
@@ -88,12 +76,16 @@ struct Element<T: PartialEq> {
88
76
89
77
impl < T : PartialEq > Element < T > {
90
78
fn remove_next ( & mut self , data : & T ) -> bool {
91
- let new_next = match self . next {
92
- Some ( ref mut next) if next. data == * data => next. next . take ( ) ,
93
- Some ( ref mut next) => return next. remove_next ( data) ,
94
- None => return false ,
95
- } ;
96
- self . next = new_next;
97
- true
79
+ let mut n = self ;
80
+ loop {
81
+ match n. next {
82
+ Some ( ref mut next) if next. data == * data => {
83
+ n. next = next. next . take ( ) ;
84
+ return true ;
85
+ }
86
+ Some ( ref mut next) => n = next,
87
+ None => return false ,
88
+ }
89
+ }
98
90
}
99
91
}
0 commit comments