File tree Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -144,15 +144,15 @@ impl<T> LinkedList<T> {
144
144
}
145
145
146
146
pub fn delete_ith ( & mut self , index : u32 ) -> Option < T > {
147
- if self . length < index {
147
+ if self . length <= index {
148
148
panic ! ( "Index out of bounds" ) ;
149
149
}
150
150
151
151
if index == 0 || self . head . is_none ( ) {
152
152
return self . delete_head ( ) ;
153
153
}
154
154
155
- if self . length == index {
155
+ if self . length - 1 == index {
156
156
return self . delete_tail ( ) ;
157
157
}
158
158
@@ -499,4 +499,14 @@ mod tests {
499
499
assert ! ( retrived_item. is_some( ) ) ;
500
500
assert_eq ! ( "B" , * retrived_item. unwrap( ) ) ;
501
501
}
502
+
503
+ #[ test]
504
+ #[ should_panic( expected = "Index out of bounds" ) ]
505
+ fn delete_ith_panics_if_index_equals_length ( ) {
506
+ let mut list = LinkedList :: < i32 > :: new ( ) ;
507
+ list. insert_at_tail ( 1 ) ;
508
+ list. insert_at_tail ( 2 ) ;
509
+ // length is 2, so index 2 is out of bounds
510
+ list. delete_ith ( 2 ) ;
511
+ }
502
512
}
You can’t perform that action at this time.
0 commit comments