GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — algo/python/09_queue/dynamic_array_queue.py at master · oooou/algo Expand file tree/ Copy pathdynamic_array_queue.pyMore file actions 51 lines (40 loc) · 1.2 KB / Copy pathdynamic_array_queue.pyFile metadata and controls51 lines (40 loc) · 1.2 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 """ Author: Wenru """ from typing import Optional class DynamicArrayQueue: def __init__(self, capacity: int): self._items = [] self._capacity = capacity self._head = 0 self._tail = 0 def enqueue(self, item: str) -> bool: if self._tail == self._capacity: if self._head == 0: return False self._items[0 : self._tail - self._head] = self._items[self._head : self._tail] self._tail -= self._head self._head = 0 if self._tail == len(self._items): self._items.append(item) else: self._items[self._tail] = item self._tail += 1 return True def dequeue(self) -> Optional[str]: if self._head != self._tail: item = self._items[self._head] self._head += 1 return item def __repr__(self) -> str: return " ".join(item for item in self._items[self._head:self._tail]) if __name__ == "__main__": q = DynamicArrayQueue(10) for i in range(10): q.enqueue(str(i)) print(q) for _ in range(3): q.dequeue() print(q) q.enqueue("7") q.enqueue("8") print(q) |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |