STACK -with list
STACK -with list
RECAP
Push
operation Pop
operation
STARTER ACTIVITY
class Stack:
def __init__(self):
Step:2Implement the push Method
the stack.
self.stack.append(element)
Step 3: Implement the pop Method
• We then implement the pop method to remove and return the top element from the stack.
• def pop(self):
• if not self.is_empty():
• return self.stack.pop()
• else:
• print("Stack is empty")
• return None
Step 4: Implement the peek Method
• We add a peek method to view the top element of the stack without removing it.
• def peek(self):
• if not self.is_empty():
• return self.stack[-1]
• else:
• print("Stack is empty")
• return None
Step 5: Implement the is_empty Method
empty.
• def is_empty(self):
• return len(self.stack) == 0
Step 6: Implement the size Method
• def size(self):
• return len(self.stack)
PLENARY
• Recap all the contents
• Discussions sessions
Any Questions you have? One new syntax you learnt.