# MENU-DRIVEN STACK CODE ############ CHECK IF STACK IS FULL ################# def IsFull(): global StackTop if (StackTop + 1 == MaxSize): # THEN StackFull = True else: StackFull = False # ENDIF return StackFull #END IsFull. def IsFull2(): global StackTop return(StackTop + 1 == MaxSize) #END IsFull2. ##################################################### ############ CHECK IF STACK IS EMPTY ################# def IsEmpty(): global StackTop if (StackTop == -1): # THEN StackEmpty = True else: StackEmpty = False # ENDIF return StackEmpty #END IsEmpty. def IsEmpty2(): global StackTop return(StackTop == -1) #END IsEmpty2. ##################################################### ############### PRINT STACK ################# def PrintStack(): global StackTop LocalTop = StackTop print(" ") print("The Stack is: ") print("===========") while (LocalTop != -1): print(Stack[LocalTop]) LocalTop = LocalTop - 1 print("===========") #END PrintStack. ############### PUSH ONTO THE STACK ################# def Push(N): global StackTop if (IsFull() == True): # THEN print("The stack is full") else: StackTop = StackTop + 1 Stack[StackTop] = N # ENDIF #END IsFull. ##################################################### ################ POP OFF THE STACK ################## def Pop(): N = 0 global StackTop if (IsEmpty() == True): # THEN print("The stack is Empty") else: N = Stack[StackTop] StackTop = StackTop - 1 # ENDIF return N #END IsFull. ##################################################### ################ TOP OFF THE STACK ################## def Top(): N = 0 global StackTop if (IsEmpty() == True): # THEN print("The stack is Empty") else: N = Stack[StackTop] # ENDIF return N #END IsFull. ##################################################### ######################################### # MAIN PROGRAM ######################################### Stack = [31,41,59,26,53,59,67] MaxSize = 7 StackTop = 5 choice = '' while (choice != '9'): print("Select Operation.") print("1. Check if the Stack is Empty") print("2. Check if the Stack is Full") print("3. Add to Stack") print("4. Pop from Stack") print("5. Top of Stack") print(" ") print("9. Exit") print(" ") choice = input("Enter choice: ") if choice == '1': AnswersEmpty = IsEmpty() print(AnswersEmpty) PrintStack() elif choice == '2': AnswersFull = IsFull() print(AnswersFull) PrintStack() elif choice == '3': PushVal = input("Enter value to add:") Push(PushVal) PrintStack() elif choice == '4': PopVal = Pop() print(PopVal) PrintStack() elif choice == '5': TopVal = Top() print(TopVal) PrintStack() elif choice == '9': print("Exiting") else: print("Invalid input") #ENDIF #ENDWHILE ######################################### # END.