diff --git a/pydatastructs/miscellaneous_data_structures/stack.py b/pydatastructs/miscellaneous_data_structures/stack.py index 6c4f8c5d4..6899eb774 100644 --- a/pydatastructs/miscellaneous_data_structures/stack.py +++ b/pydatastructs/miscellaneous_data_structures/stack.py @@ -1,4 +1,4 @@ -from pydatastructs.linear_data_structures import OneDimensionalArray +from pydatastructs.linear_data_structures import DynamicOneDimensionalArray from copy import deepcopy as dc __all__ = [ @@ -19,13 +19,7 @@ class Stack(object): By default, 'array' Currently only supports 'array' implementation. - maxsize : int - The maximum size of the stack. - For array implementation. - top : int - The top element of the stack. - For array implementation. - items : OneDimensionalArray + items : DynamicOneDimensionalArray Optional, by default, None The inital items in the stack. For array implementation. @@ -39,12 +33,12 @@ class Stack(object): ======= >>> from pydatastructs import Stack - >>> s = Stack(maxsize=5, top=0) + >>> s = Stack() >>> s.push(1) >>> s.push(2) >>> s.push(3) >>> str(s) - '[1, 2, 3, None, None]' + '[1, 2, 3]' >>> s.pop() 3 @@ -57,8 +51,6 @@ class Stack(object): def __new__(cls, implementation='array', **kwargs): if implementation == 'array': return ArrayStack( - kwargs.get('maxsize', None), - kwargs.get('top', 0), kwargs.get('items', None), kwargs.get('dtype', int)) raise NotImplementedError( @@ -82,46 +74,36 @@ def peek(self): class ArrayStack(Stack): - __slots__ = ['maxsize', 'top', 'items', 'dtype'] + __slots__ = ['items', 'dtype'] - def __new__(cls, maxsize=None, top=0, items=None, dtype=int): - if not _check_type(maxsize, int): - raise ValueError("maxsize is missing.") - if not _check_type(top, int): - raise TypeError("top is not of type int.") + def __new__(cls, items=None, dtype=int): if items is None: - items = OneDimensionalArray(dtype, maxsize) - if not _check_type(items, OneDimensionalArray): - raise ValueError("items is not of type, OneDimensionalArray") - if items._size > maxsize: - raise ValueError("Overflow, size of items %s is greater " - "than maxsize, %s"%(items._size, maxsize)) + items = DynamicOneDimensionalArray(dtype, 0) + else: + items = DynamicOneDimensionalArray(dtype, items) obj = object.__new__(cls) - obj.maxsize, obj.top, obj.items, obj.dtype = \ - maxsize, top, items, items._dtype + obj.items, obj.dtype = \ + items, items._dtype return obj def push(self, x): - if self.top == self.maxsize: - raise ValueError("Stack is full.") - self.items[self.top] = self.dtype(x) - self.top += 1 + self.items.append(x) def pop(self): - if self.top == 0: - raise ValueError("Stack is already empty.") - self.top -= 1 - r = self.items[self.top] - self.items[self.top] = None - return r + if self.is_empty: + raise ValueError("Stack is empty") + + top_element = dc(self.items[self.items._last_pos_filled]) + self.items.delete(self.items._last_pos_filled) + return top_element @property def is_empty(self): - return self.top == 0 + return self.items._last_pos_filled == -1 @property def peek(self): - return self.items[self.top - 1] + return self.items[self.items._last_pos_filled] def __str__(self): """ diff --git a/pydatastructs/miscellaneous_data_structures/tests/test_stack.py b/pydatastructs/miscellaneous_data_structures/tests/test_stack.py index 2522089be..d8ee561ec 100644 --- a/pydatastructs/miscellaneous_data_structures/tests/test_stack.py +++ b/pydatastructs/miscellaneous_data_structures/tests/test_stack.py @@ -4,22 +4,17 @@ def test_Stack(): - s = Stack(maxsize=3, top=0) + s = Stack() s.push(1) s.push(2) s.push(3) - assert s.top == 3 + assert s.peek == 3 assert str(s) == '[1, 2, 3]' - assert raises(ValueError, lambda: s.push(4)) assert s.pop() == 3 assert s.pop() == 2 assert s.pop() == 1 - assert s.top == 0 - assert raises(ValueError, lambda: s.pop()) - assert raises(ValueError, lambda: Stack()) - assert raises(TypeError, lambda: Stack(maxsize=8, top=3.5)) - assert raises(ValueError, lambda: Stack(maxsize=5, top=0, items=[1, 2, 3])) - assert raises(ValueError, lambda: Stack(maxsize=5, top=0, - items=OneDimensionalArray(int, 6))) - assert raises(NotImplementedError, lambda: Stack(implementation='', - maxsize=5, top=0)) + assert s.is_empty is True + assert raises(ValueError, lambda : s.pop()) + _s = Stack(items=[1, 2, 3]) + assert str(_s) == '[1, 2, 3]' + assert raises(NotImplementedError, lambda: Stack(implementation='')) diff --git a/pydatastructs/trees/binary_trees.py b/pydatastructs/trees/binary_trees.py index 3191d6a86..91fb96528 100644 --- a/pydatastructs/trees/binary_trees.py +++ b/pydatastructs/trees/binary_trees.py @@ -653,7 +653,7 @@ def _pre_order(self, node): """ visit = [] tree, size = self.tree.tree, self.tree.size - s = Stack(maxsize=size) + s = Stack() s.push(node) while not s.is_empty: node = s.pop() @@ -671,7 +671,7 @@ def _in_order(self, node): """ visit = [] tree, size = self.tree.tree, self.tree.size - s = Stack(maxsize=size) + s = Stack() while not s.is_empty or node is not None: if node is not None: s.push(node) @@ -689,7 +689,7 @@ def _post_order(self, node): """ visit = [] tree, size = self.tree.tree, self.tree.size - s = Stack(maxsize=size) + s = Stack() s.push(node) last = OneDimensionalArray(int, size) last.fill(False)