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
use super::DoublyLinkedList;
impl<T> DoublyLinkedList<T> {
/// Retrieves a reference to the element at the specified position in the `DoublyLinkedList`.
///
/// # Parameters
///
/// - `pos`: The zero-based index of the element to retrieve.
///
/// # Returns
///
/// - `Some(&T)`: A reference to the element at the specified position, if it exists.
/// - `None`: If the position is out of bounds or the list is empty.
///
/// # Panics
///
/// This method will panic if `pos > self.len()`. Ensure that the provided position is within bounds.
///
/// # Examples
///
/// ```
/// use villa01_data_structures::doubly_linked_list::DoublyLinkedList;
///
/// let mut list = DoublyLinkedList::new();
/// list.insert_at_end(10);
/// list.insert_at_end(20);
/// list.insert_at_end(30);
///
/// assert_eq!(list.get(0), Some(&10)); // Get the first element
/// assert_eq!(list.get(1), Some(&20)); // Get the second element
/// assert_eq!(list.get(2), Some(&30)); // Get the last element
/// ```
pub fn get(&self, pos: usize) -> Option<&T> {
assert!(pos < self.len());
// Get first and last are more efficient
if pos == 0 {
return self.get_first();
}
if pos == self.len() - 1 {
return self.get_last();
}
if pos < (self.len() / 2) {
return self.get_from_start(pos);
} else {
return self.get_from_end(pos);
}
}
fn get_from_start(&self, pos: usize) -> Option<&T> {
let mut cursor = self.head;
let mut counter = 0;
unsafe {
while let Some(node) = cursor {
if counter == pos {
return Some(&(*node).data);
}
cursor = (*node).next;
counter += 1;
}
}
return None;
}
fn get_from_end(&self, pos: usize) -> Option<&T> {
let mut cursor = self.tail;
let mut counter = self.len() - 1;
unsafe {
while let Some(node) = cursor {
if counter == pos {
return Some(&(*node).data);
}
cursor = (*node).prev;
counter -= 1;
}
}
return None;
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_at_zero() {
let mut list: DoublyLinkedList<i32> = DoublyLinkedList::new();
let value = 69;
let _ = list.insert_at_beginning(value);
let first = list.get(0).unwrap();
assert_eq!(&value, first);
}
#[test]
fn test_at_len() {
let mut list: DoublyLinkedList<i32> = DoublyLinkedList::new();
let value = 69;
list.insert_at_end(10);
list.insert_at_end(value);
let first = list.get(1).unwrap();
assert_eq!(&value, first);
}
#[test]
#[should_panic]
fn test_at_non_zero_out_of_bounds() {
let list: DoublyLinkedList<i32> = DoublyLinkedList::new();
let _ = list.get(20).unwrap();
}
#[test]
fn test_with_several_nodes_from_start() {
let mut list: DoublyLinkedList<usize> = DoublyLinkedList::new();
let list_length: usize = 10;
for i in 1..list_length + 1 {
list.insert_at_position(i, 0).unwrap();
}
let pos = list.len() / 2 - 1;
let value = 69;
list.insert_at_position(value, pos).unwrap();
let get_value = list.get(pos).unwrap();
assert_eq!(&value, get_value);
}
#[test]
fn test_with_several_nodes_from_end() {
let mut list: DoublyLinkedList<usize> = DoublyLinkedList::new();
let list_length: usize = 10;
for i in 1..list_length + 1 {
list.insert_at_position(i, 0).unwrap();
}
let pos = list.len() / 2;
let value = 69;
list.insert_at_position(value, pos).unwrap();
let get_value = list.get(pos).unwrap();
assert_eq!(&value, get_value);
}
}