-
Notifications
You must be signed in to change notification settings - Fork 3
Yann Simple linked list #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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() | ||
|
|
@@ -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 | ||
| } | ||
|
|
||
| pub fn push(&mut self, _element: T) { | ||
| unimplemented!() | ||
| pub fn push(&mut self, element: T) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .as_ref() ? |
||
| None => return None, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -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(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.