This repository contains two C++ data-structure implementations with a focus on pointer ownership, time complexity, and memory safety.
Given a (sorted) singly-linked list, recursively split it into two sorted lists:
odds: nodes with odd valuesevens: nodes with even values
Constraints
- Must be recursive (no
for/while) - Reuse existing nodes (no extra node allocations required)
- Set input pointer
intonullptrafter splitting (ownership transferred)
Complexity
- Time:
O(n) - Extra space:
O(n)recursion stack
Implements an Unrolled Linked List where each node stores a fixed-size array of strings (ARRSIZE = 10).
Supports:
push_front,push_backin O(1)pop_front,pop_backin O(1)get,setin O(n) (walk nodes + offset)
Why Unrolled Linked List? Compared to a standard linked list, it improves cache locality and reduces pointer overhead by storing multiple elements per node.
split.h / split.cpp— recursive split implementationulliststr.h / ulliststr.cpp— unrolled linked list implementationtest_split.cpp— simple manual test (prints results)test_ulliststr.cpp— simple manual test (prints results)grade_split.cpp,grade_ulliststr.cpp— GoogleTest suites (provided)grade_split.sh,grade_ulliststr.sh— compile + run tests (provided)
Requires
g++,gtest, and optionallyvalgrind.
# Split tests
bash grade_split.sh
# ULListStr tests
bash grade_ulliststr.shg++ split.cpp test_split.cpp -o demo_split
./demo_split
g++ ulliststr.cpp test_ulliststr.cpp -o demo_ullist
./demo_ullist- ULListStr owns its nodes and releases them in clear() and destructor.
- The split function transfers node ownership from in into odds/evens.
bash examples/run_demo.sh