Fix list::remove when trying to remove first element of the list (SIGSEGV)
This commit is contained in:
@ -113,8 +113,12 @@ impl<T: PartialEq> List<T> {
|
||||
let mut current: *mut Node<T> = self.head;
|
||||
let mut previous: *mut Node<T> = ptr::null_mut();
|
||||
while !current.is_null() {
|
||||
if (*current).elem == item {
|
||||
(*previous).next = (*current).next;
|
||||
if (&*current).elem == item {
|
||||
if !previous.is_null() {
|
||||
(*previous).next = (*current).next;
|
||||
} else {
|
||||
self.head = (*current).next;
|
||||
}
|
||||
drop(Box::from_raw(current).elem);
|
||||
return true;
|
||||
} else {
|
||||
@ -320,6 +324,22 @@ mod test {
|
||||
assert_eq!(list.peek(), Option::None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_test2() {
|
||||
let mut list = List::default();
|
||||
assert_eq!(list.peek(), None);
|
||||
list.push(1);
|
||||
list.push(2);
|
||||
list.push(3);
|
||||
|
||||
assert_eq!(list.contains(&1), true);
|
||||
list.remove(1);
|
||||
assert_eq!(list.contains(&1), false);
|
||||
assert_eq!(list.pop(), Option::Some(2));
|
||||
assert_eq!(list.pop(), Option::Some(3));
|
||||
assert_eq!(list.peek(), Option::None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn miri_test() {
|
||||
let mut list = List::default();
|
||||
|
Reference in New Issue
Block a user