From c1083c1eefe432e762af4f6bfa16a88ab710b666 Mon Sep 17 00:00:00 2001 From: charu210703 Date: Sun, 2 Oct 2022 11:44:53 +0530 Subject: [PATCH] Added push and pop methods for stacks --- algorithms/Python/stacks/stacks.py | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 algorithms/Python/stacks/stacks.py diff --git a/algorithms/Python/stacks/stacks.py b/algorithms/Python/stacks/stacks.py new file mode 100644 index 00000000..28fc15b4 --- /dev/null +++ b/algorithms/Python/stacks/stacks.py @@ -0,0 +1,36 @@ +class Stack: + def __init__(self): + self.stack_list = [] + self.stack_size = 0 + def is_empty(self): + return self.stack_size == 0 + def peek(self): + if self.is_empty(): + return None + return self.stack_list[-1] + def size(self): + return self.stack_size + def push(self, value): + self.stack_size += 1 + self.stack_list.append(value) + def pop(self): + if self.is_empty(): + return None + self.stack_size -= 1 + return self.stack_list.pop() +if __name__ == "__main__": + stack_obj = Stack() + limit = 5 + print("Pushing elements into the stack") + for i in range(limit): + print(i) + stack_obj.push(i) + print("is_empty(): " + str(stack_obj.is_empty())) + print("peek(): " + str(stack_obj.peek())) + print("size(): " + str(stack_obj.size())) + print("Popping elements from the stack") + for x in range(limit): + print(stack_obj.pop()) + print("is_empty():" + str(stack_obj.is_empty())) + print("peek(): " + str(stack_obj.peek())) + print("size(): " + str(stack_obj.size())) \ No newline at end of file