Last active
August 29, 2015 14:15
-
-
Save markhamilton1/1cc0499877751af509a7 to your computer and use it in GitHub Desktop.
A simple implementation of a stack in python
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
class Element(object): | |
"""Used to hold a data item on the Stack.""" | |
def __init__(self, data, next): | |
"""Construct a new Element for the Stack. | |
data: the data to push onto the Stack | |
next: the next Element on the stack | |
""" | |
self.data = data | |
self.next = next | |
class AlgoStack(object): | |
"""Implements a Stack.""" | |
def __init__(self): | |
"""Construct a new Stack.""" | |
self._top = None | |
self._cnt = 0 | |
def clear(self): | |
"""Clear the contents of the Stack.""" | |
self._top = None | |
self._cnt = 0 | |
def count(self): | |
"""Get the number of items on the Stack.""" | |
return self._cnt | |
def is_empty(self): | |
"""Test if the Stack is empty. | |
return: True=Stack is empty, False=Stack has Elements | |
""" | |
return self._top is None | |
def pop(self): | |
"""Pop the top Element off the Stack and return the data that it contains. | |
return: the data from the top Element (None if no top Element) | |
""" | |
d = None | |
if self._top is not None: | |
d = self._top.data | |
self._top = self._top.next | |
self._cnt -= 1 | |
return d | |
def push(self, data=None): | |
"""Push the provided data onto the Stack. | |
data: the data to push onto the Stack | |
""" | |
t = self._top | |
self._top = Element(data, t) | |
self._cnt += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment