Untitled
class Stack: def __init__(self): self.stack = [] # Add a record to the stack def push(self, record): self.stack.append(record) print(f"Record '{record}' added successfully!") # Delete the last record from the stack def pop(self): if not self.is_empty(): removed_record = self.stack.pop() print(f"Record '{removed_record}' deleted successfully!") else: print("The stack is empty. No record to delete.") # Display all records in the stack def display(self): if not self.is_empty(): print("Current stack records:") for i, record in enumerate(reversed(self.stack), start=1): print(f"{i}. {record}") else: print("The stack is empty.") # Check if the stack is empty def is_empty(self): return len(self.stack) == 0 def main(): stack = Stack() while True: print("\nOptions:") print("1. Add Record") print("2. Delete Record") print("3. Display Records") print("4. Exit") try: choice = int(input("Enter your choice: ")) if choice == 1: record = input("Enter the record to add: ") stack.push(record) elif choice == 2: stack.pop() elif choice == 3: stack.display() elif choice == 4: print("Exiting the program. Goodbye!") break else: print("Invalid choice. Please try again.") except ValueError: print("Please enter a valid number.") if __name__ == "__main__": main()
Leave a Comment