From 0d077d1f6983cb97620bb0c211b73c52cfcaa3c0 Mon Sep 17 00:00:00 2001 From: Fangyu Gai Date: Wed, 10 Oct 2018 22:57:44 -0700 Subject: [PATCH] Add data migration --- python/09_queue/array_queue.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/python/09_queue/array_queue.py b/python/09_queue/array_queue.py index 40f34c2..1b1252e 100644 --- a/python/09_queue/array_queue.py +++ b/python/09_queue/array_queue.py @@ -16,9 +16,16 @@ class ArrayQueue: self._tail = 0 def enqueue(self, item: str) -> bool: - if self._tail == self._capacity: return False + if self._tail == self._capacity: + if self._head == 0: + return False + else: + for i in range(0, self._tail - self._head): + self._data[i] = self._items[i + self._head] + self._tail = self._tail - self._head + self._head = 0 - self._items.append(item) + self._items.insert(self._tail, item) self._tail += 1 return True