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