Crates.io | array-linked-list |
lib.rs | array-linked-list |
version | 0.2.1 |
created_at | 2021-04-18 19:28:11.72341+00 |
updated_at | 2025-03-29 14:00:18.356607+00 |
description | A data structure, which combines the advantages of dynamic arrays and linked lists |
homepage | |
repository | https://gitlab.com/porky11/array-linked-list |
max_upload_size | |
id | 386278 |
size | 42,696 |
A hybrid data structure combining the benefits of arrays and linked lists, with O(1) indexed access, insertions, and deletions.
rev()
, and partial iteration via iter_after()
/iter_before()
Operation | ArrayLinkedList |
Vec |
LinkedList |
Vec<Option<T>> |
---|---|---|---|---|
Push/Pop Front | O(1) | O(n)* | O(1) | O(1)† |
Push/Pop Back | O(1) | O(1) | O(1) | O(1) |
Remove by Index | O(1) | O(n)* | O(n) | O(1)† |
Cache Locality | ✅ | ✅ | ❌ | ✅ |
Stable Indices | ✅ | ❌ | ❌ | ❌ |
*Amortized | †Requires slot reuse logic
use array_linked_list::ArrayLinkedList;
let mut list = ArrayLinkedList::new();
// Add elements
let idx1 = list.push_front(10); // Index 0
let idx2 = list.push_back(20); // Index 1
let idx3 = list.push_front(30); // Index 2
// Access by index
assert_eq!(list[idx1], Some(10));
assert_eq!(list[idx2], Some(20));
assert_eq!(list[idx3], Some(30));
// Iterate in logical order
let items: Vec<_> = list.iter().copied().collect();
assert_eq!(items, [30, 10, 20]);
// Replace elements while maintaining indices and moving the new element to the logical front
list.replace_front(idx2, 99).unwrap();
assert_eq!(list.iter().copied().collect::<Vec<_>>(), [99, 30, 10]);
// Iterate with indices
for (idx, val) in list.indexed() {
println!("Index {idx}: {val}");
}
// Partial iteration
let after_30: Vec<_> = list.iter_before(idx1).copied().collect();
assert_eq!(after_30, vec![99, 30]);