#~~Author~~. Don Perterson #~~Email_Address~~. donp@donp.boi.hp.com #~~Script_Type~~. Python #~~Sub_Type~~. Misc #~~Keywords~~. No Keywords #~~Comment~~. #class stack: # '''List-based implementation of a stack class. The stack elements # can be any object, sequence, number, etc. # # The methods are: # push(object) Push an object onto the stack # pop() Remove the top of stack and return it # is_empty() Return nonzero if stack is empty # num_elements() Number of elements on stack # dump_stack() Print stack to stdout, 1 line per element # ''' #~~Script~~. class Stack: def __init__(self): self.stack = [] def push(self, object): self.stack.append(object) def pop(self): if len(self.stack) == 0: raise "Error", "stack is empty" obj = self.stack[-1] del self.stack[-1] return obj def is_empty(self): if len(self.stack) == 0: return 1 return 0 def num_elements(self): return len(self.stack) def dump_stack(self): print "Top of stack is last element in list (number 1)" n = len(self.stack) fmt = " %%%dd %%s" % len(`n + 1`) for ix in xrange(n): print fmt % (n - ix, self.stack[ix]) if __name__ == "__main__": list1 = [1.2, "3.4"] s = Stack() error = "Error" if s.num_elements() != 0: raise error s.push("Hi") s.push(4) s.push(list1) if s.is_empty(): raise error s.dump_stack(); el = s.pop() if el != list1: raise error del el el = s.pop() if el != 4: raise error del el el = s.pop() if el != "Hi": raise error if not s.is_empty(): raise error