-
Notifications
You must be signed in to change notification settings - Fork 313
[WIP] Added Stacks Using Linked List #121
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8a9b9ae
Update CODE_OF_CONDUCT.md
sarthakforwet c0dee35
including updates Merge https://github.com/codezonediitj/pydatastructs
sarthakforwet 0d09164
Added Stacks Using Linked Lists
sarthakforwet 24aff34
Updated Linked_Stacks
sarthakforwet 59288b1
modified gitignore and removed build, dist
czgdp1807 9a1e200
Updated stack data structure
sarthakforwet 4c06b1c
Updates
sarthakforwet 279ae00
Updates
sarthakforwet c3305e4
Removed trailing whitespaces from stack.py
sarthakforwet 73fadc6
Updated stack.py
sarthakforwet 994ac71
Update stack.py
sarthakforwet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,8 @@ htmlcov | |
*~ | ||
__pycache__/ | ||
.pytest_cache/ | ||
build/ | ||
dist/ | ||
|
||
# Backup Files # | ||
################ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
build/lib/pydatastructs/miscellaneous_data_structures/linked_stacks.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from pydatastructs import SinglyLinkedList | ||
|
||
__all__ = ["Linked_Stacks"] | ||
class Linked_Stacks: | ||
|
||
# A class to implement Stacks Built over Singly Linked Lists | ||
|
||
def __init__(self): | ||
self.sll = SinglyLinkedList() | ||
self.top = self.sll.head | ||
|
||
def push(self,data): | ||
self.sll.append(data) | ||
if self.top is None: | ||
self.top = self.sll.head | ||
return self | ||
|
||
def pop(self): | ||
if self.top is not None: | ||
self.data = self.top.data | ||
self.top = self.top.next | ||
return self.data | ||
|
||
def peek(self): | ||
return self.top.data |
189 changes: 189 additions & 0 deletions
189
build/lib/pydatastructs/miscellaneous_data_structures/stack.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
from pydatastructs.linear_data_structures import DynamicOneDimensionalArray | ||
from pydatastructs.utils.misc_util import _check_type, NoneType | ||
from copy import deepcopy as dc | ||
from pydatastructs import DoublyLinkedList | ||
|
||
__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. | ||
|
||
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): | ||
if implementation == 'array': | ||
return ArrayStack( | ||
kwargs.get('items', None), | ||
kwargs.get('dtype', int)) | ||
|
||
elif implementation == "ll": | ||
return Linked_Stacks() | ||
|
||
raise NotImplementedError( | ||
"%s hasn't been implemented yet."%(implementation)) | ||
|
||
|
||
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): | ||
return None | ||
|
||
@property | ||
def peek(self): | ||
return None | ||
|
||
|
||
class ArrayStack(Stack): | ||
|
||
__slots__ = ['items'] | ||
|
||
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 = items | ||
return obj | ||
|
||
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 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.items._last_pos_filled == -1 | ||
|
||
@property | ||
def peek(self): | ||
return self.items[self.items._last_pos_filled] | ||
|
||
def __str__(self): | ||
""" | ||
Used for printing. | ||
""" | ||
return str(self.items._data) | ||
|
||
class Linked_Stacks(Stack): | ||
sarthakforwet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
"""Representation of Stack Data Structure using Doubly Linked List | ||
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. IMO, we can use |
||
Methods | ||
=========== | ||
push : | ||
A normal push operation to the Stack | ||
|
||
pop : | ||
Delete the top most element from the stack | ||
Returns the value of top element | ||
|
||
peek : | ||
returns the value of top element | ||
|
||
is_empty : | ||
Checks for whether a Stack is been empty or not | ||
Return True if empty else False | ||
|
||
__str__ : | ||
Used for Printing the Stack | ||
|
||
""" | ||
__slots__ = ["dll"] | ||
def __new__(cls): | ||
dll = DoublyLinkedList() | ||
obj = object.__new__(cls) | ||
obj.dll = dll | ||
obj.top = dll.head | ||
return obj | ||
|
||
|
||
def push(self,data): | ||
self.dll.append(data) | ||
if self.top is None: | ||
self.top = self.dll.head | ||
self.top = self.top.next | ||
|
||
def pop(self): | ||
if not self.is_empty: | ||
self.data = self.top.data | ||
self.top = self.top.prev | ||
return self.data | ||
else: | ||
raise ValueError("Stack is Empty") | ||
|
||
@property | ||
def is_empty(self): | ||
if self.top is None: | ||
return 1 | ||
return 0 | ||
return self.top is None | ||
|
||
|
||
@property | ||
def peek(self): | ||
if self.top is not None: | ||
return self.top.data | ||
raise ValueError("Stack is Empty") | ||
|
||
def __str__(self): | ||
"Used for Printing the Stack" | ||
iterator = self.top | ||
output = "" | ||
while iterator is not None: | ||
#print(iterator.data) | ||
output+=str(iterator.data)+"\n" | ||
iterator = iterator.prev | ||
output = output[:-1] | ||
return output |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Don't make changes to this file.
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.
Please address this.