我对链表排序有疑问(在python中)

时间:2018-09-30 10:33:58

标签: python

请理解我的英语。 我对顶列表有疑问

class Node:
    ''' Node for a linked list '''
    def __init__(self, data=None, next=None):
        self.data = data
        self.next = next
    def __str__(self):
        ''' Given a node, print the node and the following list'''
        if self == None:
            return ""
        return str(self.data) + " " + (self.next.__str__() if self.next else "")
    def __repr__(self):
        return self.__str__()

def head(lst):
    ''' Remove the first element from a linked List,
        and return a tuple with the removed and the rest of the list'''
    if lst == None:
        print("Invalid Input")
        return
    rest = lst.next
    lst.next = None
    return(lst, rest)

# Test : Creating nodes and connecting them as a linked list
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node1.next = node2
node2.next = node3
print(node1)                    # 1 2 3
print(node1.next.next.data)     # 3
lst1 = Node('a', (Node('b', (Node('c', (Node('d')))))))
print(lst1)                     # a b c d
print(lst1.next.next.next.data) # d

(x,xs) = head(lst1)
print(x.data)                   # a
print(xs)                       # b c d

(z,zs) = head(node3)
print(z.data)                   # 3
print(zs)                       # None
lnklst = Node (2, Node(1, Node(4, Node(3))))

## Sorting

直到这行我都明白了,但我听不懂'def max'

def max(lst):
    '''Given a list, find mx node(the node with largest number),
       remove it from the list, and return a tuple with
       the removed node and the modified list.
       if lst is an empty, return (None, None)
       if lst is a singleton, return (lst, None)'''
    if lst == None:
        return (None, None)
    if lst.next == None:          # lst has one node
        return (lst, None)
    original = lst                # lst will be used to traverse the node
    # Finding the mx, we need to find the previous node as well
    # to delete it from the list.
    prev = lst
    mx = lst
    while lst.next:
        if lst.next.data > mx.data:
            mx = lst.next
            prev = lst
        lst = lst.next
    # Three Cases : mx is the first, the last, or in the middle
    if prev == mx:         # mx is the first
        pass
        mx.next = None
    elif mx.next == None:  # mx is the last
        pass
    else:                  # mx is in the middle
        pass
    return(mx,rest)

def addList(lst, node):
    ''' add the node to the front of lst'''
    if lst == None:
        return node
    node.next = lst
    return node

def sortList(lst):
    ''' sort a linked list in an ascending order '''
    if lst == None:
        return None
    (x, xs) = max(lst)
    sort = x
    while xs:
        (x,xs) = max(xs)
        sort = addList(sort,x)
    return sort

# Construction of list with input data and Test
lst = Node(5,(Node(1,(Node(6,(Node(7,(Node(10,(Node(2,(Node(1,(Node(9)))))))))))))))
lst1 = Node(5,(Node(1,Node(2,(Node(3))))))
lst2 = Node(2,(Node(1)))
lst3 = Node(1,(Node(2,(Node(3,(Node(4,(Node(5,(Node(6,(Node(7,(Node(8)))))))))))))))
lst4 = Node(8,(Node(7,(Node(5,(Node(4,(Node(3,(Node(2,(Node(1,(Node(0)))))))))))))))
lst5 = Node(2,(Node(2,Node(2,(Node(2))))))
print ("Input : " + lst.__str__())
print(sortList(lst))

我在'def max'的通过行输入什么代码?

  1. 您能教我如何“定义候选名单”吗?
  2. 您能教我在“ def max”中的通过行输入什么代码  如果您有更好的主意,请告诉我这段代码。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

max()从输入列表中分离出具有最大数据量的节点,并返回一个元组(max,list_without_max)。

sortlist执行以下操作

  1. 调用max()找到最大节点并将其从输入列表中删除。

  2. 将此最大节点添加到新列表sort

  3. 循环直到输入列表为空

max中,

if prev == mx:         # mx is the first
    rest = mx.next     # rest point to the next of mx
    mx.next = None
elif mx.next == None:  # mx is the last
    prev.next = None
    rest = original
else:                  # mx is in the middle
    prev.next = mx.next
    rest = original
    mx.next = None

我不是100%确信它是正确的。希望对您有所帮助。