| ... | ... | @@ -127,20 +127,17 @@ pub fn remove(list: *DoublyLinkedList, node: *Node) void { |
| 127 | 127 | } |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | | /// Remove and return the last node in the list. |
| 131 | | /// |
| 132 | | /// Returns: |
| 133 | | /// A pointer to the last node in the list. |
| 134 | | pub fn pop(list: *DoublyLinkedList) ?*Node { |
| 130 | /// Remove and return a pointer to the last node in the list. |
| 131 | pub fn popLast(list: *DoublyLinkedList) ?*Node { |
| 135 | 132 | const last = list.last orelse return null; |
| 136 | 133 | list.remove(last); |
| 137 | 134 | return last; |
| 138 | 135 | } |
| 139 | 136 | |
| 140 | | /// Remove and return the first node in the list. |
| 141 | | /// |
| 142 | | /// Returns: |
| 143 | | /// A pointer to the first node in the list. |
| 137 | /// Deprecated in favor of `popLast` |
| 138 | pub const pop = popLast; |
| 139 | |
| 140 | /// Remove and return a pointer to the first node in the list. |
| 144 | 141 | pub fn popFirst(list: *DoublyLinkedList) ?*Node { |
| 145 | 142 | const first = list.first orelse return null; |
| 146 | 143 | list.remove(first); |
| ... | ... | @@ -200,11 +197,14 @@ test "basics" { |
| 200 | 197 | } |
| 201 | 198 | |
| 202 | 199 | _ = list.popFirst(); // {2, 3, 4, 5} |
| 203 | | _ = list.pop(); // {2, 3, 4} |
| 200 | _ = list.popLast(); // {2, 3, 4} |
| 204 | 201 | list.remove(&three.node); // {2, 4} |
| 205 | 202 | |
| 203 | // peek first and last elements of the list |
| 206 | 204 | try testing.expect(@as(*L, @fieldParentPtr("node", list.first.?)).data == 2); |
| 207 | 205 | try testing.expect(@as(*L, @fieldParentPtr("node", list.last.?)).data == 4); |
| 206 | |
| 207 | // list length |
| 208 | 208 | try testing.expect(list.len() == 2); |
| 209 | 209 | } |
| 210 | 210 | |