34 lines
511 B
Rust
34 lines
511 B
Rust
fn main() {}
|
|
|
|
#[derive(Debug)]
|
|
pub struct List {
|
|
head: Link,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
enum Link {
|
|
Empty,
|
|
More(Box<Node>),
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
struct Node {
|
|
elem: i32,
|
|
next: Link,
|
|
}
|
|
|
|
impl List {
|
|
pub fn new() -> Self {
|
|
Self { head: Link::Empty }
|
|
}
|
|
|
|
pub fn push(&mut self, elem: i32) {
|
|
let new_node = Node {
|
|
elem: elem,
|
|
next: std::mem::replace(&mut self.head, Link::Empty),
|
|
};
|
|
self.head = Link::More(Box::new(new_node));
|
|
}
|
|
}
|
|
|
|
|