mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update circular_queue_II.py
This commit is contained in:
parent
0bc7deb3af
commit
04cd4a4b7b
@ -2,9 +2,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# author: bt3gl
|
# author: bt3gl
|
||||||
|
|
||||||
## implement a circular queue
|
|
||||||
|
|
||||||
|
|
||||||
class CircularQueue:
|
class CircularQueue:
|
||||||
|
|
||||||
def __init__(self, k: int):
|
def __init__(self, k: int):
|
||||||
@ -13,15 +10,18 @@ class CircularQueue:
|
|||||||
self.size = k
|
self.size = k
|
||||||
self.queue = [None] * self.size
|
self.queue = [None] * self.size
|
||||||
|
|
||||||
|
def _get_next_position(self, end) -> int:
|
||||||
|
return (end + 1) % self.size
|
||||||
|
|
||||||
def enQueue(self, value: int) -> bool:
|
def enQueue(self, value: int) -> bool:
|
||||||
|
|
||||||
if self.isFull():
|
if self.isFull():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if self.isEmpty():
|
if self.isEmpty() :
|
||||||
head = 0;
|
self.head = 0;
|
||||||
|
|
||||||
self.tail = (self.tail + 1) % self.size
|
self.tail = self._get_next_position(self.tail)
|
||||||
self.queue[self.tail] = value
|
self.queue[self.tail] = value
|
||||||
|
|
||||||
return True
|
return True
|
||||||
@ -36,7 +36,7 @@ class CircularQueue:
|
|||||||
self.tail = -1
|
self.tail = -1
|
||||||
return True
|
return True
|
||||||
|
|
||||||
self.head = (self.head + 1) % self.size
|
self.head = self._get_next_position(self.head)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ class CircularQueue:
|
|||||||
return self.head == -1
|
return self.head == -1
|
||||||
|
|
||||||
def isFull(self) -> bool:
|
def isFull(self) -> bool:
|
||||||
return (self.tail + 1) % self.size == self.head
|
return self._get_next_position(self.tail) == self.head
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user