diff --git a/AUTHORS b/AUTHORS index 8af2532c4..d8aba096f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,6 +1,7 @@ -Gagandeep Singh -Kartikei Mittal -Umesh<23umesh.here@gmail.com> -Rohan Singh -Tarun Singh Tomar -Saptashrungi Birajdar +Gagandeep Singh +Kartikei Mittal +Umesh <23umesh.here@gmail.com> +Rohan Singh +Tarun Singh Tomar +Saptashrungi Birajdar +Rajiv Ranjan Singh diff --git a/pydatastructs/miscellaneous_data_structures/__init__.py b/pydatastructs/miscellaneous_data_structures/__init__.py index 426fde9df..b77cd4d4e 100644 --- a/pydatastructs/miscellaneous_data_structures/__init__.py +++ b/pydatastructs/miscellaneous_data_structures/__init__.py @@ -14,3 +14,8 @@ Stack, ) __all__.extend(stack.__all__) + +from .queue import ( + Queue, +) +__all__.extend(queue.__all__) diff --git a/pydatastructs/miscellaneous_data_structures/queue.py b/pydatastructs/miscellaneous_data_structures/queue.py new file mode 100644 index 000000000..16502a571 --- /dev/null +++ b/pydatastructs/miscellaneous_data_structures/queue.py @@ -0,0 +1,124 @@ +from pydatastructs.linear_data_structures import DynamicOneDimensionalArray +from pydatastructs.utils.misc_util import NoneType +from copy import deepcopy as dc + +__all__ = [ + 'Queue' +] + +class Queue(object): + """Representation of queue data structure. + + Parameters + ========== + + implementation : str + Implementation to be used for queue. + By default, 'array' + Currently only supports 'array' + implementation. + items : list/tuple + Optional, by default, None + The inital items in the queue. + For array implementation. + dtype : A valid python type + Optional, by default NoneType if item + is None, otherwise takes the data + type of DynamicOneDimensionalArray + For array implementation. + + Examples + ======== + + >>> from pydatastructs import Queue + >>> q = Queue() + >>> q.append(1) + >>> q.append(2) + >>> q.append(3) + >>> q.popleft() + 1 + >>> len(q) + 2 + + 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 None + +class ArrayQueue(Queue): + + __slots__ = ['front'] + + def __new__(cls, items=None, dtype=NoneType): + if items is None: + items = DynamicOneDimensionalArray(dtype, 0) + else: + dtype = type(items[0]) + items = DynamicOneDimensionalArray(dtype, items) + obj = object.__new__(cls) + obj.items, obj.front = items, -1 + if items.size == 0: + obj.front = -1 + else: + obj.front = 0 + return obj + + def append(self, x): + if self.is_empty: + self.front = 0 + self.items._dtype = type(x) + self.items.append(x) + + def popleft(self): + if self.is_empty: + raise ValueError("Queue is empty.") + return_value = dc(self.items[self.front]) + front_temp = self.front + if self.front == self.rear: + self.front = -1 + else: + if (self.items._num - 1)/self.items._size < \ + self.items._load_factor: + self.front = 0 + else: + self.front += 1 + self.items.delete(front_temp) + return return_value + + @property + def rear(self): + return self.items._last_pos_filled + + @property + def is_empty(self): + return self.__len__() == 0 + + def __len__(self): + return self.items._num + + def __str__(self): + _data = [] + for i in range(self.front, self.rear + 1): + _data.append(self.items._data[i]) + return str(_data) diff --git a/pydatastructs/miscellaneous_data_structures/stack.py b/pydatastructs/miscellaneous_data_structures/stack.py index 74f3b7ed4..dc44e3895 100644 --- a/pydatastructs/miscellaneous_data_structures/stack.py +++ b/pydatastructs/miscellaneous_data_structures/stack.py @@ -1,15 +1,13 @@ from pydatastructs.linear_data_structures import DynamicOneDimensionalArray +from pydatastructs.utils.misc_util import _check_type, NoneType from copy import deepcopy as dc __all__ = [ 'Stack' ] -_check_type = lambda a, t: isinstance(a, t) -NoneType = type(None) - class Stack(object): - """Respresentation of stack data structure + """Representation of stack data structure Parameters ========== @@ -24,13 +22,13 @@ class Stack(object): The inital items in the stack. For array implementation. dtype : A valid python type - Optional, by default int if item + Optional, by default NoneType if item is None, otherwise takes the data - type of OneDimensionalArray + type of DynamicOneDimensionalArray For array implementation. - Example - ======= + Examples + ======== >>> from pydatastructs import Stack >>> s = Stack() @@ -74,19 +72,20 @@ def peek(self): class ArrayStack(Stack): - __slots__ = ['items', 'dtype'] + __slots__ = ['items'] - def __new__(cls, items=None, dtype=int): + def __new__(cls, items=None, dtype=NoneType): if items is None: items = DynamicOneDimensionalArray(dtype, 0) else: items = DynamicOneDimensionalArray(dtype, items) obj = object.__new__(cls) - obj.items, obj.dtype = \ - items, items._dtype + obj.items = items return obj def push(self, x): + if self.is_empty: + self.items._dtype = type(x) self.items.append(x) def pop(self): diff --git a/pydatastructs/miscellaneous_data_structures/tests/test_queue.py b/pydatastructs/miscellaneous_data_structures/tests/test_queue.py new file mode 100644 index 000000000..18f8030f0 --- /dev/null +++ b/pydatastructs/miscellaneous_data_structures/tests/test_queue.py @@ -0,0 +1,20 @@ +from pydatastructs.miscellaneous_data_structures import Queue +from pydatastructs.utils.raises_util import raises + +def test_Queue(): + + q1 = Queue(implementation='array', items=[0]) + q1.append(1) + q1.append(2) + q1.append(3) + assert str(q1) == '[0, 1, 2, 3]' + assert len(q1) == 4 + assert q1.popleft() == 0 + assert q1.popleft() == 1 + assert len(q1) == 2 + assert q1.popleft() == 2 + assert q1.popleft() == 3 + assert len(q1) == 0 + + q1 = Queue() + raises(ValueError, lambda: q1.popleft())