Notes
A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle.
Stack Class
#In python we can use list to help us implement the stack class
class stack:
def __init__(self):
self.items = []
def __str__(self):
return str(self.items)
def push(self, item): #Add an element
self.items.append(item)
def pop(self):
return self.items.pop() #Remove the last element added and then return the deleted element
def peek(self):
return self.items[len(self.items)-1] #Get the last element added.
def getsize(self):
return len(self.items) #Get the number of elements