From 37fdadda69d0df16bd5a5f63e421df6fd544b445 Mon Sep 17 00:00:00 2001 From: guru_prashanth <127014978+guruprashanth2004@users.noreply.github.com> Date: Thu, 11 Jul 2024 20:22:41 +0530 Subject: [PATCH] New Algorithm --- data-structures/Stack/StackImplementation.py | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 data-structures/Stack/StackImplementation.py diff --git a/data-structures/Stack/StackImplementation.py b/data-structures/Stack/StackImplementation.py new file mode 100644 index 0000000..e585bf7 --- /dev/null +++ b/data-structures/Stack/StackImplementation.py @@ -0,0 +1,32 @@ + +from sys import maxsize + +def createStack(): + stack = [] + return stack + +def isEmpty(stack): + return len(stack) == 0 + +def push(stack, item): + stack.append(item) + print(item + " pushed to stack ") + +# Function to remove an item from stack. It decreases size by 1 +def pop(stack): + if (isEmpty(stack)): + return str(-maxsize -1) # return minus infinite + + return stack.pop() + +# Function to return the top from stack without removing it +def peek(stack): + if (isEmpty(stack)): + return str(-maxsize -1) # return minus infinite + return stack[len(stack) - 1] + +stack = createStack() +push(stack, str(10)) +push(stack, str(20)) +push(stack, str(30)) +print(pop(stack) + " popped from stack")