Skip to content

Updated __init__.py as for queue requirement #70

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

Closed
wants to merge 14 commits into from
9 changes: 7 additions & 2 deletions pydatastructs/miscellaneous_data_structures/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
__all__ = []

from . import (
stack,
binomial_trees
queue

)

from .binomial_trees import (
Expand All @@ -14,3 +14,8 @@
Stack,
)
__all__.extend(stack.__all__)

from .queue import (
Queue,
)
__all__.extend(queue.__all__)
66 changes: 66 additions & 0 deletions pydatastructs/miscellaneous_data_structures/queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from pydatastructs.linear_data_structures import DynamicOneDimensionalArray

__all__ = [
'Queue'
]

_check_type = lambda a, t: isinstance(a, t)
NoneType = type(None)

class Queue(objects):
def __new__(clas, implementation='array', **kwargs):
if implementation == 'array':
return ArrayQueue(
kwargs.get('item', 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, **kwargs):
raise NotImplementedError("This is an abstract method.")

def __len__(self, **kwargs):
raise NotImplementedError("This is an abstract method.")


class ArrayQueue(Queue):
__slots__ = ['item','dtype']
front= -1
count= 0
def __new__(clas, item = None, dtype = int):
if item is None:
item= DynamicOneDimensionalArray(dtype, 0)
else:
item= DynamicOneDimensionalArray(dtype, item)

obj = object.__new__(clas)
obj.item, obj.dtype = item, items._dtype
return obj

def append(self, x):
if self.front == -1:
self.front=0
self.item.append(x)
self.count+=1

def popleft(self):
if (self.front == -1):
raise ValueError("Queue is empty.")
r = dc(self.item[self.front])
self.item.delete(self.front)
self.front += 1
return r

def __len__(self):
if (self.front == -1):
return 0
else:
return (self.count- self.front)

def __str__(self):
"""
Used for printing.
"""
return str(self.item._data)