-
-
Save JackMorris/9e997ea2e2f9f035fdd3 to your computer and use it in GitHub Desktop.
Stack implementation in Python (http://jackm.co.uk/posts/2014/04/12/evaluating-expressions-using-reverse-polish-notation/)
This file contains 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 Stack: | |
def __init__(self): | |
# Empty stack represented with an empty list. | |
self.stack = [] | |
def push(self, x): | |
# Append the item onto our list. | |
self.stack.append(x) | |
def pop(self): | |
# Pop the item off of our list. | |
return self.stack.pop() | |
def peek(self): | |
# As long as it isn't empty, return the last item in our list. | |
if len(self.stack) > 0: | |
return self.stack[-1] # Index -1 = last element. | |
else: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment