|
| 1 | +class CircQueue: |
| 2 | + """ |
| 3 | + Implementation of circular, array-backed queue data structure |
| 4 | + """ |
| 5 | + |
| 6 | + # circular queue keeps track of position of recently enqueued item and |
| 7 | + # position of next item to dequeue |
| 8 | + def __init__(self, limit=20): |
| 9 | + self.data = [None] * limit |
| 10 | + self.head = -1 |
| 11 | + self.tail = -1 |
| 12 | + |
| 13 | + # add item into the queue |
| 14 | + def enqueue(self, val): |
| 15 | + if (self.tail + 1) % len(self.data) == self.head: |
| 16 | + raise RuntimeError |
| 17 | + |
| 18 | + if self.head == self.tail == -1: |
| 19 | + self.tail = 0 |
| 20 | + self.head = 0 |
| 21 | + else: |
| 22 | + self.tail = (self.tail + 1) % len(self.data) |
| 23 | + |
| 24 | + self.data[self.tail] = val |
| 25 | + |
| 26 | + # remove and return the first item in the queue (first in first out) |
| 27 | + def dequeue(self): |
| 28 | + if self.head == -1: |
| 29 | + raise RuntimeError |
| 30 | + |
| 31 | + data = self.data[self.head] |
| 32 | + self.data[self.head] = None |
| 33 | + |
| 34 | + if self.head == self.tail: |
| 35 | + self.head = -1 |
| 36 | + self.tail = -1 |
| 37 | + else: |
| 38 | + self.head = (self.head + 1) % len(self.data) |
| 39 | + |
| 40 | + return data |
| 41 | + |
| 42 | + # resize data array to given size |
| 43 | + def resize(self, newsize): |
| 44 | + assert newsize > len(self.data) |
| 45 | + |
| 46 | + new_data = [] |
| 47 | + for item in self: |
| 48 | + new_data.append(item) |
| 49 | + |
| 50 | + self.tail = len(new_data) - 1 |
| 51 | + |
| 52 | + for _ in range(newsize - len(self.data)): |
| 53 | + new_data.append(None) |
| 54 | + |
| 55 | + self.data = new_data |
| 56 | + self.head = 0 |
| 57 | + |
| 58 | + # return True if queue is empty, otherwise False |
| 59 | + def empty(self): |
| 60 | + for item in self.data: |
| 61 | + if item is not None: |
| 62 | + return False |
| 63 | + return True |
| 64 | + |
| 65 | + # return True if queue is not empty, otherwise False |
| 66 | + def __bool__(self): |
| 67 | + return not self.empty() |
| 68 | + |
| 69 | + # return all data in queue in a string |
| 70 | + def __str__(self): |
| 71 | + if not self: |
| 72 | + return '' |
| 73 | + return ', '.join(str(item) for item in self) |
| 74 | + |
| 75 | + # return all data in queue in a string |
| 76 | + def __repr__(self): |
| 77 | + return str(self) |
| 78 | + |
| 79 | + # iter through the queue |
| 80 | + def __iter__(self): |
| 81 | + idx = self.head |
| 82 | + while idx != self.tail: |
| 83 | + yield self.data[idx] |
| 84 | + idx = (idx + 1) % len(self.data) |
| 85 | + yield self.data[idx] |
| 86 | + |
| 87 | + |
| 88 | +# print option menu and take user input |
| 89 | +def option_menu(): |
| 90 | + print("1. Enqueue") |
| 91 | + print("2. Dequeue") |
| 92 | + print("3. Resize Queue") |
| 93 | + print("4. Print Queue") |
| 94 | + print("5. Exit") |
| 95 | + |
| 96 | + try: |
| 97 | + option = int(input("Enter option: ")) |
| 98 | + return option |
| 99 | + |
| 100 | + except Exception as e: |
| 101 | + print(f"ERROR: {e}\n") |
| 102 | + |
| 103 | +# test function |
| 104 | +def test(): |
| 105 | + q = CircQueue() |
| 106 | + |
| 107 | + while True: |
| 108 | + option = option_menu() |
| 109 | + |
| 110 | + if option == 1: |
| 111 | + while True: |
| 112 | + try: |
| 113 | + item = int(input("Enter value to enqueue (enter character to quit): ")) |
| 114 | + q.enqueue(item) |
| 115 | + except: |
| 116 | + print() |
| 117 | + break |
| 118 | + |
| 119 | + elif option == 2: |
| 120 | + print("Result of dequeue: ", q.dequeue(), "\n") |
| 121 | + |
| 122 | + elif option == 3: |
| 123 | + new_size = int(input("Enter new size: ")) |
| 124 | + q.resize(new_size) |
| 125 | + |
| 126 | + elif option == 4: |
| 127 | + print("Current queue: ", q) |
| 128 | + print("Current queue data: ", q.data, "\n") |
| 129 | + |
| 130 | + elif option == 5: |
| 131 | + print("Exiting...") |
| 132 | + break |
| 133 | + |
| 134 | + else: |
| 135 | + print("Invalid option\n") |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + test() |
0 commit comments