From e1ccc6ee8935daae9e1220ce66a184c98dc86646 Mon Sep 17 00:00:00 2001 From: davidrobert99 Date: Fri, 8 Apr 2022 11:51:10 +0100 Subject: [PATCH] generics list exercise --- subjects/generics_list/README.md | 78 ++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 subjects/generics_list/README.md diff --git a/subjects/generics_list/README.md b/subjects/generics_list/README.md new file mode 100644 index 000000000..0888b8d15 --- /dev/null +++ b/subjects/generics_list/README.md @@ -0,0 +1,78 @@ +## generics_list + +### Instructions + +Create a linked list of generic values with the following methods. + +- `new` which returns a new empty list. + +- `push` which adds at the begining of the list a new element. + +- `pop` which deletes the last added element to the list. + +- `len` which returns size of the list. + + + +### Expected Functions + +```rust +#[derive(Clone, Debug)] +pub struct List { + pub head: Option>, +} + +#[derive(Clone, Debug)] +pub struct Node { + pub value: T, + pub next: Option>>, +} + +impl List { + pub fn new() -> List { + } + + pub fn push(&mut self, value: T) { + } + + pub fn pop(&mut self) { + } + + pub fn len(&self) -> usize { + } +} +``` + +### Usage + +Here is a program to test your function. + +```rust +use generics_list::*; + +fn main() { + let mut new_list_str = List::new(); + new_list_str.push("String Test 1"); + println!("The size of the list is {}", new_list_str.len()); + + new_list_str.push("String Test 2"); + println!("The size of the list is {}", new_list_str.len()); + + new_list_str.push("String Test 3"); + println!("The size of the list is {}", new_list_str.len()); + + new_list_str.pop(); + println!("The size of the list is {}", new_list_str.len()); +} +``` + +And its output + +```console +$ cargo run +The size of the list is 1 +The size of the list is 2 +The size of the list is 3 +The size of the list is 2 +$ +```