1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/// module containing nodes
pub mod nodes {
    #[derive(Debug, Clone)]
    /// structure for a specific node inside of a stack
    pub struct Node<T>
    where
        T: Clone,
    {
        next: Box<Option<Node<T>>>,
        prev: Box<Option<Node<T>>>,
        val: T,
    }

    impl<T: std::clone::Clone> Node<T> {
        /// create a new node
        ///
        /// # Arguments
        ///
        /// * `next`: the next node
        /// * `prev`: the previews node
        /// * `val`: the value of the node
        ///
        /// # Returns
        ///
        /// this function returns a new Node built with the specified parameters
        pub fn new(next: Option<Node<T>>, prev: Option<Node<T>>, val: T) -> Node<T> {
            Node {
                next: Box::new(next),
                prev: Box::new(prev),
                val,
            }
        }

        /// get the previous Node
        ///
        /// # Returns
        ///
        /// the previous Node
        pub fn get_prev(&self) -> Option<Node<T>> {
            *self.prev.clone()
        }

        /// set the previous Node
        ///
        /// # Arguments
        ///
        /// * `new_prev`: the new previous Node
        pub fn set_prev(&mut self, new_prev: Option<Node<T>>) {
            self.prev = Box::new(new_prev);
        }

        /// get the next Node
        ///
        /// # Returns
        ///
        /// the next Node
        pub fn get_next(&self) -> Option<Node<T>> {
            *self.next.clone()
        }

        /// set the next Node
        ///
        /// # Arguments
        ///
        /// * `new_next`: the new next Node
        pub fn set_next(&mut self, new_next: Option<Node<T>>) {
            self.next = Box::new(new_next);
        }

        /// get the current value
        ///
        /// # Returns
        ///
        /// the current value
        pub fn get_val(&self) -> T {
            self.val.clone()
        }

        /// set the value
        ///
        /// # Arguments
        ///
        /// * `new_val`: the new value
        pub fn set_val(&mut self, new_val: T) {
            self.val = new_val;
        }
    }
}

/// module containing stacks
pub mod stacks {
    use crate::stack::nodes::Node;

    #[derive(Debug, Clone)]
    /// structure representing a stack
    ///
    /// A stack is handled as a collection of nodes.
    /// we specify the top node who himself specifies the previous node and so on.
    pub struct Stack<T: std::clone::Clone> {
        top: Option<Node<T>>,
        len: usize,
    }

    impl<T: std::clone::Clone> Stack<T> {
        /// create a new stack
        ///
        /// # Arguments
        ///
        /// * `val`: the value to use
        ///
        /// # Returns
        ///
        /// this function returns a new stack with one node that has the value of `val`
        pub fn new() -> Stack<T> {
            Stack { top: None, len: 0 }
        }

        /// push a new element to the stack
        ///
        /// # Arguments
        ///
        /// `val`: the value for the new element to have
        ///
        /// # Examples
        ///
        /// ```rust
        /// use stacking::stacks::Stack;
        ///
        /// let mut stack: Stack<i32> = Stack::new();
        /// stack.push(4);
        /// assert_eq!(stack.pop(), Some(4));
        /// ```
        pub fn push(&mut self, val: T) {
            let new_top: Node<T> = Node::new(None, self.top.clone(), val);
            self.top = Some(new_top);
            self.len += 1;
        }

        /// pop the top element of the stack.
        ///
        /// # Returns
        ///
        /// an `Option<T>` for the value that the element had
        ///
        /// # Examples
        ///
        /// ```rust
        /// use stacking::stacks::Stack;
        ///
        /// let mut stack: Stack<i32> = Stack::new();
        /// stack.push(4);
        /// stack.push(5);
        /// assert_eq!(stack.pop(), Some(5));
        /// assert_eq!(stack.pop(), Some(4));
        /// ```
        pub fn pop(&mut self) -> Option<T> {
            if self.len == 0 {
                return None;
            }
            match self.top.clone() {
                Some(top) => {
                    let retval = top.get_val().clone();
                    let new_top = match top.clone().get_prev() {
                        Some(mut n) => {
                            n.set_next(None);
                            Some(n)
                        }
                        None => None,
                    };
                    self.top = new_top;
                    self.len -= 1;
                    Some(retval)
                }
                None => None,
            }
        }

        /// return the length of the stack
        ///
        /// # Returns
        ///
        /// the length of the stack as a `usize`
        ///
        /// # Examples
        ///
        /// ```rust
        /// use stacking::stacks::Stack;
        ///
        /// let mut stack: Stack<i32> = Stack::new();
        /// stack.push(5);
        /// assert_eq!(stack.len(), 1);
        /// stack.pop();
        /// assert_eq!(stack.len(), 0);
        /// ```
        pub fn len(&self) -> usize {
            self.len
        }
    }

    #[derive(Debug, Clone)]
    /// a struct representing a queue which is a specific type of stack.
    /// You push to the top and pop from the bottom.
    pub struct Queue<T: Clone> {
        top: Option<Node<T>>,
        bottom: Option<Node<T>>,
        len: usize,
    }

    impl<T: Clone> Queue<T> {
        /// create a new instance of a Queue
        /// # Returns
        /// a new empty instance of a Queue
        pub fn new() -> Queue<T> {
            Queue {
                top: None,
                bottom: None,
                len: 0,
            }
        }

        /// push a new element to the Queue
        ///
        /// # Arguments
        /// 
        /// * `val`: the value to push
        ///
        /// # Examples
        ///
        /// ```rust
        /// use stacking::stacks::Queue;
        ///
        /// let mut queue: Queue<i32> = Queue::new();
        /// queue.push(4);
        /// queue.push(5);
        /// assert_eq!(queue.pop(), Some(4));
        /// assert_eq!(queue.pop(), Some(5));
        /// ```
        pub fn push(&mut self, val: T) {
            let new_top: Node<T> = Node::new(None, self.top.clone(), val);
            self.top = Some(new_top.clone());
            if self.len == 0 {
                self.bottom = Some(new_top.clone());
            } else if self.len == 1 {
                self.bottom = Some(Node::new(self.top.clone(), None, self.bottom.clone().unwrap().get_val()))
            }
            self.len += 1;
        }

        /// pop the element at the bottom and return it's value
        ///
        /// # Returns
        /// the value of the element at the bottom
        ///
        /// # Examples
        ///
        /// ```rust
        /// use stacking::stacks::Queue;
        ///
        /// let mut queue: Queue<i32> = Queue::new();
        /// queue.push(4);
        /// queue.push(5);
        /// assert_eq!(queue.pop(), Some(4));
        /// assert_eq!(queue.pop(), Some(5));
        /// ```
        pub fn pop(&mut self) -> Option<T> {
            if self.len == 0 {
                return None;
            }
            match self.bottom.clone() {
                Some(bottom) => {
                    let val = bottom.get_val().clone();
                    let new_bottom = match bottom.clone().get_next() {
                        Some(mut n) => {
                            n.set_prev(None);
                            Some(n)
                        }
                        None => None,
                    };
                    self.bottom = new_bottom;
                    self.len -= 1;
                    Some(val)
                }
                None => None,
            }
        }

        /// get the length of the current Queue
        ///
        /// # Returns
        /// the length of the current Queue as a `usize`
        ///
        /// # Examples
        ///
        /// ```rust
        /// use stacking::stacks::Queue;
        ///
        /// let mut queue: Queue<i32> = Queue::new();
        /// assert_eq!(queue.len(), 0);
        /// queue.push(4);
        /// assert_eq!(queue.len(), 1);
        /// ```
        pub fn len(&self) -> usize {
            self.len
        }
    }   
}