00001 ## 00002 # 00003 # Stack class 00004 # 00005 # Copyright (C) 2002 GDS Software 00006 # 00007 # This program is free software; you can redistribute it and/or 00008 # modify it under the terms of the GNU General Public License as 00009 # published by the Free Software Foundation; either version 2 of 00010 # the License, or (at your option) any later version. 00011 # 00012 # This program is distributed in the hope that it will be useful, 00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 # GNU General Public License for more details. 00016 # 00017 # You should have received a copy of the GNU General Public 00018 # License along with this program; if not, write to the Free 00019 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 00020 # MA 02111-1307 USA 00021 # 00022 # See http://www.gnu.org/licenses/licenses.html for more details. 00023 # 00024 __version__ = "$Id: stack.py,v 1.3 2002/08/21 12:41:49 donp Exp $" 00025 00026 ## 00027 # List-based implementation of a stack class. The stack elements 00028 # can be any object, sequence, number, etc. 00029 # 00030 # The methods are: 00031 # push(object) Push an object onto the stack 00032 # pop() Remove the top of stack and return it 00033 # is_empty() Return nonzero if stack is empty 00034 # num_elements() Number of elements on stack 00035 # dump_stack() Print stack to stdout, 1 line per element 00036 # 00037 class stack: 00038 def __init__(self): 00039 self.stack = [] 00040 00041 def push(self, object): 00042 self.stack.append(object) 00043 00044 def pop(self): 00045 if len(self.stack) == 0: 00046 raise "Error", "stack is empty" 00047 obj = self.stack[-1] 00048 del self.stack[-1] 00049 return obj 00050 00051 def is_empty(self): 00052 if len(self.stack) == 0: 00053 return 1 00054 return 0 00055 00056 def num_elements(self): 00057 return len(self.stack) 00058 00059 def dump_stack(self): 00060 print "Top of stack is last element in list (number 1)" 00061 n = len(self.stack) 00062 fmt = " %%%dd %%s" % len(`n + 1`) 00063 for ix in xrange(n): 00064 print fmt % (n - ix, self.stack[ix]) 00065 00066 00067 if __name__ == "__main__": 00068 list1 = [1.2, "3.4"] 00069 s = stack() 00070 error = "Error" 00071 if s.num_elements() != 0: raise error 00072 s.push("Hi") 00073 s.push(4) 00074 s.push(list1) 00075 if s.is_empty(): raise error 00076 el = s.pop() 00077 if el != list1: raise error 00078 del el 00079 el = s.pop() 00080 if el != 4: raise error 00081 del el 00082 el = s.pop() 00083 if el != "Hi": raise error 00084 if not s.is_empty(): raise error 00085 00086
© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."
Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.
122,241 lines of code and growing...