File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -101,6 +101,7 @@ impl<T> LinkedList<T> {
101101 let node_ptr = Some ( NonNull :: new_unchecked ( Box :: into_raw ( node) ) ) ;
102102 println ! ( "{:?}" , ( * p. as_ptr( ) ) . next) ;
103103 ( * p. as_ptr ( ) ) . next = node_ptr;
104+ ( * ith_node. as_ptr ( ) ) . prev = node_ptr;
104105 self . length += 1 ;
105106 }
106107 }
@@ -300,6 +301,36 @@ mod tests {
300301 }
301302 }
302303
304+ #[ test]
305+ fn insert_at_ith_and_delete_at_ith_in_the_middle ( ) {
306+ // Insert and delete in the middle of the list to ensure pointers are updated correctly
307+ let mut list = LinkedList :: < i32 > :: new ( ) ;
308+ let first_value = 0 ;
309+ let second_value = 1 ;
310+ let third_value = 2 ;
311+ let fourth_value = 3 ;
312+
313+ list. insert_at_ith ( 0 , first_value) ;
314+ list. insert_at_ith ( 1 , fourth_value) ;
315+ list. insert_at_ith ( 1 , third_value) ;
316+ list. insert_at_ith ( 1 , second_value) ;
317+
318+ list. delete_ith ( 2 ) ;
319+ list. insert_at_ith ( 2 , third_value) ;
320+
321+ for ( i, expected) in [
322+ ( 0 , first_value) ,
323+ ( 1 , second_value) ,
324+ ( 2 , third_value) ,
325+ ( 3 , fourth_value) ,
326+ ] {
327+ match list. get ( i) {
328+ Some ( val) => assert_eq ! ( * val, expected) ,
329+ None => panic ! ( "Expected to find {} at index {}" , expected, i) ,
330+ }
331+ }
332+ }
333+
303334 #[ test]
304335 fn insert_at_ith_and_delete_ith_work_over_many_iterations ( ) {
305336 let mut list = LinkedList :: < i32 > :: new ( ) ;
You can’t perform that action at this time.
0 commit comments