Skip to content

Commit 94bc317

Browse files
04121604
1 parent cb198f5 commit 94bc317

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

exercises/algorithm/algorithm2.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
double linked list reverse
33
This problem requires you to reverse a doubly linked list
44
*/
5-
// I AM NOT DONE
65

76
use std::fmt::{self, Display, Formatter};
87
use std::ptr::NonNull;
9-
use std::vec::*;
108

119
#[derive(Debug)]
1210
struct Node<T> {
@@ -24,6 +22,7 @@ impl<T> Node<T> {
2422
}
2523
}
2624
}
25+
2726
#[derive(Debug)]
2827
struct LinkedList<T> {
2928
length: u32,
@@ -73,7 +72,16 @@ impl<T> LinkedList<T> {
7372
}
7473
}
7574
pub fn reverse(&mut self) {
76-
// TODO
75+
let mut curr = self.start;
76+
77+
while let Some(curr_ptr) = curr {
78+
unsafe {
79+
let node = curr_ptr.as_ptr();
80+
std::mem::swap(&mut (*node).prev, &mut (*node).next);
81+
curr = (*node).prev;
82+
}
83+
}
84+
std::mem::swap(&mut self.start, &mut self.end);
7785
}
7886
}
7987

exercises/algorithm/algorithm3.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
sort
3-
This problem requires you to implement a sorting algorithm
4-
you can use bubble sorting, insertion sorting, heap sorting, etc.
2+
sort
3+
This problem requires you to implement a sorting algorithm
4+
you can use bubble sorting, insertion sorting, heap sorting, etc.
55
*/
66
// I AM NOT DONE
77

8-
fn sort<T>(array: &mut [T]){
9-
//TODO
8+
fn sort<T>(array: &mut [T]) {
9+
//TODO
1010
}
1111
#[cfg(test)]
1212
mod tests {
@@ -18,16 +18,16 @@ mod tests {
1818
sort(&mut vec);
1919
assert_eq!(vec, vec![19, 37, 46, 57, 64, 73, 75, 91]);
2020
}
21-
#[test]
21+
#[test]
2222
fn test_sort_2() {
2323
let mut vec = vec![1];
2424
sort(&mut vec);
2525
assert_eq!(vec, vec![1]);
2626
}
27-
#[test]
27+
#[test]
2828
fn test_sort_3() {
2929
let mut vec = vec![99, 88, 77, 66, 55, 44, 33, 22, 11];
3030
sort(&mut vec);
3131
assert_eq!(vec, vec![11, 22, 33, 44, 55, 66, 77, 88, 99]);
3232
}
33-
}
33+
}

0 commit comments

Comments
 (0)