mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 16:27:02 +00:00
[mypy] fix type annotations in data_structures/queue/circular_queue_linked_list.py
(#5749)
* [mypy] fix type annotations in circular_queue_linked_list * Remove 10 blank lines Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
3c8fec1316
commit
24731b078c
|
@ -1,6 +1,8 @@
|
||||||
# Implementation of Circular Queue using linked lists
|
# Implementation of Circular Queue using linked lists
|
||||||
# https://en.wikipedia.org/wiki/Circular_buffer
|
# https://en.wikipedia.org/wiki/Circular_buffer
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,8 +20,8 @@ class CircularQueueLinkedList:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, initial_capacity: int = 6) -> None:
|
def __init__(self, initial_capacity: int = 6) -> None:
|
||||||
self.front = None
|
self.front: Node | None = None
|
||||||
self.rear = None
|
self.rear: Node | None = None
|
||||||
self.create_linked_list(initial_capacity)
|
self.create_linked_list(initial_capacity)
|
||||||
|
|
||||||
def create_linked_list(self, initial_capacity: int) -> None:
|
def create_linked_list(self, initial_capacity: int) -> None:
|
||||||
|
@ -27,7 +29,7 @@ class CircularQueueLinkedList:
|
||||||
self.front = current_node
|
self.front = current_node
|
||||||
self.rear = current_node
|
self.rear = current_node
|
||||||
previous_node = current_node
|
previous_node = current_node
|
||||||
for i in range(1, initial_capacity):
|
for _ in range(1, initial_capacity):
|
||||||
current_node = Node()
|
current_node = Node()
|
||||||
previous_node.next = current_node
|
previous_node.next = current_node
|
||||||
current_node.prev = previous_node
|
current_node.prev = previous_node
|
||||||
|
@ -49,9 +51,14 @@ class CircularQueueLinkedList:
|
||||||
>>> cq.is_empty()
|
>>> cq.is_empty()
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
return self.front == self.rear and self.front.data is None
|
|
||||||
|
|
||||||
def first(self) -> Any:
|
return (
|
||||||
|
self.front == self.rear
|
||||||
|
and self.front is not None
|
||||||
|
and self.front.data is None
|
||||||
|
)
|
||||||
|
|
||||||
|
def first(self) -> Any | None:
|
||||||
"""
|
"""
|
||||||
Returns the first element of the queue
|
Returns the first element of the queue
|
||||||
>>> cq = CircularQueueLinkedList()
|
>>> cq = CircularQueueLinkedList()
|
||||||
|
@ -74,7 +81,7 @@ class CircularQueueLinkedList:
|
||||||
'b'
|
'b'
|
||||||
"""
|
"""
|
||||||
self.check_can_perform_operation()
|
self.check_can_perform_operation()
|
||||||
return self.front.data
|
return self.front.data if self.front else None
|
||||||
|
|
||||||
def enqueue(self, data: Any) -> None:
|
def enqueue(self, data: Any) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -92,11 +99,13 @@ class CircularQueueLinkedList:
|
||||||
...
|
...
|
||||||
Exception: Empty Queue
|
Exception: Empty Queue
|
||||||
"""
|
"""
|
||||||
|
if self.rear is None:
|
||||||
|
return
|
||||||
|
|
||||||
self.check_is_full()
|
self.check_is_full()
|
||||||
if self.is_empty():
|
if not self.is_empty():
|
||||||
self.rear.data = data
|
|
||||||
else:
|
|
||||||
self.rear = self.rear.next
|
self.rear = self.rear.next
|
||||||
|
if self.rear:
|
||||||
self.rear.data = data
|
self.rear.data = data
|
||||||
|
|
||||||
def dequeue(self) -> Any:
|
def dequeue(self) -> Any:
|
||||||
|
@ -117,6 +126,8 @@ class CircularQueueLinkedList:
|
||||||
Exception: Empty Queue
|
Exception: Empty Queue
|
||||||
"""
|
"""
|
||||||
self.check_can_perform_operation()
|
self.check_can_perform_operation()
|
||||||
|
if self.rear is None or self.front is None:
|
||||||
|
return
|
||||||
if self.front == self.rear:
|
if self.front == self.rear:
|
||||||
data = self.front.data
|
data = self.front.data
|
||||||
self.front.data = None
|
self.front.data = None
|
||||||
|
@ -133,15 +144,15 @@ class CircularQueueLinkedList:
|
||||||
raise Exception("Empty Queue")
|
raise Exception("Empty Queue")
|
||||||
|
|
||||||
def check_is_full(self) -> None:
|
def check_is_full(self) -> None:
|
||||||
if self.rear.next == self.front:
|
if self.rear and self.rear.next == self.front:
|
||||||
raise Exception("Full Queue")
|
raise Exception("Full Queue")
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.data = None
|
self.data: Any | None = None
|
||||||
self.next = None
|
self.next: Node | None = None
|
||||||
self.prev = None
|
self.prev: Node | None = None
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue
Block a user