basis/list/
stack.rs

1pub struct Stack<T> {
2    head: Link<T>,
3}
4
5type Link<T> = Option<Box<Node<T>>>;
6
7struct Node<T> {
8    elem: T,
9    next: Link<T>,
10}
11
12impl<T> Stack<T> {
13    pub fn new() -> Self {
14        Stack { head: None }
15    }
16
17    pub fn push(&mut self, elem: T) {
18        let new_node = Box::new(Node {
19            elem,
20            next: self.head.take(),
21        });
22
23        self.head = Some(new_node);
24    }
25
26    pub fn pop(&mut self) -> Option<T> {
27        self.head.take().map(|node| {
28            self.head = node.next;
29            node.elem
30        })
31    }
32
33    pub fn peek(&self) -> Option<&T> {
34        self.head.as_ref().map(|node| &node.elem)
35    }
36
37    pub fn peek_mut(&mut self) -> Option<&mut T> {
38        self.head.as_mut().map(|node| &mut node.elem)
39    }
40}
41
42impl<T> Default for Stack<T> {
43    fn default() -> Self {
44        Self::new()
45    }
46}
47
48impl<T> Drop for Stack<T> {
49    fn drop(&mut self) {
50        let mut cur_link = self.head.take();
51        while let Some(mut boxed_node) = cur_link {
52            cur_link = boxed_node.next.take();
53        }
54    }
55}
56
57pub struct IntoIter<T>(Stack<T>);
58
59impl<T> IntoIterator for Stack<T> {
60    type Item = T;
61    type IntoIter = IntoIter<T>;
62    fn into_iter(self) -> Self::IntoIter {
63        IntoIter(self)
64    }
65}
66
67impl<T> Iterator for IntoIter<T> {
68    type Item = T;
69    fn next(&mut self) -> Option<Self::Item> {
70        self.0.pop()
71    }
72}
73
74#[cfg(test)]
75mod test {
76    use super::Stack;
77
78    #[test]
79    fn push_and_pop() {
80        let mut list = Stack::new();
81        assert_eq!(list.pop(), None);
82
83        list.push(1);
84        list.push(2);
85        list.push(3);
86        assert_eq!(list.pop(), Some(3));
87        assert_eq!(list.pop(), Some(2));
88
89        list.push(4);
90        list.push(5);
91        assert_eq!(list.pop(), Some(5));
92        assert_eq!(list.pop(), Some(4));
93        assert_eq!(list.pop(), Some(1));
94        assert_eq!(list.pop(), None);
95    }
96
97    #[test]
98    fn peek() {
99        let mut list = Stack::new();
100        assert_eq!(list.peek(), None);
101        assert_eq!(list.peek_mut(), None);
102
103        list.push(1);
104        list.push(2);
105        list.push(3);
106        assert_eq!(list.peek(), Some(&3));
107        assert_eq!(list.peek_mut(), Some(&mut 3));
108
109        if let Some(value) = list.peek_mut() {
110            *value = 42;
111        }
112        assert_eq!(list.peek(), Some(&42));
113        assert_eq!(list.pop(), Some(42));
114    }
115
116    #[test]
117    fn into_iter() {
118        let mut list = Stack::new();
119        list.push(1);
120        list.push(2);
121        list.push(3);
122
123        let mut iter = list.into_iter();
124        assert_eq!(iter.next(), Some(3));
125        assert_eq!(iter.next(), Some(2));
126        assert_eq!(iter.next(), Some(1));
127        assert_eq!(iter.next(), None);
128    }
129}