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
|
||||
# https://en.wikipedia.org/wiki/Circular_buffer
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
|
@ -18,8 +20,8 @@ class CircularQueueLinkedList:
|
|||
"""
|
||||
|
||||
def __init__(self, initial_capacity: int = 6) -> None:
|
||||
self.front = None
|
||||
self.rear = None
|
||||
self.front: Node | None = None
|
||||
self.rear: Node | None = None
|
||||
self.create_linked_list(initial_capacity)
|
||||
|
||||
def create_linked_list(self, initial_capacity: int) -> None:
|
||||
|
@ -27,7 +29,7 @@ class CircularQueueLinkedList:
|
|||
self.front = current_node
|
||||
self.rear = current_node
|
||||
previous_node = current_node
|
||||
for i in range(1, initial_capacity):
|
||||
for _ in range(1, initial_capacity):
|
||||
current_node = Node()
|
||||
previous_node.next = current_node
|
||||
current_node.prev = previous_node
|
||||
|
@ -49,9 +51,14 @@ class CircularQueueLinkedList:
|
|||
>>> cq.is_empty()
|
||||
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
|
||||
>>> cq = CircularQueueLinkedList()
|
||||
|
@ -74,7 +81,7 @@ class CircularQueueLinkedList:
|
|||
'b'
|
||||
"""
|
||||
self.check_can_perform_operation()
|
||||
return self.front.data
|
||||
return self.front.data if self.front else None
|
||||
|
||||
def enqueue(self, data: Any) -> None:
|
||||
"""
|
||||
|
@ -92,11 +99,13 @@ class CircularQueueLinkedList:
|
|||
...
|
||||
Exception: Empty Queue
|
||||
"""
|
||||
if self.rear is None:
|
||||
return
|
||||
|
||||
self.check_is_full()
|
||||
if self.is_empty():
|
||||
self.rear.data = data
|
||||
else:
|
||||
if not self.is_empty():
|
||||
self.rear = self.rear.next
|
||||
if self.rear:
|
||||
self.rear.data = data
|
||||
|
||||
def dequeue(self) -> Any:
|
||||
|
@ -117,6 +126,8 @@ class CircularQueueLinkedList:
|
|||
Exception: Empty Queue
|
||||
"""
|
||||
self.check_can_perform_operation()
|
||||
if self.rear is None or self.front is None:
|
||||
return
|
||||
if self.front == self.rear:
|
||||
data = self.front.data
|
||||
self.front.data = None
|
||||
|
@ -133,15 +144,15 @@ class CircularQueueLinkedList:
|
|||
raise Exception("Empty Queue")
|
||||
|
||||
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")
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self) -> None:
|
||||
self.data = None
|
||||
self.next = None
|
||||
self.prev = None
|
||||
self.data: Any | None = None
|
||||
self.next: Node | None = None
|
||||
self.prev: Node | None = None
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Reference in New Issue
Block a user