将LinkedList类转换为循环Linked List类的最简单方法是什么?

时间:2018-10-03 19:42:24

标签: python python-3.x linked-list

我有一个LinkedList类,它具有接近200行代码。我想通过始终确保任何SELECT CallDate ,MediaChannel ,SubCategory ,Vendor ,BusinessVertical ,NumberOfLeads = COUNT(*) ,Cost = NULLIF(SUM(CostPerLead), 0) ,SourceName ,ParentLeadSource ,IsBillable ,dvce_type FROM ( SELECT CallDate = CAST(cd.CallDateTime AS DATE) ,MediaChannel = ftlc.channel2 ,SubCategory = ftlc.sub ,Vendor = ftlc.vendor ,BusinessVertical = ftlc.business_vertical ,CostPerLead = CAST(cpl.cost AS INT) ,SourceName = bt.LeadCompany ,ParentLeadSource = ftlc.fruit ,IsBillable = CASE WHEN ISNUMERIC(cd.TalkTimeMinutes) = 1 AND ( CAST(cd.TalkTimeMinutes AS DECIMAL(10, 2)) * 60 >= CAST(bt.Billabletime AS INT) OR bt.Billabletime = 900 ) THEN 1 ELSE 0 END ,dvce_type = ftlc.dvce_type FROM dbo.Abc d WITH (NOLOCK) INNER JOIN dt.cde cd WITH (NOLOCK) ON d.FullDate = CAST(cd.CallDateTime AS DATE) INNER JOIN dt.hij m WITH (NOLOCK) ON cd.CallerId = m.DialogTechPhoneNumber INNER JOIN dt.klm bt WITH (NOLOCK) ON m.LeadSourceInfoId = bt.LeadSourceId AND cd.CallDateTime BETWEEN bt.StartDateTime AND ISNULL(bt.EndDateTime, GETDATE()) INNER JOIN dbo.jkl ftlc WITH (NOLOCK) ON bt.ParentLeadSource = ftlc.fruit INNER JOIN dbo.xyz cpl WITH (NOLOCK) ON ftlc.lead_company = cpl.lead_company AND cd.CallDateTime BETWEEN cpl.start_date AND ISNULL(cpl.end_date, GETDATE()) WHERE d.FullDate >= '2018-08-01' AND ( ISNUMERIC(cd.TalkTimeMinutes) = 1 AND ( CAST(cd.TalkTimeMinutes AS DECIMAL(10, 2)) * 60 >= CAST(bt.Billabletime AS INT) OR bt.Billabletime = 900 ) ) ) sub GROUP BY CallDate ,MediaChannel ,SubCategory ,Vendor ,BusinessVertical ,SourceName ,ParentLeadSource ,IsBillable ,dvce_type; 来创建新的class LLCircular(LinkedList)。我相信我需要相应地更新myLL.tail.next is myLL.headappend()push()等。有什么办法可以使原始LinkedList类保持完整吗?也许是装饰器或一些笨拙的方法?

为简便起见,如果阅读代码,我的remove()方法就是push()的反函数。我还有一个append()pop()方法,如果我只是重写这些方法,则需要对其进行更新。当我试图避免这种方法时,我没有在代码的那一部分发布。

remove()

测试代码:

class LinkedListNode:
   def __init__(self, value, nextNode=None, prevNode=None):
      self.value  = value
      self.next   = nextNode
      self.prev   = prevNode
   def __str__(self):
      return str(self.value)

class LinkedList:
   def __init__(self, values=None):
      self.head   = None
      self.tail   = None
      if values is not None:
         self.append(values)
   def __str__(self):
      values = [str(x) for x in self]
      return ' -> '.join(values)
   def append(self, value=None):
      if value is None:
         raise ValueError('ERROR: LinkedList.py: append() `value` PARAMETER MISSING')
      if isinstance(value, list):
         for v in value:
            self.append(v)
         return
      elif self.head is None:
         self.head   = LinkedListNode(value)
         self.tail   = self.head
      else:
         ''' We have existing nodes  '''
         ''' Head.next is same '''
         ''' Tail is new node '''
         self.tail.next = LinkedListNode(value, None, self.tail)
         self.tail      = self.tail.next
         if self.head.next is None:
            self.head.next = self.tail.prev
      return self.tail

'''class LLCircular(LinkedList):'''
    ''' ??? '''

2 个答案:

答案 0 :(得分:1)

您想要的是使用LinkedList关键字调用super基类函数,然后对LLCircular类函数进行少量修改,即:

class LLCircular(LinkedList):
    def append(self, value=None):
        super(LLCircular, self).append(value)
        # In addition to having called the LinkedList append, now you want
        # to make sure the tail is pointing at the head
        self.tail.next = self.head
        self.head.prev = self.tail

答案 1 :(得分:1)

如果它是“圆形”,则不需要尾巴或头部,对吗? “ append”也没有意义-insert_after和insert_before方法应该足够了-而且,任何节点都是对完整循环列表的引用,不需要其他对象:

class Circular:
    def __init__(self, value=None):
        self.value = value
        self.next = self
        self.previous = self
    def insert_after(self, value):
        node = Circular(value)
        node.next = self.next
        node.previous = self
        self.next.previous = node
        self.next = node
    def insert_before(self, value):
        node = Circular(value)
        node.next = self
        node.previous = self.previous
        self.previous.next = node
        self.previous = node
    def del_next(self):
        self.next = self.next.next
        self.next.previous = self
    def __iter__(self):
        cursor = self.next
        yield self
        while cursor != self:
             yield cursor
             cursor = cursor.next
    def __len__(self):
        return sum(1 for _ in self)