Update circular_queue_II.py

This commit is contained in:
marina 2023-08-01 14:40:02 -07:00 committed by GitHub
parent 0bc7deb3af
commit 04cd4a4b7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,9 +2,6 @@
# -*- coding: utf-8 -*-
# author: bt3gl
## implement a circular queue
class CircularQueue:
def __init__(self, k: int):
@ -13,15 +10,18 @@ class CircularQueue:
self.size = k
self.queue = [None] * self.size
def _get_next_position(self, end) -> int:
return (end + 1) % self.size
def enQueue(self, value: int) -> bool:
if self.isFull():
return False
if self.isEmpty():
head = 0;
self.tail = (self.tail + 1) % self.size
if self.isEmpty() :
self.head = 0;
self.tail = self._get_next_position(self.tail)
self.queue[self.tail] = value
return True
@ -36,7 +36,7 @@ class CircularQueue:
self.tail = -1
return True
self.head = (self.head + 1) % self.size
self.head = self._get_next_position(self.head)
return True
@ -54,7 +54,7 @@ class CircularQueue:
return self.head == -1
def isFull(self) -> bool:
return (self.tail + 1) % self.size == self.head
return self._get_next_position(self.tail) == self.head
if __name__ == "__main__":