@@ -16,41 +16,29 @@ mod tests;
1616
1717#[ derive( Clone ) ]
1818pub struct TinyList < T : PartialEq > {
19- head : Option < Element < T > >
19+ head : Option < Element < T > > ,
2020}
2121
2222impl < T : PartialEq > TinyList < T > {
2323 #[ inline]
2424 pub fn new ( ) -> TinyList < T > {
25- TinyList {
26- head : None
27- }
25+ TinyList { head : None }
2826 }
2927
3028 #[ inline]
3129 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 } ) }
3831 }
3932
4033 #[ inline]
4134 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) } ) ;
4636 }
4737
4838 #[ inline]
4939 pub fn remove ( & mut self , data : & T ) -> bool {
5040 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) ,
5442 Some ( ref mut head) => return head. remove_next ( data) ,
5543 None => return false ,
5644 } ;
@@ -88,12 +76,16 @@ struct Element<T: PartialEq> {
8876
8977impl < T : PartialEq > Element < T > {
9078 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+ }
9890 }
9991}
0 commit comments