|
| 1 | +# Introduction |
| 2 | + |
| 3 | +There is more than one way to solve Simple Linked List. |
| 4 | +One general approach is to keep track of the length as nodes are pushed and popped. |
| 5 | +Another approach is to calculate the length every time it is asked for. |
| 6 | + |
| 7 | +## General guidance |
| 8 | + |
| 9 | +One thing to keep in mind is to not mutate the list when it is not necessary. |
| 10 | +For instance, if you find yourself using `mut self` for `rev()` or `into()`, that is an indication that the list is being mutated when it is not necessary. |
| 11 | + |
| 12 | +A well-known treatment of writing linked lists in Rust is [`Learn Rust With Entirely Too Many Linked Lists`][too-many-lists]. |
| 13 | + |
| 14 | +## Approach: Keep track of length |
| 15 | + |
| 16 | +```rust |
| 17 | +use std::iter::FromIterator; |
| 18 | + |
| 19 | +type Link<T> = Option<Box<Node<T>>>; |
| 20 | + |
| 21 | +pub struct SimpleLinkedList<T> { |
| 22 | + head: Link<T>, |
| 23 | + len: usize, |
| 24 | +} |
| 25 | +struct Node<T> { |
| 26 | + data: T, |
| 27 | + next: Link<T>, |
| 28 | +} |
| 29 | + |
| 30 | +impl<T> SimpleLinkedList<T> { |
| 31 | + pub fn new() -> Self { |
| 32 | + Self { head: None, len: 0 } |
| 33 | + } |
| 34 | + |
| 35 | + pub fn is_empty(&self) -> bool { |
| 36 | + self.len == 0 |
| 37 | + } |
| 38 | + |
| 39 | + pub fn len(&self) -> usize { |
| 40 | + self.len |
| 41 | + } |
| 42 | + |
| 43 | + pub fn push(&mut self, _element: T) { |
| 44 | + let new_node = Box::new(Node { |
| 45 | + data: _element, |
| 46 | + next: self.head.take(), |
| 47 | + }); |
| 48 | + |
| 49 | + self.head = Some(new_node); |
| 50 | + self.len += 1; |
| 51 | + } |
| 52 | + |
| 53 | + pub fn pop(&mut self) -> Option<T> { |
| 54 | + if self.len == 0 { |
| 55 | + return None; |
| 56 | + } |
| 57 | + self.len -= 1; |
| 58 | + self.head.take().map(|node| { |
| 59 | + self.head = node.next; |
| 60 | + node.data |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + pub fn peek(&self) -> Option<&T> { |
| 65 | + self.head.as_ref().map(|node| &node.data) |
| 66 | + } |
| 67 | + |
| 68 | + pub fn rev(self) -> SimpleLinkedList<T> { |
| 69 | + let mut list = Self::new(); |
| 70 | + if self.len == 0 { |
| 71 | + return list; |
| 72 | + } |
| 73 | + let mut cur_node = self.head; |
| 74 | + while let Some(node) = cur_node { |
| 75 | + list.push(node.data); |
| 76 | + cur_node = node.next; |
| 77 | + } |
| 78 | + list |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl<T> FromIterator<T> for SimpleLinkedList<T> { |
| 83 | + fn from_iter<I: IntoIterator<Item = T>>(_iter: I) -> Self { |
| 84 | + let mut list = Self::new(); |
| 85 | + for val in _iter { |
| 86 | + list.push(val); |
| 87 | + } |
| 88 | + list |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl<T> Into<Vec<T>> for SimpleLinkedList<T> { |
| 93 | + fn into(self) -> Vec<T> { |
| 94 | + let mut the_vec: Vec<T> = vec![]; |
| 95 | + if self.len == 0 { |
| 96 | + return the_vec; |
| 97 | + } |
| 98 | + let mut cur_node = self.rev().head; |
| 99 | + while let Some(node) = cur_node { |
| 100 | + the_vec.push(node.data); |
| 101 | + cur_node = node.next; |
| 102 | + } |
| 103 | + the_vec |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +For more information, check the [keep track of length appproach][approach-keep-track-of-length]. |
| 109 | + |
| 110 | +## Approach: Do not keep track of length |
| 111 | + |
| 112 | +```rust |
| 113 | +use std::iter::FromIterator; |
| 114 | + |
| 115 | +type Link<T> = Option<Box<Node<T>>>; |
| 116 | + |
| 117 | +pub struct SimpleLinkedList<T> { |
| 118 | + head: Link<T>, |
| 119 | +} |
| 120 | +struct Node<T> { |
| 121 | + data: T, |
| 122 | + next: Link<T>, |
| 123 | +} |
| 124 | + |
| 125 | +impl<T> Node<T> { |
| 126 | + fn new(data: T, next: Option<Box<Node<T>>>) -> Self { |
| 127 | + Self { data, next } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +impl<T> SimpleLinkedList<T> { |
| 132 | + pub fn new() -> Self { |
| 133 | + Self { head: None } |
| 134 | + } |
| 135 | + |
| 136 | + pub fn is_empty(&self) -> bool { |
| 137 | + self.head.is_none() |
| 138 | + } |
| 139 | + |
| 140 | + pub fn len(&self) -> usize { |
| 141 | + let mut current_node = &self.head; |
| 142 | + let mut size = 0; |
| 143 | + while let Some(x) = current_node { |
| 144 | + size += 1; |
| 145 | + current_node = &x.next; |
| 146 | + } |
| 147 | + size |
| 148 | + } |
| 149 | + |
| 150 | + pub fn push(&mut self, element: T) { |
| 151 | + let node = Box::new(Node::new(element, self.head.take())); |
| 152 | + self.head = Some(node); |
| 153 | + } |
| 154 | + |
| 155 | + pub fn pop(&mut self) -> Option<T> { |
| 156 | + if self.head.is_some() { |
| 157 | + let head_node = self.head.take().unwrap(); |
| 158 | + self.head = head_node.next; |
| 159 | + Some(head_node.data) |
| 160 | + } else { |
| 161 | + None |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + pub fn peek(&self) -> Option<&T> { |
| 166 | + self.head.as_ref().map(|head| &(head.data)) |
| 167 | + } |
| 168 | + |
| 169 | + pub fn rev(self) -> SimpleLinkedList<T> { |
| 170 | + let mut list = SimpleLinkedList::new(); |
| 171 | + let mut cur_node = self.head; |
| 172 | + while let Some(node) = cur_node { |
| 173 | + list.push(node.data); |
| 174 | + cur_node = node.next; |
| 175 | + } |
| 176 | + list |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +impl<T> FromIterator<T> for SimpleLinkedList<T> { |
| 181 | + fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self { |
| 182 | + let mut list = SimpleLinkedList::new(); |
| 183 | + for item in iter { |
| 184 | + list.push(item); |
| 185 | + } |
| 186 | + list |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +impl<T> Into<Vec<T>> for SimpleLinkedList<T> { |
| 191 | + fn into(self) -> Vec<T> { |
| 192 | + let mut the_vec: Vec<T> = vec![]; |
| 193 | + let mut cur_node = self.rev().head; |
| 194 | + while let Some(node) = cur_node { |
| 195 | + the_vec.push(node.data); |
| 196 | + cur_node = node.next; |
| 197 | + } |
| 198 | + the_vec |
| 199 | + } |
| 200 | +} |
| 201 | +``` |
| 202 | + |
| 203 | +For more information, check the [do not keep track of length appproach][approach-do-not-keep-track-of-length]. |
| 204 | + |
| 205 | +## Which approach to use? |
| 206 | + |
| 207 | +Since benchmarking is currently outside the scope of this document, which to use is pretty much a matter of personal preference. |
| 208 | +To keep track of the length as you go may seem wasteful if the length is never requested. |
| 209 | +On the other hand, if the the length is requested more than once on an unchanged list, it may seem wasteful to calculate the same length |
| 210 | +multiple times. |
| 211 | + |
| 212 | +[too-many-lists]: https://rust-unofficial.github.io/too-many-lists/ |
| 213 | +[approach-keep-track-of-length]: https://exercism.org/tracks/rust/exercises/simple-linked-list/approaches/keep-track-of-length |
| 214 | +[approach-do-not-keep-track-of-length]: https://exercism.org/tracks/rust/exercises/simple-linked-list/approaches/do-not-keep-track-of-length |
0 commit comments