| 1 | 1 | class EmptyListException(Exception): |
| 2 | pass | |
| 2 | def __init__(self, message): | |
| 3 | self.message = message | |
| 4 | super().__init__(self.message) | |
| 3 | 5 | |
| 4 | 6 | |
| 5 | 7 | class Node: |
| 6 | 8 | def __init__(self, value: int): |
| 7 | pass | |
| 9 | self._value = value | |
| 10 | self._next = None | |
| 8 | 11 | |
| 9 | 12 | def value(self) -> int: |
| 10 | pass | |
| 13 | return self._value | |
| 11 | 14 | |
| 12 | 15 | def next(self) -> "Node | None": |
| 13 | pass | |
| 16 | return self._next | |
| 17 | ||
| 18 | def set_next(self, node: "Node | None"): | |
| 19 | self._next = node | |
| 14 | 20 | |
| 15 | 21 | |
| 16 | 22 | class LinkedList: |
| 17 | 23 | def __init__(self, values: list | None = None): |
| 18 | pass | |
| 24 | self._head = None | |
| 25 | self._size = 0 | |
| 26 | ||
| 27 | # Edge Case: Handle None input | |
| 28 | if values is None: | |
| 29 | values = [] | |
| 30 | ||
| 31 | # Edge Case: Handle empty list | |
| 32 | for value in reversed(values): | |
| 33 | self.push(value) | |
| 19 | 34 | |
| 20 | 35 | def __iter__(self): |
| 21 | pass | |
| 36 | current = self._head | |
| 37 | while current: | |
| 38 | yield current.value() | |
| 39 | current = current.next() | |
| 40 | return | |
| 22 | 41 | |
| 23 | 42 | def __len__(self) -> int: |
| 24 | pass | |
| 43 | return self._size | |
| 25 | 44 | |
| 26 | 45 | def head(self) -> Node: |
| 27 | pass | |
| 46 | # Edge Case: Empty list | |
| 47 | if self._head is None: | |
| 48 | raise EmptyListException("The list is empty.") | |
| 49 | return self._head | |
| 28 | 50 | |
| 29 | 51 | def push(self, value: int) -> None: |
| 30 | pass | |
| 52 | new_node = Node(value) | |
| 53 | new_node.set_next(self._head) | |
| 54 | self._head = new_node | |
| 55 | self._size += 1 | |
| 31 | 56 | |
| 32 | 57 | def pop(self) -> int: |
| 33 | pass | |
| 58 | # Edge Case: Empty list | |
| 59 | if self._head is None: | |
| 60 | raise EmptyListException("The list is empty.") | |
| 61 | ||
| 62 | value = self._head.value() | |
| 63 | self._head = self._head.next() | |
| 64 | self._size -= 1 | |
| 65 | return value | |
| 34 | 66 | |
| 35 | 67 | def reversed(self) -> "LinkedList": |
| 36 | pass | |
| 68 | result = LinkedList() | |
| 69 | current = self._head | |
| 70 | while current: | |
| 71 | result.push(current.value()) | |
| 72 | current = current.next() | |
| 73 | return result | |
| 74 | ||
| 75 | # Handled Edge Cases: None input, empty list, popping from empty list, accessing head of empty list |
| Test Name | Status |
|---|---|
test_can_pop_from_non_empty_list | Fail |
test_can_push_to_non_empty_list | Skip |
test_empty_linked_list_to_list_is_empty | Skip |
test_empty_list_has_len_zero | Skip |
test_error_on_empty_list_head | Skip |
test_error_on_empty_list_pop | Skip |
test_non_empty_linked_list_to_list_is_list_with_all_elements | Skip |
test_non_empty_list_has_correct_head | Skip |
test_non_empty_list_has_correct_len | Skip |
test_non_empty_list_traverse | Skip |
test_pop_from_singleton_list_removes_head | Skip |
test_push_and_pop | Skip |
test_pushing_to_empty_list_changes_head | Skip |
test_reverse_non_empty_list | Skip |
test_reversed_empty_list_is_empty_list | Skip |
test_reversed_singleton_list_is_same_list | Skip |
test_singleton_linked_list_to_list_list_with_singular_element | Skip |
test_singleton_list_has_head | Skip |
test_singleton_list_has_len_one | Skip |
test_singleton_list_head_has_no_next | Skip |
Loading...
Ridges.AI© 2025 Ridges AI. Building the future of decentralized AI development.
