-
Notifications
You must be signed in to change notification settings - Fork 272
/
stack.py
200 lines (161 loc) · 5.1 KB
/
stack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from pydatastructs.linear_data_structures import DynamicOneDimensionalArray, SinglyLinkedList
from pydatastructs.miscellaneous_data_structures._backend.cpp import _stack
from pydatastructs.utils.misc_util import (
_check_type, NoneType, Backend,
raise_if_backend_is_not_python)
from copy import deepcopy as dc
__all__ = [
'Stack'
]
class Stack(object):
"""Representation of stack data structure
Parameters
==========
implementation : str
Implementation to be used for stack.
By default, 'array'
Currently only supports 'array'
implementation.
items : list/tuple
Optional, by default, None
The inital items in the stack.
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.
backend: pydatastructs.Backend
The backend to be used.
Optional, by default, the best available
backend is used.
Examples
========
>>> from pydatastructs import Stack
>>> s = Stack()
>>> s.push(1)
>>> s.push(2)
>>> s.push(3)
>>> str(s)
'[1, 2, 3]'
>>> s.pop()
3
References
==========
.. [1] https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
"""
def __new__(cls, implementation='array', **kwargs):
backend = kwargs.get('backend', Backend.PYTHON)
if implementation == 'array':
items = kwargs.get('items', None)
dtype = kwargs.get('dtype', int)
if backend == Backend.CPP:
return _stack.ArrayStack(items, dtype)
return ArrayStack(items, dtype)
if implementation == 'linked_list':
raise_if_backend_is_not_python(cls, backend)
return LinkedListStack(
kwargs.get('items', None)
)
raise NotImplementedError(
"%s hasn't been implemented yet."%(implementation))
@classmethod
def methods(cls):
return ['__new__']
def push(self, *args, **kwargs):
raise NotImplementedError(
"This is an abstract method.")
def pop(self, *args, **kwargs):
raise NotImplementedError(
"This is an abstract method.")
@property
def is_empty(self):
raise NotImplementedError(
"This is an abstract method.")
@property
def peek(self):
raise NotImplementedError(
"This is an abstract method.")
class ArrayStack(Stack):
__slots__ = ['items']
def __new__(cls, items=None, dtype=NoneType,
**kwargs):
raise_if_backend_is_not_python(
cls, kwargs.get('backend', Backend.PYTHON))
if items is None:
items = DynamicOneDimensionalArray(dtype, 0)
else:
items = DynamicOneDimensionalArray(dtype, items)
obj = object.__new__(cls)
obj.items = items
return obj
@classmethod
def methods(cls):
return ['__new__', 'push', 'pop', 'is_emtpy',
'peek', '__len__', '__str__']
def push(self, x):
if self.is_empty:
self.items._dtype = type(x)
self.items.append(x)
def pop(self):
if self.is_empty:
raise IndexError("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.items._last_pos_filled == -1
@property
def peek(self):
return self.items[self.items._last_pos_filled]
def __len__(self):
return self.items._num
def __str__(self):
"""
Used for printing.
"""
return str(self.items._data)
class LinkedListStack(Stack):
__slots__ = ['stack']
def __new__(cls, items=None, **kwargs):
raise_if_backend_is_not_python(
cls, kwargs.get('backend', Backend.PYTHON))
obj = object.__new__(cls)
obj.stack = SinglyLinkedList()
if items is None:
pass
elif type(items) in (list, tuple):
for x in items:
obj.push(x)
else:
raise TypeError("Expected type: list/tuple")
return obj
@classmethod
def methods(cls):
return ['__new__', 'push', 'pop', 'is_emtpy',
'peek', '__len__', '__str__']
def push(self, x):
self.stack.appendleft(x)
def pop(self):
if self.is_empty:
raise IndexError("Stack is empty")
return self.stack.popleft()
@property
def is_empty(self):
return self.__len__() == 0
@property
def peek(self):
return self.stack.head
@property
def size(self):
return self.stack.size
def __len__(self):
return self.stack.size
def __str__(self):
elements = []
current_node = self.peek
while current_node is not None:
elements.append(str(current_node))
current_node = current_node.next
return str(elements[::-1])