mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-22 16:31:15 -04:00
Update and rename circular_queue.py to circular_queue_array.py
This commit is contained in:
parent
3d361330b7
commit
71ca605489
1 changed files with 1 additions and 0 deletions
|
@ -1,63 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
class CircularQueue:
|
||||
|
||||
def __init__(self, k: int):
|
||||
self.head = 0
|
||||
self.tail = 0
|
||||
self.size = k
|
||||
self.queue = [None] * self.size
|
||||
|
||||
def enqueue(self, value: int) -> bool:
|
||||
|
||||
if value is None:
|
||||
return False
|
||||
|
||||
if self.is_full():
|
||||
return False
|
||||
|
||||
if self.is_empty():
|
||||
self.heard = 0
|
||||
|
||||
while self.queue[self.tail] is not None:
|
||||
self.tail += 1
|
||||
if self.tail == self.size:
|
||||
self.tail = 0
|
||||
|
||||
self.queue[self.tail] = value
|
||||
return True
|
||||
|
||||
def dequeue(self) -> bool:
|
||||
|
||||
if self.is_empty():
|
||||
return False
|
||||
|
||||
value = self.queue[self.head]
|
||||
self.queue[self.head] = None
|
||||
self.head += 1
|
||||
|
||||
if self.head == self.size:
|
||||
self.head = 0
|
||||
|
||||
return True
|
||||
|
||||
def front(self) -> int:
|
||||
return self.queue[self.head] or -1
|
||||
|
||||
def rear(self) -> int:
|
||||
return self.queue[self.tail] or -1
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
for n in self.queue:
|
||||
if n is not None:
|
||||
return False
|
||||
return True
|
||||
|
||||
def is_full(self) -> bool:
|
||||
for n in self.queue:
|
||||
if n is None:
|
||||
return False
|
||||
return True
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue