-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "too-many-lists" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use std::mem; | ||
|
||
// pub enum List { | ||
// Empty, | ||
// Elem(i32, Box<List>), | ||
// } | ||
|
||
// struct Node { | ||
// elem: i32, | ||
// next: List, | ||
// } | ||
// | ||
// pub enum List { | ||
// Empty, | ||
// More(Box<Node>), | ||
// } | ||
|
||
pub struct List { | ||
head: Link, | ||
} | ||
|
||
struct Node { | ||
elem: i32, | ||
next: Link, | ||
} | ||
|
||
enum Link { | ||
Empty, | ||
More(Box<Node>), | ||
} | ||
|
||
impl List { | ||
pub fn new() -> Self { | ||
List { head: Link::Empty } | ||
} | ||
|
||
pub fn push(&mut self, elem: i32) { | ||
let new_node = Box::new(Node { | ||
elem: elem, | ||
next: mem::replace(&mut self.head, Link::Empty), | ||
}); | ||
|
||
self.head = Link::More(new_node); | ||
} | ||
|
||
pub fn pop(&mut self) -> Option<i32> { | ||
match mem::replace(&mut self.head, Link::Empty) { | ||
Link::Empty => None, | ||
Link::More(node) => { | ||
self.head = node.next; | ||
Some(node.elem) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Drop for List { | ||
fn drop(&mut self) { | ||
let mut cur_link = mem::replace(&mut self.head, Link::Empty); | ||
while let Link::More(mut boxed_node) = cur_link { | ||
cur_link = mem::replace(&mut boxed_node.next, Link::Empty); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::List; | ||
|
||
#[test] | ||
fn basics() { | ||
let mut list = List::new(); | ||
|
||
assert_eq!(list.pop(), None); | ||
|
||
list.push(1); | ||
list.push(2); | ||
list.push(3); | ||
|
||
assert_eq!(list.pop(), Some(3)); | ||
assert_eq!(list.pop(), Some(2)); | ||
|
||
list.push(4); | ||
list.push(5); | ||
|
||
assert_eq!(list.pop(), Some(5)); | ||
assert_eq!(list.pop(), Some(4)); | ||
|
||
assert_eq!(list.pop(), Some(1)); | ||
assert_eq!(list.pop(), None); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod first; |