Skip to content

Instantly share code, notes, and snippets.

@JackMorris
Created April 12, 2014 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackMorris/9e997ea2e2f9f035fdd3 to your computer and use it in GitHub Desktop.
Save JackMorris/9e997ea2e2f9f035fdd3 to your computer and use it in GitHub Desktop.
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