Before we implement our own Stack class, let's review the properties and methods of a Stack.
The stack abstract data type is defined by the following structure and operations. A stack is structured, as described above, as an ordered collection of items where items are added to and removed from the end called the “top.” Stacks are ordered LIFO. The stack operations are given below.
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)
Let's try it out!
s = Stack()
print s.isEmpty()
True
s.push(1)
s.push('two')
s.peek()
'two'
s.push(True)
s.size()
3
s.isEmpty()
False
s.pop()
True
s.pop()
two
s.size()
1
s.pop()
1
s.isEmpty()
True