|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | + |
| 4 | +class _BaseQueue: |
| 5 | + class _EmptyQueueError(Exception): |
| 6 | + def __str__(self) -> str: |
| 7 | + return "Queue is empty." |
| 8 | + |
| 9 | + def __init__(self) -> None: |
| 10 | + self._data: list[Optional[object]] = [None] * 10 |
| 11 | + self._size: int = 0 |
| 12 | + |
| 13 | + def __len__(self) -> int: |
| 14 | + return self._size |
| 15 | + |
| 16 | + def length(self) -> int: |
| 17 | + return self.__len__() |
| 18 | + |
| 19 | + def __str__(self) -> str: |
| 20 | + return str(self._data) |
| 21 | + |
| 22 | + def is_empty(self) -> bool: |
| 23 | + return self._size == 0 |
| 24 | + |
| 25 | + |
| 26 | +class SingleEndedQueue(_BaseQueue): |
| 27 | + def __init__(self) -> None: |
| 28 | + super().__init__() |
| 29 | + self._front: int = 0 |
| 30 | + |
| 31 | + def __eq__(self, other: 'SingleEndedQueue') -> bool: |
| 32 | + if self.__class__ == other.__class__: |
| 33 | + if self._data == other._data and self._front == other._front: |
| 34 | + return True |
| 35 | + return False |
| 36 | + |
| 37 | + def first(self) -> Optional[object]: |
| 38 | + if self.is_empty(): |
| 39 | + raise self._EmptyQueueError |
| 40 | + return self._data[self._front] |
| 41 | + |
| 42 | + def _resize(self, capacity: int) -> None: |
| 43 | + old: list[Optional[object]] = self._data |
| 44 | + self._data = [None] * capacity |
| 45 | + walk: int = self._front |
| 46 | + for k in range(self._size): |
| 47 | + self._data[k] = old[walk] |
| 48 | + walk = (1 + walk) % len(old) |
| 49 | + self._front = 0 |
| 50 | + |
| 51 | + def enqueue_back(self, element: Optional[object]) -> None: |
| 52 | + if self._size == len(self._data): |
| 53 | + self._resize(2 * len(self._data)) |
| 54 | + avail: int = (self._front + self._size) % len(self._data) |
| 55 | + self._data[avail] = element |
| 56 | + self._size += 1 |
| 57 | + |
| 58 | + def dequeue_front(self) -> Optional[object]: |
| 59 | + if self.is_empty(): |
| 60 | + raise self._EmptyQueueError |
| 61 | + element: Optional[object] = self._data[self._front] |
| 62 | + self._data[self._front] = None |
| 63 | + self._front = (self._front + 1) % len(self._data) |
| 64 | + self._size -= 1 |
| 65 | + return element |
| 66 | + |
| 67 | + |
| 68 | +class DoubleEndedQueue(SingleEndedQueue): |
| 69 | + def __init__(self) -> None: |
| 70 | + super().__init__() |
| 71 | + self._back = 0 |
| 72 | + |
| 73 | + def __eq__(self, other: 'DoubleEndedQueue') -> bool: |
| 74 | + if self.__class__ == other.__class__: |
| 75 | + if self._data == other._data and self._front == other._front and self._back == other._back: |
| 76 | + return True |
| 77 | + return False |
| 78 | + |
| 79 | + def last(self) -> Optional[object]: |
| 80 | + if self.is_empty(): |
| 81 | + raise self._EmptyQueueError |
| 82 | + return self._data[self._back] |
| 83 | + |
| 84 | + def _resize(self, capacity: int) -> None: |
| 85 | + super(DoubleEndedQueue, self)._resize(capacity) |
| 86 | + self._back = (self._front + self._size - 1) % len(self._data) |
| 87 | + |
| 88 | + def enqueue_back(self, element: Optional[object]) -> None: |
| 89 | + super(DoubleEndedQueue, self).enqueue_back(element) |
| 90 | + self._back = (self._front + self._size - 1) % len(self._data) |
| 91 | + |
| 92 | + def dequeue_front(self) -> Optional[object]: |
| 93 | + element: Optional[object] = super(DoubleEndedQueue, self).dequeue_front() |
| 94 | + self._back = (self._front + self._size - 1) % len(self._data) |
| 95 | + return element |
| 96 | + |
| 97 | + def enqueue_front(self, element: Optional[object]) -> None: |
| 98 | + if self._size == len(self._data): |
| 99 | + self._resize(2 * len(self._data)) |
| 100 | + self._front = (self._front - 1) % len(self._data) |
| 101 | + self._data[self._front] = element |
| 102 | + self._size += 1 |
| 103 | + self._back = (self._front + self._size - 1) % len(self._data) |
| 104 | + |
| 105 | + def dequeue_back(self) -> Optional[object]: |
| 106 | + if self.is_empty(): |
| 107 | + raise self._EmptyQueueError |
| 108 | + back: int = (self._front + self._size - 1) % len(self._data) |
| 109 | + element: Optional[object] = self._data[back] |
| 110 | + self._data[back] = None |
| 111 | + self._size -= 1 |
| 112 | + self._back = (self._front + self._size - 1) % len(self._data) |
| 113 | + return element |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + print("1. Single Ended Queue") |
| 118 | + print("2. Double Ended Queue") |
| 119 | + mode = int(input("Choose:")) |
| 120 | + if mode == 1: |
| 121 | + single_ended_queue = SingleEndedQueue() |
| 122 | + for i in range(1, 11): |
| 123 | + single_ended_queue.enqueue_back("f_" + str(i)) |
| 124 | + print(single_ended_queue.first()) |
| 125 | + print(single_ended_queue.dequeue_front()) |
| 126 | + single_ended_queue.enqueue_back("f_" + str(11)) |
| 127 | + print(single_ended_queue.first()) |
| 128 | + print(single_ended_queue) |
| 129 | + elif mode == 2: |
| 130 | + double_ended_queue = DoubleEndedQueue() |
| 131 | + for i in range(1, 6): |
| 132 | + double_ended_queue.enqueue_back("f_" + str(i)) |
| 133 | + print(double_ended_queue.first()) |
| 134 | + print(double_ended_queue.dequeue_front()) |
| 135 | + double_ended_queue.enqueue_front("f_" + str(1)) |
| 136 | + print(double_ended_queue.first()) |
| 137 | + print(double_ended_queue.last()) |
| 138 | + print(double_ended_queue.dequeue_back()) |
| 139 | + print(double_ended_queue.last()) |
0 commit comments