-
Notifications
You must be signed in to change notification settings - Fork 312
Linked list stack implemented #139
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
Changes from all commits
1abbf2c
688ee21
3a755fa
a6265e9
5c1c6ac
192b0d6
8801c9e
1718d7a
7ed4cb1
cd18a9a
d9cbefd
41b20f0
7d87af0
d7c25a7
4b95f40
65d005a
0fb608c
cb8e651
7dc4c88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,3 +64,4 @@ __pycache__/ | |
.idea/ | ||
build/ | ||
dist/ | ||
venv/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from pydatastructs.linear_data_structures import DynamicOneDimensionalArray | ||
from pydatastructs.utils.misc_util import _check_type, NoneType | ||
from pydatastructs.linear_data_structures import DynamicOneDimensionalArray, SinglyLinkedList | ||
from pydatastructs.utils.misc_util import _check_type, NoneType, LinkedListNode | ||
from copy import deepcopy as dc | ||
|
||
__all__ = [ | ||
|
@@ -51,6 +51,10 @@ def __new__(cls, implementation='array', **kwargs): | |
return ArrayStack( | ||
kwargs.get('items', None), | ||
kwargs.get('dtype', int)) | ||
elif implementation == 'linkedlist': | ||
return LinkedListStack( | ||
kwargs.get('items', None), | ||
kwargs.get('dtype', int)) | ||
raise NotImplementedError( | ||
"%s hasn't been implemented yet."%(implementation)) | ||
|
||
|
@@ -109,3 +113,59 @@ def __str__(self): | |
Used for printing. | ||
""" | ||
return str(self.items._data) | ||
|
||
|
||
class LinkedListStack(Stack): | ||
|
||
def __new__(cls, items=None, dtype=NoneType): | ||
obj = object.__new__(cls) | ||
obj.stack = SinglyLinkedList() | ||
obj._dtype = dtype | ||
obj.size = 0 | ||
if items is None: | ||
pass | ||
elif type(items) in (list, tuple): | ||
if len(items) != 0 and dtype is NoneType: | ||
obj._dtype = type(items[0]) | ||
for x in items: | ||
if type(x) == obj._dtype: | ||
obj.stack.append_left(x) | ||
obj.size+=1 | ||
else: | ||
raise TypeError("Expected %s but got %s"%(obj._dtype, type(x))) | ||
Comment on lines
+134
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
else: | ||
raise TypeError("Expected type: list/tuple") | ||
Comment on lines
+136
to
+137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do input filtering at the entry point of a function/method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved refactored the new method, check it its good enough |
||
obj.top = obj.stack.head | ||
return obj | ||
|
||
def push(self, x): | ||
if self._dtype is NoneType: | ||
self._dtype = type(x) | ||
elif type(x) is not self._dtype: | ||
raise TypeError("Expected %s but got %s"%(self._dtype, type(x))) | ||
self.size += 1 | ||
self.stack.append_left(x) | ||
if self.top is None: | ||
self.top = self.stack.head | ||
|
||
def pop(self): | ||
if self.is_empty: | ||
raise ValueError("Stack is empty") | ||
self.size -= 1 | ||
return_value = self.stack.pop_left() | ||
self.top = self.stack.head | ||
return return_value | ||
|
||
@property | ||
def is_empty(self): | ||
return self.size == 0 | ||
Comment on lines
+160
to
+161
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will lose consistency with LinkedListQueue if i change it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure will solve it in different PR. |
||
|
||
@property | ||
def peek(self): | ||
return self.top | ||
|
||
def __len__(self): | ||
return self.size | ||
|
||
def __str__(self): | ||
return str(self.stack) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to use
_check_type
frommisc_utils.py
here?