Skip to content

Instantly share code, notes, and snippets.

@Chityanj
Created December 7, 2021 14:05
Show Gist options
  • Save Chityanj/a5dc6fbb52ce1233b7baa42abd60a312 to your computer and use it in GitHub Desktop.
Save Chityanj/a5dc6fbb52ce1233b7baa42abd60a312 to your computer and use it in GitHub Desktop.
Modified Stack: Write a program to implement a special type of stack, which apart from supporting normal stack operations: push(), isFull(), pop() and isEmpty(), supports additional operations: getMax(), getMin(), getMiddle().
def display(stack):
print("\n".join(stack))
def push(stack,size):
if isFull(stack,size):
print("Stack is full")
else:
stack.append(input())
def pop(stack):
if isEmpty(stack):
print("error: stack empty")
else:
print(stack.pop())
def isFull(stack,size):
if len(stack)>size:
return True
else:
return False
def isEmpty(stack):
if len(stack) == 0:
return True
else:
return False
def getMax(stack):
if isEmpty(stack):
print("error: stack empty")
else:
return max(stack)
def getMin(stack):
if isEmpty(stack):
print("error: stack empty")
else:
return min(stack)
def getMiddle(stack):
if isEmpty(stack):
print("error: stack empty")
else:
return stack[len(stack)//2]
size=int(input("Initilialize size of stack: "))
size=size-1
stack=[]
flag=True
while flag:
print("\n1.Push\n2.Pop\n3.Display\n4.Get Max\n5.Get Min\n6.Get Middle\n7.isFull\n8.isEmpty\n9.Exit")
choice=int(input("Enter your choice: "))
if choice==1:
push(stack,size)
elif choice==2:
pop(stack)
elif choice==3:
display(stack)
elif choice==4:
print(getMax(stack))
elif choice==5:
print(getMin(stack))
elif choice==6:
print(getMiddle(stack))
elif choice==7:
print(isFull(stack,size))
elif choice==8:
print(isEmpty(stack))
elif choice==9:
flag=False
else:
print("Invalid choice")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment