Skip to content

Instantly share code, notes, and snippets.

@JackMorris JackMorris/stack.py Secret
Created Apr 12, 2014

Embed
What would you like to do?
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
You can’t perform that action at this time.