Skip to content
Permalink
Browse files

Create stackF_Harsh2255.py

this is a program to understand the basuc concept of the stack implimentation by python
  • Loading branch information
Harsh2255 committed Oct 25, 2019
1 parent a19a798 commit a52ae6508353b37954cb08969da0320ae343276a
Showing with 39 additions and 0 deletions.
  1. +39 −0 stackF_Harsh2255.py
@@ -0,0 +1,39 @@
# Python program for implementation of stack

# import maxsize from sys module
# Used to return -infinite when stack is empty
from sys import maxsize

# Function to create a stack. It initializes size of stack as 0
def createStack():
stack = []
return stack

# Stack is empty when stack size is 0
def isEmpty(stack):
return len(stack) == 0

# Function to add an item to stack. It increases size by 1
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]

# Driver program to test above functions
stack = createStack()
push(stack, str(10))
push(stack, str(20))
push(stack, str(30))
print(pop(stack) + " popped from stack")

0 comments on commit a52ae65

Please sign in to comment.
You can’t perform that action at this time.