-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Implement Circular Queue using linked lists. Fixes TheAlgorithms#5361 #5587
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 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1917dcc
CircularQueueLinkedList: empty list, trivial implementation TheAlgori…
bit-man daec873
CircularQueueLinkedList: single element list TheAlgorithms#5361
bit-man 6259697
CircularQueueLinkedList: refactor, no que empty attribute TheAlgorith…
bit-man 7fb8973
CircularQueueLinkedList: refactor TheAlgorithms#5361
bit-man 8f76f46
CircularQueueLinkedList: changed internal data structure to use doubl…
bit-man b3abd07
CircularQueueLinkedList: enqueue test cases added TheAlgorithms#5361
bit-man ae12cab
CircularQueueLinkedList: track full queue TheAlgorithms#5361
bit-man 77f3237
CircularQueueLinkedList: adding functions description TheAlgorithms#5361
bit-man 46e0d0b
CircularQueueLinkedList: type hints TheAlgorithms#5361
bit-man 2a07f99
CircularQueueLinkedList: algorithm explanation TheAlgorithms#5361
bit-man 4d8d8c8
CircularQueueLinkedList: missing type hints TheAlgorithms#5361
bit-man 0cd8d02
CircularQueueLinkedList: more missing type hints TheAlgorithms#5361
bit-man 39ae28b
Update data_structures/queue/circular_queue_linked_list.py
poyea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# Implementation of Circular Queue using linked lists\ | ||
# https://en.wikipedia.org/wiki/Circular_buffer | ||
|
||
from typing import Any | ||
|
||
|
||
class CircularQueueLinkedList: | ||
""" | ||
Circular FIFO list with the given capacity (default queue length : 6) | ||
|
||
>>> cq = CircularQueueLinkedList(2) | ||
>>> cq.enqueue('a') | ||
>>> cq.enqueue('b') | ||
>>> cq.enqueue('c') | ||
Traceback (most recent call last): | ||
... | ||
Exception: Full Queue | ||
""" | ||
|
||
def __init__(self, initial_capacity: int = 6): | ||
bit-man marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.front = None | ||
self.rear = None | ||
self.create_linked_list(initial_capacity) | ||
|
||
def create_linked_list(self, initial_capacity): | ||
bit-man marked this conversation as resolved.
Show resolved
Hide resolved
|
||
current_node = Node() | ||
self.front = current_node | ||
self.rear = current_node | ||
previous_node = current_node | ||
for i 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 | ||
|
||
def is_empty(self) -> bool: | ||
""" | ||
Checks where the queue is empty or not | ||
>>> cq = CircularQueueLinkedList() | ||
>>> cq.is_empty() | ||
True | ||
>>> cq.enqueue('a') | ||
>>> cq.is_empty() | ||
False | ||
>>> cq.dequeue() | ||
'a' | ||
>>> cq.is_empty() | ||
True | ||
""" | ||
return self.front == self.rear and self.front.data is None | ||
|
||
def first(self) -> Any: | ||
""" | ||
Returns the first element of the queue | ||
>>> cq = CircularQueueLinkedList() | ||
>>> cq.first() | ||
Traceback (most recent call last): | ||
... | ||
Exception: Empty Queue | ||
>>> cq.enqueue('a') | ||
>>> cq.first() | ||
'a' | ||
>>> cq.dequeue() | ||
'a' | ||
>>> cq.first() | ||
Traceback (most recent call last): | ||
... | ||
Exception: Empty Queue | ||
>>> cq.enqueue('b') | ||
>>> cq.enqueue('c') | ||
>>> cq.first() | ||
'b' | ||
""" | ||
self.check_can_perform_operation() | ||
return self.front.data | ||
|
||
def enqueue(self, data: Any): | ||
bit-man marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Saves data at the end of the queue | ||
|
||
>>> cq = CircularQueueLinkedList() | ||
>>> cq.enqueue('a') | ||
>>> cq.enqueue('b') | ||
>>> cq.dequeue() | ||
'a' | ||
>>> cq.dequeue() | ||
'b' | ||
>>> cq.dequeue() | ||
Traceback (most recent call last): | ||
... | ||
Exception: Empty Queue | ||
""" | ||
self.check_is_full() | ||
if self.is_empty(): | ||
self.rear.data = data | ||
else: | ||
self.rear = self.rear.next | ||
self.rear.data = data | ||
|
||
def dequeue(self) -> Any: | ||
""" | ||
Removes and retrieves the first element of the queue | ||
|
||
>>> cq = CircularQueueLinkedList() | ||
>>> cq.dequeue() | ||
Traceback (most recent call last): | ||
... | ||
Exception: Empty Queue | ||
>>> cq.enqueue('a') | ||
>>> cq.dequeue() | ||
'a' | ||
>>> cq.dequeue() | ||
Traceback (most recent call last): | ||
... | ||
Exception: Empty Queue | ||
""" | ||
self.check_can_perform_operation() | ||
if self.front == self.rear: | ||
data = self.front.data | ||
self.front.data = None | ||
return data | ||
|
||
old_front = self.front | ||
self.front = old_front.next | ||
data = old_front.data | ||
old_front.data = None | ||
return data | ||
|
||
def check_can_perform_operation(self): | ||
bit-man marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if self.is_empty(): | ||
raise Exception("Empty Queue") | ||
|
||
def check_is_full(self): | ||
bit-man marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if self.rear.next == self.front: | ||
raise Exception("Full Queue") | ||
|
||
|
||
class Node: | ||
def __init__(self): | ||
bit-man marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.data = None | ||
self.next = None | ||
self.prev = None | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.