#!/usr/bin/env python3; ############################################################################### # This program demonstrates the Stack ADT # # Copyright © 2021 Richard Lesh. All rights reserved. ############################################################################### import Utils class IntegerStack : # Internal representation for the stack is a list stack = [] def __init__(self) : self.stack = [] def pop(self) : if (len(self.stack) == 0) : raise RuntimeError("Can't pop on empty stack!") return self.stack.pop() def push(self, value) : self.stack.append(value) def peek(self) : if (len(self.stack) == 0) : raise RuntimeError("Can't peek on empty stack!") return self.stack[len(self.stack) - 1] # Begin Main stack = IntegerStack() for x in range(1, 10 + 1) : stack.push(x) try : print(str(stack.peek())) for x in range(1, 11 + 1) : print(str(stack.pop())) except RuntimeError as ex : print(str(ex)) except Exception as ex : print("Unknown Exception!")