mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update and rename circular_queue_III.py to circular_queue_list.py
This commit is contained in:
parent
ad2c24a519
commit
18c33edfb0
@ -1,7 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# author: bt3gl
|
# author: bt3gl
|
||||||
# this is my favorite implementation <3
|
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
@ -13,17 +12,14 @@ class Node:
|
|||||||
class CircularQueue:
|
class CircularQueue:
|
||||||
|
|
||||||
def __init__(self, k: int):
|
def __init__(self, k: int):
|
||||||
|
|
||||||
self.capacity = k
|
self.capacity = k
|
||||||
self.count = 0
|
self.count = 0
|
||||||
self.head = None
|
self.head = None
|
||||||
self.tail = None
|
self.tail = None
|
||||||
|
|
||||||
def enqueue(self, value: int) -> bool:
|
def enqueue(self, value: int) -> bool:
|
||||||
|
|
||||||
if self.count == self.capacity:
|
if self.count == self.capacity:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if self.count == 0:
|
if self.count == 0:
|
||||||
self.head = Node(value)
|
self.head = Node(value)
|
||||||
self.tail = self.head
|
self.tail = self.head
|
||||||
@ -31,40 +27,30 @@ class CircularQueue:
|
|||||||
new_node = Node(value)
|
new_node = Node(value)
|
||||||
self.tail.next = new_node
|
self.tail.next = new_node
|
||||||
self.tail = new_node
|
self.tail = new_node
|
||||||
|
|
||||||
self.count += 1
|
self.count += 1
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def dequeue(self) -> bool:
|
def dequeue(self) -> bool:
|
||||||
|
|
||||||
if self.count == 0:
|
if self.count == 0:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.head = self.head.next
|
self.head = self.head.next
|
||||||
self.count -= 1
|
self.count -= 1
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def front(self) -> int:
|
def front(self) -> int:
|
||||||
|
|
||||||
if self.count == 0:
|
if self.count == 0:
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
return self.head.value
|
return self.head.value
|
||||||
|
|
||||||
def rear(self) -> int:
|
def rear(self) -> int:
|
||||||
|
|
||||||
if self.count == 0:
|
if self.count == 0:
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
return self.tail.value
|
return self.tail.value
|
||||||
|
|
||||||
def is_empty(self) -> bool:
|
def is_empty(self) -> bool:
|
||||||
|
|
||||||
return self.count == 0
|
return self.count == 0
|
||||||
|
|
||||||
def is_full(self) -> bool:
|
def is_full(self) -> bool:
|
||||||
|
|
||||||
return self.count == self.capacity
|
return self.count == self.capacity
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user