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
//! # Linked List
//! 
//! Implements a simple linked list using Heap memory.
//! Currently, there's an implementation only for the i32 primitive type.
pub struct LinkedList<T> {
    pub val: Option<T>,
    pub next: Option<Box<LinkedList<T>>>,
}
pub struct PopLeftResult<T> {
    pub val: Option<T>,
    pub list: Option<Box<LinkedList<T>>>
}

impl LinkedList<i32> {
    /// # Creates an empty LinkedList that may hold i32 values
    /// 
    /// # Example
    /// ```
    /// let list = mini_linked_list::LinkedList::<i32>::new();
    /// ```
    pub fn new() -> LinkedList<i32>{
        LinkedList {
            val: None,
            next: None,
        }
    }
    /// # Adds an element to the right side of the list
    /// 
    /// This method uses O(N) operations, as it needs to traverse
    /// the entire list before appending the new value to the end of the list.
    /// 
    /// # Example
    /// ```
    /// let mut list = mini_linked_list::LinkedList::<i32>::new();
    /// list.push_right(1);
    /// list.push_right(2);
    /// list.push_right(3);
    /// list.push_right(4);
    /// assert_eq!(list.collect(), vec![1,2,3,4]);
    /// ```
    pub fn push_right(&mut self, x: i32) {
        let mut node = self;
        while node.next.is_some() {
            node = node.next.as_mut().unwrap();
        }
        node.next = Some(Box::new(LinkedList{
            val: Some(x),
            next: None,
        }))
    }
    /// # Adds an element to the left side of the list
    /// 
    /// This method works in O(1) operation, as it replaces the head of the list
    /// with a new one and no traversal thus is required.
    /// 
    /// The method returns the new memory address of the list that must be handled
    /// by the caller (typically reassigning the variable).
    /// 
    /// # Example
    /// ```
    /// use mini_linked_list::LinkedList;
    /// let mut list: LinkedList<i32> = LinkedList::<i32>::new();
    /// list = list.push_left(1);
    /// list = list.push_left(2);
    /// list = list.push_left(3);
    /// list = list.push_left(4);
    /// assert_eq!(list.collect(), vec![4,3,2,1]);
    /// ```
    pub fn push_left(self, x: i32) -> LinkedList<i32>{
        let node= LinkedList::<i32> {
            val: Some(x),
            next: Some(Box::new(self))
        };
        node
    }

    /// # Pops the List head on the left side, returning a PopLeftResult
    /// 
    /// This operation works in O(1), as it only pops the head and
    /// no traversal is required.
    /// 
    /// It's usage is not so straightforward, however, as it requires
    /// the caller to replace the reference to the list head with the
    /// address returned by this method, inside `PopLeftResult.list`
    /// 
    /// It's advised to not call `unwrap` directly in `PopLeftResult.list``
    /// directly, but rather rely on safer `Option` methods.
    /// 
    /// # Example
    /// 
    /// ```
    /// use mini_linked_list::{LinkedList, PopLeftResult};
    /// let mut list: LinkedList<i32> = LinkedList::<i32>::new();
    /// list.push_right(1);
    /// list.push_right(2);
    /// 
    /// let result: PopLeftResult<i32> = list.pop_left();
    /// let list = result.list.unwrap();
    /// 
    /// assert_eq!(list.collect(), vec![2]);
    /// assert_eq!(result.val.unwrap(), 1);
    /// 
    /// let result: PopLeftResult<i32> = list.pop_left();
    /// let list = result.list;
    /// 
    /// assert_eq!(list.is_none(), true);
    /// assert_eq!(result.val.unwrap(), 2);
    /// ```

    pub fn pop_left(self) -> PopLeftResult<i32> {
        if self.val.is_some() {
            return PopLeftResult { val: self.val, list: self.next }
        }
        match self.next {
            Some(node) => PopLeftResult { val: node.val, list: node.next },
            None => PopLeftResult { val: self.val, list: None }
        }
    }

    /// # Pops the List head on the right side.
    /// 
    /// This operation works in O(N), as it requires a full traversal
    /// of the list.
    /// 
    /// Whenever possible, prefer relying on the `pop_left` method, as it is more
    /// efficient.
    /// 
    /// # Example
    /// 
    /// ```
    /// use mini_linked_list::LinkedList;
    /// let mut list: LinkedList<i32> = LinkedList::<i32>::new();
    /// list.push_right(1);
    /// list.push_right(2);
    /// assert_eq!(list.pop_right().unwrap(), 2);
    /// assert_eq!(list.pop_right().unwrap(), 1);
    /// assert_eq!(list.pop_right().is_none(), true);
    /// ```
    pub fn pop_right(&mut self) -> Option<i32>{
        let mut node: &mut Option<Box<LinkedList<i32>>> = &mut self.next;
        let x: Option<i32>;
        // no next node, need to try to pop self
        if node.is_none() {
            let val = self.val;
            self.val = None;
            return val
        }
        // only one next node, we should return it
        if node.as_mut().unwrap().next.is_none() {
            x = node.as_mut().unwrap().val;
            node.as_mut().unwrap().next = None;
            node.as_mut().unwrap().val = None;
            return x;
        }
        // we can assume we have at least two valid next aheads
        else {
            while node.as_mut().unwrap().next.as_mut().unwrap().next.is_some() {
                node = &mut node.as_mut().unwrap().next;
            }
            x = node.as_mut().unwrap().next.as_mut().unwrap().val;
            node.as_mut().unwrap().next = None;
        }
        return x
    }

    /// # Collects the list into an Array
    /// 
    /// This method is used mostly for debugging and testing,
    /// but can also be used to iterate over the list without popping
    /// it's values.
    /// 
    /// It's not memory efficient, however, as it copies the entire
    /// data.
    /// 
    /// # Example
    /// ```
    /// use mini_linked_list::LinkedList;
    /// let mut list: LinkedList<i32> = LinkedList::<i32>::new();
    /// list.push_right(1);
    /// list.push_right(2);
    /// assert_eq!(list.collect(), vec![1,2]);
    /// ```
    pub fn collect(&self) -> Vec<i32> {
        let mut result = Vec::<i32>::new();
        let mut node = self;

        match node.val {
            Some(val) => result.push(val),
            _ => ()
        }
        while node.next.is_some() {
            node = node.next.as_ref().unwrap();
            match node.val {
                Some(val) => result.push(val),
                _ => ()
            }
        }

        return result;

    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_linked_list_push_right() {
        let mut list: LinkedList<i32> = LinkedList::<i32>::new();
        list.push_right(1);
        list.push_right(2);
        list.push_right(3);
        list.push_right(4);
        assert_eq!(list.collect(), vec![1,2,3,4]);
    }
    #[test]
    fn test_linked_list_push_left() {
        let mut list: LinkedList<i32> = LinkedList::<i32>::new();
        list = list.push_left(1);
        list = list.push_left(2);
        list = list.push_left(3);
        list = list.push_left(4);
        assert_eq!(list.collect(), vec![4,3,2,1]);
    }
    
    #[test]
    fn test_linked_list_pop_left() {
        let mut list: LinkedList<i32> = LinkedList::<i32>::new();
        list.push_right(1);
        list.push_right(2);
        list.push_right(3);
        list.push_right(4);

        let result = list.pop_left();
        let list = result.list.unwrap();

        assert_eq!(list.collect(), vec![2,3,4]);
        assert_eq!(result.val.unwrap(), 1);

        let result = list.pop_left();
        let list = result.list.unwrap();

        assert_eq!(list.collect(), vec![3,4]);
        assert_eq!(result.val.unwrap(), 2);

        let result = list.pop_left();
        let list = result.list.unwrap();

        assert_eq!(list.collect(), vec![4]);
        assert_eq!(result.val.unwrap(), 3);

        let result = list.pop_left();
        assert_eq!(result.list.is_none(), true);
        assert_eq!(result.val.unwrap(), 4);

    }

    #[test]
    fn test_linked_list_pop_right() {
        let mut list: LinkedList<i32> = LinkedList::<i32>::new();
        list.push_right(1);
        list.push_right(2);
        list.push_right(3);
        list.push_right(4);
 
        assert_eq!(list.pop_right().unwrap(), 4);
        assert_eq!(list.pop_right().unwrap(), 3);
        assert_eq!(list.pop_right().unwrap(), 2);
        assert_eq!(list.pop_right().unwrap(), 1);
        assert_eq!(list.pop_right().is_none(), true);
    }

    #[test]
    fn test_several_apis() {
        let mut list: LinkedList<i32> = LinkedList::<i32>::new();
        list.push_right(1);
        list.push_right(2);
        list.push_right(3);
        list.push_right(4);
        assert_eq!(list.collect(), vec![1,2,3,4]);

        list = list.push_left(1);
        list = list.push_left(2);
        list = list.push_left(3);
        list = list.push_left(4);
        assert_eq!(list.collect(), vec![4,3,2,1,1,2,3,4]);
        let x = list.pop_right();
        assert_eq!(x.unwrap(), 4);
        assert_eq!(list.collect(), vec![4,3,2,1,1,2,3]);

        let result = list.pop_left();
        let list = result.list.unwrap();
        assert_eq!(list.collect(), vec![3,2,1,1,2,3]);
        assert_eq!(result.val.unwrap(), 4);

    }
}