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

Added Queue data structure #85

Merged
merged 26 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5919602
added queue.py
iamrajiv Jan 16, 2020
a6b81bf
changes made to __init.py__
iamrajiv Jan 16, 2020
bfb8158
added tests for Queue.py
iamrajiv Jan 16, 2020
c277a02
added new line at end of __init.py__
iamrajiv Jan 16, 2020
87dfc60
added queue size
iamrajiv Jan 16, 2020
81a6fed
corrected tests
iamrajiv Jan 16, 2020
643a336
corrected tests
iamrajiv Jan 16, 2020
7fcd05d
corrected tests
iamrajiv Jan 16, 2020
378046b
corrected tests and queue.py
iamrajiv Jan 16, 2020
94f4c05
corrected tests and queue.py
iamrajiv Jan 16, 2020
466d66f
corrected tests and queue.py
iamrajiv Jan 16, 2020
3ca4ba9
corrected tests and queue.py
iamrajiv Jan 16, 2020
b6c7a0c
corrected tests and queue.py
iamrajiv Jan 16, 2020
2b11c96
corrected tests and queue.py
iamrajiv Jan 16, 2020
7a8262c
corrected tests and queue.py
iamrajiv Jan 16, 2020
f3a7c3d
implemented DynamicOneDimensionalArray of queue and improved code qua…
iamrajiv Jan 31, 2020
df81e1e
implemented DynamicOneDimensionalArray of queue and improved code qua…
iamrajiv Jan 31, 2020
8606d2b
remove unnecessary line
iamrajiv Jan 31, 2020
7418af0
improved tests
iamrajiv Feb 3, 2020
3845d67
removed init
iamrajiv Feb 3, 2020
7452720
added front and rear attributes
iamrajiv Feb 5, 2020
c143412
corrected the code syntax
iamrajiv Feb 5, 2020
80c1607
corrected the code syntax
iamrajiv Feb 5, 2020
9a0d81b
queue corrected, tests to be verified
czgdp1807 Feb 12, 2020
540c060
added tests, ready for merge
czgdp1807 Feb 14, 2020
0b20f54
updated authors
czgdp1807 Feb 14, 2020
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
5 changes: 5 additions & 0 deletions pydatastructs/miscellaneous_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@
Stack,
)
__all__.extend(stack.__all__)

from .queue import (
Queue,
)
__all__.extend(queue.__all__)
95 changes: 95 additions & 0 deletions pydatastructs/miscellaneous_data_structures/queue.py
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
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
========

>>> 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

6 changes: 3 additions & 3 deletions pydatastructs/miscellaneous_data_structures/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
NoneType = type(None)

class Stack(object):
"""Respresentation of stack data structure
"""Representation of stack data structure

Parameters
==========
Expand All @@ -29,8 +29,8 @@ class Stack(object):
type of OneDimensionalArray
For array implementation.

Example
=======
Examples
========

>>> from pydatastructs import Stack
>>> s = Stack()
Expand Down
12 changes: 12 additions & 0 deletions pydatastructs/miscellaneous_data_structures/tests/test_queue.py
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