-
Notifications
You must be signed in to change notification settings - Fork 312
Added Queue data structure #85
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 19 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
5919602
added queue.py
iamrajiv a6b81bf
changes made to __init.py__
iamrajiv bfb8158
added tests for Queue.py
iamrajiv c277a02
added new line at end of __init.py__
iamrajiv 87dfc60
added queue size
iamrajiv 81a6fed
corrected tests
iamrajiv 643a336
corrected tests
iamrajiv 7fcd05d
corrected tests
iamrajiv 378046b
corrected tests and queue.py
iamrajiv 94f4c05
corrected tests and queue.py
iamrajiv 466d66f
corrected tests and queue.py
iamrajiv 3ca4ba9
corrected tests and queue.py
iamrajiv b6c7a0c
corrected tests and queue.py
iamrajiv 2b11c96
corrected tests and queue.py
iamrajiv 7a8262c
corrected tests and queue.py
iamrajiv f3a7c3d
implemented DynamicOneDimensionalArray of queue and improved code qua…
iamrajiv df81e1e
implemented DynamicOneDimensionalArray of queue and improved code qua…
iamrajiv 8606d2b
remove unnecessary line
iamrajiv 7418af0
improved tests
iamrajiv 3845d67
removed init
iamrajiv 7452720
added front and rear attributes
iamrajiv c143412
corrected the code syntax
iamrajiv 80c1607
corrected the code syntax
iamrajiv 9a0d81b
queue corrected, tests to be verified
czgdp1807 540c060
added tests, ready for merge
czgdp1807 0b20f54
updated authors
czgdp1807 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 |
---|---|---|
|
@@ -14,3 +14,8 @@ | |
Stack, | ||
) | ||
__all__.extend(stack.__all__) | ||
|
||
from .queue import ( | ||
Queue, | ||
) | ||
__all__.extend(queue.__all__) |
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,95 @@ | ||
from pydatastructs.linear_data_structures import DynamicOneDimensionalArray | ||
|
||
__all__ = [ | ||
'Queue' | ||
] | ||
|
||
class Queue(object): | ||
"""Representation of queue data structure | ||
|
||
Examples | ||
======== | ||
|
||
>>> from pydatastructs import Queue | ||
>>> q = Queue() | ||
>>> q.append(1) | ||
>>> q.append(2) | ||
>>> q.append(3) | ||
>>> q.popleft() | ||
>>> q.len() | ||
1 | ||
|
||
References | ||
========== | ||
|
||
.. [1] https://en.wikipedia.org/wiki/Queue_(abstract_data_type) | ||
""" | ||
|
||
def __new__(cls, implementation='array', **kwargs): | ||
if implementation == 'array': | ||
return ArrayQueue( | ||
kwargs.get('items', None), | ||
kwargs.get('dtype', int)) | ||
raise NotImplementedError( | ||
"%s hasn't been implemented yet."%(implementation)) | ||
|
||
def append(self, *args, **kwargs): | ||
raise NotImplementedError( | ||
"This is an abstract method.") | ||
|
||
def popleft(self, *args, **kwargs): | ||
raise NotImplementedError( | ||
"This is an abstract method.") | ||
|
||
@property | ||
def is_empty(self): | ||
return self.front == -1 | ||
|
||
@property | ||
def is_full(self): | ||
return ((self.rear + 1) % self.size == self.front) | ||
|
||
class ArrayQueue(Queue): | ||
|
||
__slots__ = ['items', 'dtype'] | ||
|
||
def __new__(cls, items=None, dtype=int): | ||
if items is None: | ||
items = DynamicOneDimensionalArray(dtype, 0) | ||
else: | ||
items = DynamicOneDimensionalArray(dtype, items) | ||
obj = object.__new__(cls) | ||
obj.items, obj.dtype = \ | ||
items, items._dtype | ||
return obj | ||
|
||
def __init__(self): | ||
self.items.size = size | ||
self.items = [None for i in range(size)] | ||
self.items.front = self.items.rear = -1 | ||
czgdp1807 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def append(self, x): | ||
if self.is_full: | ||
raise ValueError("Queue is full") | ||
elif (self.items.front == -1): | ||
self.items.front = 0 | ||
iamrajiv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.items.rear = 0 | ||
self.items[self.items.rear] = x | ||
else: | ||
self.items.rear = (self.items.rear + 1) % self.size | ||
self.items[self.items.rear] = x | ||
|
||
def popleft(self): | ||
if self.is_empty: | ||
raise ValueError("Queue is empty") | ||
elif (self.items.front == self.items.rear): | ||
self.items.front = -1 | ||
self.items.rear = -1 | ||
return self.items[self.items.front] | ||
else: | ||
self.items.front = (self.items.front + 1) % self.size | ||
return self.items[self.items.front] | ||
|
||
def len(self): | ||
return abs(abs(self.size - self.items.front) - abs(self.size - self.items.rear))+1 | ||
|
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
12 changes: 12 additions & 0 deletions
12
pydatastructs/miscellaneous_data_structures/tests/test_queue.py
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,12 @@ | ||
from pydatastructs.miscellaneous_data_structures import Queue | ||
from pydatastructs.utils.raises_util import raises | ||
|
||
def test_Queue(): | ||
|
||
q = Queue() | ||
q.append(1) | ||
q.append(2) | ||
q.append(3) | ||
assert q.popleft() == 1 | ||
assert q.popleft() == 2 | ||
assert q.len() == 1 |
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.