| 1 | 1 | class Node: |
| 2 | 2 | def __init__(self, value, succeeding=None, previous=None): |
| 3 | | pass |
| 3 | self.value = value |
| 4 | self.succeeding = succeeding |
| 5 | self.previous = previous |
| 4 | 6 | |
| 5 | 7 | |
| 6 | 8 | class LinkedList: |
| 7 | 9 | def __init__(self): |
| 8 | | pass |
| 10 | self.head = None |
| 11 | self.tail = None |
| 12 | self.length = 0 |
| 13 | |
| 14 | def push(self, value): |
| 15 | """Add a node with the specified value to the end of the list.""" |
| 16 | new_node = Node(value) |
| 17 | # Edge Case: Adding to an empty list |
| 18 | if self.length == 0: |
| 19 | self.head = new_node |
| 20 | self.tail = new_node |
| 21 | else: |
| 22 | new_node.previous = self.tail |
| 23 | self.tail.succeeding = new_node |
| 24 | self.tail = new_node |
| 25 | self.length += 1 |
| 26 | # Handled Edge Cases: Adding to an empty list |
| 27 | |
| 28 | def pop(self): |
| 29 | """Remove and return the last element of the list.""" |
| 30 | # Edge Case: Popping from an empty list |
| 31 | if self.length == 0: |
| 32 | raise IndexError("List is empty") |
| 33 | |
| 34 | value = self.tail.value |
| 35 | # Edge Case: Removing the last node (list becomes empty) |
| 36 | if self.length == 1: |
| 37 | self.head = None |
| 38 | self.tail = None |
| 39 | else: |
| 40 | self.tail = self.tail.previous |
| 41 | self.tail.succeeding = None |
| 42 | self.length -= 1 |
| 43 | return value |
| 44 | # Handled Edge Cases: Popping from an empty list, removing the last node |
| 45 | |
| 46 | def unshift(self, value): |
| 47 | """Add a node with the specified value to the beginning of the list.""" |
| 48 | new_node = Node(value) |
| 49 | # Edge Case: Adding to an empty list |
| 50 | if self.length == 0: |
| 51 | self.head = new_node |
| 52 | self.tail = new_node |
| 53 | else: |
| 54 | new_node.succeeding = self.head |
| 55 | self.head.previous = new_node |
| 56 | self.head = new_node |
| 57 | self.length += 1 |
| 58 | # Handled Edge Cases: Adding to an empty list |
| 59 | |
| 60 | def shift(self): |
| 61 | """Remove and return the first element of the list.""" |
| 62 | # Edge Case: Shifting from an empty list |
| 63 | if self.length == 0: |
| 64 | raise IndexError("List is empty") |
| 65 | |
| 66 | value = self.head.value |
| 67 | # Edge Case: Removing the last node (list becomes empty) |
| 68 | if self.length == 1: |
| 69 | self.head = None |
| 70 | self.tail = None |
| 71 | else: |
| 72 | self.head = self.head.succeeding |
| 73 | self.head.previous = None |
| 74 | self.length -= 1 |
| 75 | return value |
| 76 | # Handled Edge Cases: Shifting from an empty list, removing the last node |
| 77 | |
| 78 | def delete(self, value): |
| 79 | """Remove the first occurrence of a node with the specified value.""" |
| 80 | # Edge Case: Deleting from an empty list |
| 81 | if self.length == 0: |
| 82 | raise ValueError("Value not found") |
| 83 | |
| 84 | current = self.head |
| 85 | # Edge Case: Deleting the head node |
| 86 | if current.value == value: |
| 87 | return self.shift() |
| 88 | |
| 89 | # Edge Case: Deleting the tail node |
| 90 | if self.tail.value == value: |
| 91 | return self.pop() |
| 92 | |
| 93 | # Search for the value in the middle of the list |
| 94 | while current and current.value != value: |
| 95 | current = current.succeeding |
| 96 | |
| 97 | # Edge Case: Value not found in the list |
| 98 | if current is None: |
| 99 | raise ValueError("Value not found") |
| 100 | |
| 101 | # Remove the node |
| 102 | current.previous.succeeding = current.succeeding |
| 103 | current.succeeding.previous = current.previous |
| 104 | self.length -= 1 |
| 105 | return current.value |
| 106 | # Handled Edge Cases: Deleting from an empty list, deleting head node, deleting tail node, value not found |
| 107 | |
| 108 | def __len__(self): |
| 109 | """Return the length of the list.""" |
| 110 | return self.length |
| 111 | # Handled Edge Cases: None (this is a simple getter) |
| 112 | |
| 113 | def __iter__(self): |
| 114 | """Allow iteration over the list.""" |
| 115 | current = self.head |
| 116 | while current: |
| 117 | yield current.value |
| 118 | current = current.succeeding |
| 119 | # Handled Edge Cases: None (empty list is naturally handled by while loop) |