Overview of Stack Data Structure π€π
class Stack:
def __init__(self):
self.items = []
def isEmpty(self) -> bool:
return self.items == []
def push(self, data: any) -> list:
self.items.append(data)
def pop(self) -> any:
return self.items.pop()
def peek(self) -> any:
return self.items[len(self.items) - 1]
def size(self) -> int:
return len(self.items)
s = Stack()
#Add element to Stack
s.push(1)
s.push(2)
s.push("hello")
#size of stack
n = s.size()
print("Stack Size {}".format(n))
#delete element
print(s.pop())
#pick top element
print(s.peek())
#check empty
print(s.isEmpty()) Recommended Reading: Balanced Parentheses Check in Python π
If you like my post please follow me to read my latest post on programming and technology.
A design pattern isΒ a reusable solution to a commonly occurring problem in software design. They…
Factory Method is a creational design pattern that deals with the object creation. It separates…
You are given two singly linked lists that intersect at some node. Your task is…
A builder plans to construct N houses in a row, where each house can be…
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…