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
use std::{cmp::PartialOrd, fmt::Debug};
#[derive(Debug)]
struct Node<T> {
next: Option<Box<Node<T>>>,
elem: T,
}
/// A singly-linked list implementation with sorting capabilities.
///
/// # Type Parameters
///
/// * `T` - The element type, which must be `Copy`, `Debug`, and `PartialOrd`
///
/// # Examples
///
/// ```
/// use toolbox_rs::single_linked_list::SingleLinkedList;
///
/// let mut list = SingleLinkedList::new();
/// list.push_front(1);
/// list.push_front(2);
/// assert_eq!(list.pop_front(), Some(2));
/// ```
pub struct SingleLinkedList<T> {
head: Option<Box<Node<T>>>,
}
impl<T: Copy + Debug + PartialOrd> SingleLinkedList<T> {
/// Creates a new empty linked list.
///
/// # Examples
/// ```
/// use toolbox_rs::single_linked_list::SingleLinkedList;
/// let list: SingleLinkedList<i32> = SingleLinkedList::new();
/// assert!(list.is_empty());
/// ```
pub fn new() -> Self {
Self { head: None }
}
/// Adds an element to the front of the list.
///
/// # Arguments
/// * `elem` - The element to add
///
/// # Examples
/// ```
/// use toolbox_rs::single_linked_list::SingleLinkedList;
/// let mut list = SingleLinkedList::new();
/// list.push_front(1);
/// assert_eq!(list.peek_front(), Some(&1));
/// ```
pub fn push_front(&mut self, elem: T) {
let new_node = Box::new(Node {
next: self.head.take(),
elem,
});
self.head = Some(new_node);
}
/// Removes and returns the first element of the list.
///
/// # Returns
/// * `Some(T)` - The first element if the list is not empty
/// * `None` - If the list is empty
///
/// # Examples
/// ```
/// use toolbox_rs::single_linked_list::SingleLinkedList;
/// let mut list = SingleLinkedList::new();
/// list.push_front(1);
/// assert_eq!(list.pop_front(), Some(1));
/// assert_eq!(list.pop_front(), None);
/// ```
pub fn pop_front(&mut self) -> Option<T> {
self.head.take().map(|node| {
self.head = node.next;
node.elem
})
}
/// Returns a reference to the first element without removing it.
///
/// # Returns
/// * `Some(&T)` - Reference to the first element if the list is not empty
/// * `None` - If the list is empty
pub fn peek_front(&self) -> Option<&T> {
self.head.as_ref().map(|node| &node.elem)
}
/// Returns a mutable reference to the first element without removing it.
///
/// # Returns
/// * `Some(&mut T)` - Mutable reference to the first element if the list is not empty
/// * `None` - If the list is empty
pub fn peek_front_mut(&mut self) -> Option<&mut T> {
self.head.as_mut().map(|node| &mut node.elem)
}
/// Checks if the list is empty.
///
/// # Returns
/// * `true` - If the list contains no elements
/// * `false` - If the list contains at least one element
pub fn is_empty(&self) -> bool {
self.head.is_none()
}
/// Checks if the list is sorted in ascending order.
///
/// # Returns
/// * `true` - If the list is sorted or has fewer than 2 elements
/// * `false` - If the list is not sorted
pub fn is_sorted(&self) -> bool {
let mut current = &self.head;
while let Some(node) = current {
if let Some(next_node) = &node.next {
if node.elem > next_node.elem {
return false;
}
}
current = &node.next;
}
true
}
/// Inserts an element into the list maintaining sorted order.
///
/// # Arguments
/// * `elem` - The element to insert
///
/// # Note
/// Assumes the list is already sorted. If the list is not sorted,
/// the resulting order is undefined.
pub fn insert_sorted(&mut self, elem: T) {
let mut current = &mut self.head;
while let Some(node) = current {
let next_is_smaller = match &node.next {
Some(next) => next.elem < elem,
None => false,
};
if next_is_smaller {
current = &mut node.next;
} else {
let new_node = Box::new(Node {
next: node.next.take(),
elem,
});
node.next = Some(new_node);
return;
}
}
}
/// Removes all elements from the list.
///
/// # Examples
/// ```
/// use toolbox_rs::single_linked_list::SingleLinkedList;
/// let mut list = SingleLinkedList::new();
/// list.push_front(1);
/// list.clear();
/// assert!(list.is_empty());
/// ```
pub fn clear(&mut self) {
self.head = None;
}
}
impl<T: Copy + Debug + PartialOrd> Default for SingleLinkedList<T> {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod test {
#[test]
fn creation_push_peek_pop() {
let mut list = super::SingleLinkedList::new();
assert!(list.is_empty());
list.push_front(1);
list.push_front(2);
list.push_front(3);
assert!(!list.is_empty());
assert_eq!(list.peek_front(), Some(&3));
assert_eq!(list.peek_front_mut(), Some(&mut 3));
assert_eq!(list.pop_front(), Some(3));
assert_eq!(list.pop_front(), Some(2));
assert_eq!(list.pop_front(), Some(1));
assert_eq!(list.pop_front(), None);
assert!(list.is_empty());
}
#[test]
fn find_not_less() {
let mut list = super::SingleLinkedList::new();
assert!(list.is_empty());
assert!(list.is_sorted());
list.push_front(8);
list.push_front(5);
list.push_front(1);
assert!(list.is_sorted());
list.insert_sorted(3);
list.insert_sorted(2);
assert!(list.is_sorted());
list.insert_sorted(6);
list.insert_sorted(4);
list.insert_sorted(7);
list.insert_sorted(9);
}
#[test]
fn unsorted() {
let mut list = super::SingleLinkedList::default();
assert!(list.is_empty());
assert!(list.is_sorted());
list.push_front(5);
list.push_front(8);
list.push_front(1);
assert!(!list.is_sorted());
}
#[test]
fn clear_list() {
let mut list = super::SingleLinkedList::new();
// Clear empty list
list.clear();
assert!(list.is_empty());
// Add elements and clear
list.push_front(1);
list.push_front(2);
list.push_front(3);
assert!(!list.is_empty());
assert_eq!(list.peek_front(), Some(&3));
list.clear();
assert!(list.is_empty());
assert_eq!(list.peek_front(), None);
// Verify operations work after clearing
list.push_front(4);
assert!(!list.is_empty());
assert_eq!(list.peek_front(), Some(&4));
}
}