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.
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…
A Binary Search Tree (BST) is a type of binary tree that satisfies the following…