Skip to content

[mypy] Fix type annotations in data_structures/queue/circular_queue_linked_list.py #5749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions data_structures/queue/circular_queue_linked_list.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -18,20 +20,26 @@ 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:
current_node = Node()

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

previous_node = current_node

previous_node.next = self.front
self.front.prev = previous_node

Expand All @@ -49,9 +57,14 @@ def is_empty(self) -> bool:
>>> 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()
Expand All @@ -74,7 +87,7 @@ def first(self) -> Any:
'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:
"""
Expand All @@ -92,11 +105,15 @@ def enqueue(self, data: Any) -> None:
...
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:
Expand All @@ -117,6 +134,10 @@ def dequeue(self) -> Any:
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
Expand All @@ -133,15 +154,15 @@ def check_can_perform_operation(self) -> None:
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__":
Expand Down