-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from bchaoss/main
add CircularQueue.py
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Circular Queue implementation using Lists (Pseudo Arrays) | ||
from typing import Any, Optional | ||
|
||
from datastax.Utils.Exceptions import OverflowException, UnderflowException | ||
from datastax.Arrays import Queue | ||
|
||
|
||
class CircularQueue(Queue): | ||
def __init__(self, *, capacity: Optional[int] = None): | ||
super().__init__(capacity=capacity) | ||
self._front = 0 | ||
self._rear = 0 | ||
|
||
def is_full(self) -> bool: | ||
if not super().is_full(): | ||
return False | ||
return (self._rear + 1) % self.capacity == self._front | ||
|
||
def is_empty(self) -> bool: | ||
if super().is_empty(): | ||
return True | ||
return self._front == self._rear | ||
|
||
def enqueue(self, item: Any) -> int: | ||
if self.is_full(): | ||
raise OverflowException(self) | ||
|
||
if not super().is_full(): | ||
self._array.append(item) | ||
else: | ||
self._array[self._rear] = item | ||
self._rear = (self._rear + 1) % self.capacity | ||
return 0 | ||
|
||
def dequeue(self) -> Any: | ||
if self.is_empty(): | ||
raise UnderflowException(self) | ||
deleted_item = self._array[self.front] | ||
self._front = (self._front + 1) % self.capacity | ||
return deleted_item | ||
|
||
def peek(self) -> Any: | ||
if self.is_empty(): | ||
return "CIRCULAR QUEUE EMPTY" | ||
return self._array[self._front] |