class Stack:
# Python list implementation of stacks
def __init__(self):
# construct an empty stack
self._stack = []
def __str__(self):
# string representation of stack
return str(self._stack)
def __len__(self):
# returns number of items in stack
return len(self._stack)
def is_empty(self):
# returns True if stack is empty and False otherwise
return len(self._stack) == 0
def top(self):
# returns top item of stack if stack is not empty; otherwise returns None
if len(self._stack) == 0:
return None
else:
#return self._stack[len(self._stack)-1]
return self._stack[-1]
def pop(self):
# similar to top, except this also deletes top item from stack.
if len(self._stack) == 0:
return None
else:
x = self._stack[-1]
self._stack = self._stack[0:-1]
# self._stack.remove(????)
return x
def push(self,e):
# puts new item e on top of stack
self._stack.append(e)