This commit is contained in:
Sander Hautvast 2022-02-14 12:52:53 +01:00
parent b892e70bb5
commit 71587e810c

View file

@ -9,28 +9,28 @@ fn main() {
}
#[derive(Debug)]
pub struct List {
head: Link,
pub struct List<T> {
head: Link<T>,
}
#[derive(Debug)]
enum Link {
enum Link<T> {
Empty,
More(Box<Node>),
More(Box<Node<T>>),
}
#[derive(Debug)]
struct Node {
elem: i32,
next: Link,
struct Node<T> {
elem: T,
next: Link<T>,
}
impl List {
impl <T> List<T> {
pub fn new() -> Self {
Self { head: Link::Empty }
}
pub fn push(&mut self, elem: i32) {
pub fn push(&mut self, elem: T) {
let new_node = Node {
elem: elem,
next: std::mem::replace(&mut self.head, Link::Empty),
@ -38,7 +38,7 @@ impl List {
self.head = Link::More(Box::new(new_node));
}
pub fn pop(&mut self) -> Option<i32> {
pub fn pop(&mut self) -> Option<T> {
match std::mem::replace(&mut self.head, Link::Empty) {
Link::Empty => None,
Link::More(node) => {