【问题标题】:Sorting a Sublist in a Linked List Python在链接列表 Python 中对子列表进行排序
【发布时间】:2013-05-25 18:37:45
【问题描述】:
def sublist(head):
    current=head
    #temp=current #If my list is 3->2->1->4->5->6 it goes into an infinite loop
    while ((current.next is not None) and current.value < current.next.value ):
        temp=current
        current=current.next
    start=current
    while ((current.next is not None) and current.value > current.next.value ):
        current=current.next
    last=current.next
    current.next=None
    first=reverse_linked_list(start)
    temp.next=first

    while first.next is not None:
        first=first.next
    first.next=last

    while head:
        print head.value
        head=head.next
    return head

代码的工作: 我将代码的输入作为无序子列表提供,其中列表中的子列表按降序排列,而链表的其余部分按升序排列..

该代码适用于以下输入 1, 2, 3, 4 ,5, 9, 8, 7, 10 和 1,10,9,8,7,6,5.. 即中间和末尾的未排序列表,但如果列表不起作用对于 3,2,1,4,5,6 等输入,在开始时未排序! 谁能帮帮我。。

【问题讨论】:

标签: python sorting linked-list sublist


【解决方案1】:

我无法弄清楚您的代码,但我怀疑无限循环的原因是您将temp 设置为current 而不是current.value,因此您自己翻转了 cons 单元格他们的内容。这可能是在创建您无限迭代的循环链接列表。

但是,我会这样做(使用基本的冒泡排序算法):

from functools import total_ordering

@total_ordering
class cons:
    def __init__(self, head, tail=None):
        self.head = head
        self.tail = tail

    @classmethod
    def fromlist(self, l):
        if l == []:
            return None
        return cons(l[0], cons.fromlist(l[1:]))

    def tolist(self):
        if self.tail == None:
            return [self.head]
        else:
            return [self.head] + self.tail.tolist()

    def flip(self):
        if not self.tail == None:
            tmp = self.head
            self.head = self.tail.head
            self.tail.head = tmp

    def __lt__(self, other):
        if other == None:
            return True
        else: 
            return self.head < other.head
    def __eq__(self, other):
        if other == None:
            return False
        else:
            return self.head == other.head

def llsort(head):
    changed = True
    while changed:
        changed = False
        current = head
        while current != None:
            if current > current.tail:
                current.flip()
                changed = True
            current = current.tail

编辑__lt__flip 对其他用途不可靠。事实上,我只将__lt____eq__ 放在那里,因为我试图找到一种方法来使用python 的内置排序系统之一,但无法让它工作。为了简化一点:

class cons:
    def __init__(self, head, tail=None):
        self.head = head
        self.tail = tail

    def flip(self):
        if not self.tail == None:
            tmp = self.head
            self.head = self.tail.head
            self.tail.head = tmp


def llsort(head):
    changed = True
    while changed:
        changed = False
        current = head
        while current.tail != None:
            if current.head > current.tail.head:
                current.flip()
                changed = True
            current = current.tail

【讨论】:

  • 我不应该使用任何内置函数..我也不能使用中间列表..无论如何感谢您的帮助..我理解我在分析您的代码时的错误! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 2012-11-07
  • 1970-01-01
  • 2013-10-12
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
相关资源
最近更新 更多