Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 54 additions & 15 deletions simple-linked-list/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
use core::marker::PhantomData;
use std::iter::FromIterator;

pub struct SimpleLinkedList<T> {
// Delete this field
// dummy is needed to avoid unused parameter error during compilation
dummy: ::std::marker::PhantomData<T>,
head: Option<Box<Node<T>>>,
len: usize,
}

struct Node<T> {
next: Option<Box<Node<T>>>,
element: T,
}

impl<T> Node<T> {
fn new(next: Option<Box<Node<T>>>, element: T) -> Self {
Self { next, element }
}
}

impl<T> SimpleLinkedList<T> {
pub fn new() -> Self {
unimplemented!()
Self { head: None, len: 0 }
}

// You may be wondering why it's necessary to have is_empty()
Expand All @@ -17,33 +28,57 @@ impl<T> SimpleLinkedList<T> {
// whereas is_empty() is almost always cheap.
// (Also ask yourself whether len() is expensive for SimpleLinkedList)
pub fn is_empty(&self) -> bool {
unimplemented!()
self.head.is_none()
}

pub fn len(&self) -> usize {
unimplemented!()
self.len
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je pense que le fait de mettre la len comme propriété et de la modifier manuellement est débattable, et que ce serait peut-être plus pertinent de la calculer pendant la fonction len, en allant sur chaque item.

}

pub fn push(&mut self, _element: T) {
unimplemented!()
pub fn push(&mut self, element: T) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

En théorie push est censé rajouter des éléments à la suite de la liste si je ne dis pas de bêtise. Là ils sont insérés au début. Ça ne se voit pas parce que le test en question n’ajoute qu’un seul élément

let initial_head = self.head.take();
self.len += 1;
self.head = Some(Box::new(Node::new(initial_head, element)))
}

pub fn pop(&mut self) -> Option<T> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

De même pour pop, je me demande si ça ne supprime pas les éléments en bout de chaine

unimplemented!()
let initial_head = self.head.take();
match initial_head {
Some(mut head) => {
self.head = head.next.take();
self.len -= 1;
return Some(head.element);
}
None => return None,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Les return ne sont pas nécessaires

}
}

pub fn peek(&self) -> Option<&T> {
unimplemented!()
match &self.head {
Some(head) => return Some(&head.element),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.as_ref() ?

None => return None,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Les returns ne sont pas nécessaires

}
}

pub fn rev(self) -> SimpleLinkedList<T> {
unimplemented!()
let mut node = self;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On appelle ça un node, mais en fait c'est la liste complète, non ?

let mut rev_list = SimpleLinkedList::new();

while let Some(element) = node.pop() {
rev_list.push(element);
}

rev_list
}
}

impl<T> FromIterator<T> for SimpleLinkedList<T> {
fn from_iter<I: IntoIterator<Item = T>>(_iter: I) -> Self {
unimplemented!()
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut new_linked_list = SimpleLinkedList::new();
for i in iter {
new_linked_list.push(i)
}
new_linked_list
}
}

Expand All @@ -59,7 +94,11 @@ impl<T> FromIterator<T> for SimpleLinkedList<T> {
// demands more of the student than we expect at this point in the track.

impl<T> From<SimpleLinkedList<T>> for Vec<T> {
fn from(mut _linked_list: SimpleLinkedList<T>) -> Vec<T> {
unimplemented!()
fn from(mut linked_list: SimpleLinkedList<T>) -> Vec<T> {
let mut new_vec = Vec::new();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pour être un chouilla plus efficace tu peux initialiser le vecteur cible avec Vec::with_capacity, étant donné que tu connais déjà la taille du vecteur à créer

while let Some(element) = linked_list.pop() {
new_vec.insert(0, element)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

insert décale tout les éléments suivants d’un cran vers la droite. Ce serait plus efficace d’itérer dans l’ordre de linked_list pour les insérer dans l’ordre également

}
new_vec
}
}
8 changes: 0 additions & 8 deletions simple-linked-list/tests/simple-linked-list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ fn test_new_list_is_empty() {
}

#[test]
#[ignore]
fn test_push_increments_length() {
let mut list: SimpleLinkedList<u32> = SimpleLinkedList::new();
list.push(1);
Expand All @@ -17,7 +16,6 @@ fn test_push_increments_length() {
}

#[test]
#[ignore]
fn test_pop_decrements_length() {
let mut list: SimpleLinkedList<u32> = SimpleLinkedList::new();
list.push(1);
Expand All @@ -29,7 +27,6 @@ fn test_pop_decrements_length() {
}

#[test]
#[ignore]
fn test_is_empty() {
let mut list: SimpleLinkedList<u32> = SimpleLinkedList::new();
assert!(list.is_empty(), "List wasn't empty on creation");
Expand Down Expand Up @@ -61,7 +58,6 @@ fn test_is_empty() {
}

#[test]
#[ignore]
fn test_pop_returns_head_element_and_removes_it() {
let mut list: SimpleLinkedList<u32> = SimpleLinkedList::new();
list.push(1);
Expand All @@ -72,7 +68,6 @@ fn test_pop_returns_head_element_and_removes_it() {
}

#[test]
#[ignore]
fn test_peek_returns_reference_to_head_element_but_does_not_remove_it() {
let mut list: SimpleLinkedList<u32> = SimpleLinkedList::new();
assert_eq!(list.peek(), None, "No element should be contained in list");
Expand All @@ -88,7 +83,6 @@ fn test_peek_returns_reference_to_head_element_but_does_not_remove_it() {
}

#[test]
#[ignore]
fn test_from_slice() {
let mut array = vec!["1", "2", "3", "4"];
let mut list: SimpleLinkedList<_> = array.drain(..).collect();
Expand All @@ -99,7 +93,6 @@ fn test_from_slice() {
}

#[test]
#[ignore]
fn test_reverse() {
let mut list: SimpleLinkedList<u32> = SimpleLinkedList::new();
list.push(1);
Expand All @@ -113,7 +106,6 @@ fn test_reverse() {
}

#[test]
#[ignore]
fn test_into_vector() {
let mut v = Vec::new();
let mut s = SimpleLinkedList::new();
Expand Down