Write a class to implement stack statically.


In [28]:
class UnderflowError(Exception):
    def __init__(self):
        pass
class Stack(object):
    '''Class to implement stack statically'''
    def __init__(self,size):
        self.s = [0 for i in xrange(size)]
        self.size = size
        self.top = -1
    def push(self,elem):
        if self.top<(self.size-1):
            self.top+=1
            self.s[self.top] = elem
        else:
            raise OverflowError
    def pop(self):
        if self.top>-1:
            self.top-=1
            return self.s[self.top+1]
        else:
            raise UnderflowError
    def empty(self):
        if self.top==-1:
            return True
        else:
            return False

In [ ]: