Skip to content
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

LinkedListQueue Added #127

Merged
merged 5 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
52 changes: 49 additions & 3 deletions pydatastructs/miscellaneous_data_structures/queue.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydatastructs.linear_data_structures import DynamicOneDimensionalArray
from pydatastructs.utils.misc_util import NoneType
from pydatastructs.linear_data_structures import DynamicOneDimensionalArray, SinglyLinkedList
from pydatastructs.utils.misc_util import NoneType, LinkedListNode
from copy import deepcopy as dc

__all__ = [
Expand Down Expand Up @@ -51,6 +51,11 @@ def __new__(cls, implementation='array', **kwargs):
return ArrayQueue(
kwargs.get('items', None),
kwargs.get('dtype', int))
elif implementation == 'linkedlist':
return LinkedListQueue(
# kwargs.get('items', None),
# kwargs.get('dtype', LinkedListNode)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this commented out? Can we keep the API similar to the ArrayQueue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made LinkedListQueue analogous to ArrayQueue. Now they have similar API.

)
raise NotImplementedError(
"%s hasn't been implemented yet."%(implementation))

Expand All @@ -64,7 +69,9 @@ def popleft(self, *args, **kwargs):

@property
def is_empty(self):
return None
raise NotImplementedError(
"This is an abstract method.")

czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved

class ArrayQueue(Queue):

Expand Down Expand Up @@ -122,3 +129,42 @@ def __str__(self):
for i in range(self.front, self.rear + 1):
_data.append(self.items._data[i])
return str(_data)


class LinkedListQueue(Queue):

__slots__ = ['front', 'rear', 'size']

def __new__(cls):
obj = object.__new__(cls)
obj.queue = SinglyLinkedList()
obj.front = None
obj.rear = None
obj.size = 0
return obj

def append(self, x):
self.size += 1
self.queue.append(x)
if self.front is None:
self.front = self.queue.head
self.rear = self.queue.tail

def popleft(self):
if self.is_empty:
raise ValueError("Queue is empty.")
self.size -= 1
return_value = self.queue.pop_left()
self.front = self.queue.head
self.rear = self.queue.tail
return return_value

@property
def is_empty(self):
return self.size == 0

def __len__(self):
return self.size

def __str__(self):
return str(self.queue)
15 changes: 15 additions & 0 deletions pydatastructs/miscellaneous_data_structures/tests/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,18 @@ def test_Queue():

q1 = Queue()
raises(ValueError, lambda: q1.popleft())

q1 = Queue(implementation='linkedlist')
q1.append(0)
q1.append(1)
q1.append(2)
q1.append(3)
assert str(q1) == '[0, 1, 2, 3]'
assert len(q1) == 4
assert q1.popleft().data == 0
assert q1.popleft().data == 1
assert len(q1) == 2
assert q1.popleft().data == 2
assert q1.popleft().data == 3
assert len(q1) == 0
raises(ValueError, lambda: q1.popleft())