mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update README.md
This commit is contained in:
parent
18c33edfb0
commit
f895ac1da8
@ -112,55 +112,56 @@ class CircularQueue:
|
|||||||
<br>
|
<br>
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
class Node:
|
||||||
|
def __init__(self, value, next=None):
|
||||||
|
self.value = value
|
||||||
|
self.next = next
|
||||||
|
|
||||||
|
|
||||||
class CircularQueue:
|
class CircularQueue:
|
||||||
|
|
||||||
def __init__(self, k: int):
|
def __init__(self, k: int):
|
||||||
self.head = -1
|
self.capacity = k
|
||||||
self.tail = -1
|
self.count = 0
|
||||||
self.size = k
|
self.head = None
|
||||||
self.queue = [None] * self.size
|
self.tail = None
|
||||||
|
|
||||||
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.is_full():
|
if self.count == self.capacity:
|
||||||
return False
|
return False
|
||||||
|
if self.count == 0:
|
||||||
if self.is_empty() :
|
self.head = Node(value)
|
||||||
self.head = 0;
|
self.tail = self.head
|
||||||
|
else:
|
||||||
self.tail = self._get_next_position(self.tail)
|
new_node = Node(value)
|
||||||
self.queue[self.tail] = value
|
self.tail.next = new_node
|
||||||
|
self.tail = new_node
|
||||||
|
self.count += 1
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def dequeue(self) -> bool:
|
def dequeue(self) -> bool:
|
||||||
if self.is_empty():
|
if self.count == 0:
|
||||||
return False
|
return False
|
||||||
|
self.head = self.head.next
|
||||||
if self.head == self.tail:
|
self.count -= 1
|
||||||
self.head = -1
|
|
||||||
self.tail = -1
|
|
||||||
return True
|
|
||||||
|
|
||||||
self.head = self._get_next_position(self.head)
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def front(self) -> int:
|
def front(self) -> int:
|
||||||
if self.is_empty():
|
if self.count == 0:
|
||||||
return -1
|
return -1
|
||||||
return self.queue[self.head]
|
|
||||||
|
return self.head.value
|
||||||
|
|
||||||
def rear(self) -> int:
|
def rear(self) -> int:
|
||||||
if self.is_empty():
|
if self.count == 0:
|
||||||
return -1
|
return -1
|
||||||
return self.queue[self.tail]
|
return self.tail.value
|
||||||
|
|
||||||
def is_empty(self) -> bool:
|
def is_empty(self) -> bool:
|
||||||
return self.head == -1
|
return self.count == 0
|
||||||
|
|
||||||
def is_full(self) -> bool:
|
def is_full(self) -> bool:
|
||||||
return self._get_next_position(self.tail) == self.head
|
return self.count == self.capacity
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user