mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-23 00:41:19 -04:00
Create queue_list_II.py
This commit is contained in:
parent
7e40ca15da
commit
8da5fea499
1 changed files with 58 additions and 0 deletions
58
queues/queue_list_II.py
Normal file
58
queues/queue_list_II.py
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# author: bt3gl
|
||||||
|
|
||||||
|
class CircularQueue:
|
||||||
|
|
||||||
|
def __init__(self, size):
|
||||||
|
self.head = 0
|
||||||
|
self.count = 0
|
||||||
|
self.queue = [0] * size
|
||||||
|
self.size = size
|
||||||
|
|
||||||
|
def _get_tail(self):
|
||||||
|
return (self.head + self.count - 1) % self.size
|
||||||
|
|
||||||
|
def _get_next_tail(self):
|
||||||
|
return (self.head + self.count) % self.size
|
||||||
|
|
||||||
|
def _get_next_head(self):
|
||||||
|
return (self.head + 1) % self.size
|
||||||
|
|
||||||
|
def enqueue(self, value: int) -> bool:
|
||||||
|
|
||||||
|
if self.is_empty():
|
||||||
|
return False
|
||||||
|
|
||||||
|
self.queue[self._get_next_tail()] = value
|
||||||
|
self.count += 1
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def dequeue(self) -> bool:
|
||||||
|
|
||||||
|
if self.is_empty():
|
||||||
|
return False
|
||||||
|
|
||||||
|
self.head = self._get_next_head()
|
||||||
|
self.count -= 1
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def Front(self) -> int:
|
||||||
|
if self.is_empty():
|
||||||
|
return False
|
||||||
|
|
||||||
|
return self.queue[self.head]
|
||||||
|
|
||||||
|
def Rear(self) -> int:
|
||||||
|
if self.is_empty():
|
||||||
|
return False
|
||||||
|
|
||||||
|
return self.queue[self._get_tail()]
|
||||||
|
|
||||||
|
def isEmpty(self) -> bool:
|
||||||
|
return self.count == 0
|
||||||
|
|
||||||
|
def isFull(self) -> bool:
|
||||||
|
return self.count == self.size
|
Loading…
Add table
Add a link
Reference in a new issue