Skip to content
11 changes: 9 additions & 2 deletions pydatastructs/miscellaneous_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from . import (
stack,
binomial_trees
binomial_trees,
queue

)

from .binomial_trees import (
Expand All @@ -11,6 +13,11 @@
__all__.extend(binomial_trees.__all__)

from .stack import (
Stack,
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)