Beispiel #1
0
  def remove(self, item):
      """移除数组中第一个匹配的元素, 移除后有必要就缩容
      """
      if item not in self:
          raise KeyError(f'{item} not in bag.')
 
      for i, _item in enumerate(self):
          if _item == item:
              targetIndex = i
              break   
      # 将 targetIndex及其后的元素左移
      for i in range(targetIndex, len(self) - 1):
          self.items[i] = self.items[i+1]
      
      self.size -= 1
      if len(self) < len(self.items) // 2:
          self.items = Array.decrease_capacity(self.items)
Beispiel #2
0
    def remove(self, item):

        if item not in self:
            raise KeyError(f'{item} not in bag.')
        
        frontNode, targetNode = None, None
        for _item in self:
            if _item.data == item:
                targetNode = _item
                break
            else:
                frontNode = _item
        
        if frontNode is None:
            self.items = self.items.next
        else:
            frontNode.next = targetNode.next
        
        self.size -= 1
        if len(self) < len(self.items) // 2: #如果有必要就缩容
            self.items = Array.decrease_capacity(self.items)