coinor.blimpy.Stack'

Package coinor :: Package blimpy :: Module Stack' |
[hide private]
[frames] | no frames]

# Source Code for Module coinor.blimpy.Stack'

     1  '''
     2  A basic stack implementation using a linked list.
     3  '''
     4
     5  __version__    = '1.1.0'
     6  __author__     = 'Aykut Bulut, Ted Ralphs (ayb211@lehigh.edu,ted@lehigh.edu)'
     7  __license__    = 'BSD'
     8  __maintainer__ = 'Aykut Bulut'
     9  __email__      = 'ayb211@lehigh.edu'
    10  __url__        = None
    11  __title__      = 'Stack data structure'
    12
    13  from LinkedList import LinkedList
    14
    15 -class Stack(object):
    16      '''
    17      This stack class is built on top of a linked list data structure.
    18      '''
    19 -    def __init__(self):
    20          self.items = LinkedList()
    21
    22 -    def isEmpty(self):
    23          return len(self.items) == 0
    24
    25 -    def push(self, item):
    26          self.items.append(item)
    27
    28 -    def pop(self, item = None):
    29          if item == None:
    30              return self.items.pop()
    31          else:
    32              self.items.remove(item)
    33              return item
    34
    35 -    def remove(self, item):
    36          self.items.remove(item)
    37
    38 -    def peek(self, item = None):
    39          if item == None:
    40              return self.items[len(self.items)-1]
    41          else:
    42              for i in self.items:
    43                  if i == item:
    44                      return i
    45          return None
    46
    47 -    def size(self):
    48          return len(self.items)
    49
    50 -    def __repr__(self):
    51          return self.items.__repr__()
    52
    53 -    def __str__(self):
    54          return self.items.__str__()
    55
    56  if __name__ == '__main__':
    57
    58      s = Stack()
    59
    60      for i in range(10):
    61          s.push((i, 1))
    62
    63      print s.pop()
    64
    65      print s
    66