OneCompiler

Stack Implementation

239

Stack Introduction .png

Stack Explanation .png

PYTHON CODE

class Stack:
  
  def __init__(self):
    self.items = []
    
  def push(self,item):
    self.items.append(item)
    
  def is_Empty(self):
    return len(self.items) == 0
    
  def pop(self):
    if self.is_Empty():
      return None
    else:
      return self.items.pop()
      
  def peek(self):
    if self.is_Empty():
      return None
    else:
      return self.items[-1]
      
  def size(self):
    return len(self.items)
    
    
myStack = Stack()

print(myStack.is_Empty())
print(myStack.size())
print(myStack.pop())
myStack.push(1)
myStack.push(2)
myStack.push(3)
myStack.push(4)
myStack.push(5)
print(myStack.is_Empty())
print(myStack.size())
print(myStack.peek())
print(myStack.pop())
print(myStack.pop())
print(myStack.peek())
print(myStack.size())