繁体   English   中英

带有节点的Python链接列表:查找最小值

[英]Python Linked list with Nodes: find minimum

我试图创建我的UnorderedList:类的find_min()方法,该方法应该在列表中找到最小数量。

def find_min(self):
    current = self.head
    previous = None
    found = False
    while not found:
        if current.getData == None:
            found = True
            minimum = "None"
        else: 
    #this is where i got stuck.... 

如果列表为空,则该方法应返回“无”。 我试图使用“上一个”和“当前”穿过节点,并将它们彼此进行比较,以查看最小值是多少,但是失败了。

我也做了一个clear / delete_all方法不能正常工作。 应该清除列表中的所有项目并留下一个空列表。

def clear(self):
    current = self.head
    current.setNext(None)
    endOfList = current.getData()
    self.remove(endOfList)

这是我两节课的其余部分。

class Node:
def __init__(self,initdata):
    self.data = initdata
    self.next = None

def getData(self):
    return self.data

def getNext(self):
    return self.next

def setData(self,newdata):
    self.data = newdata

def setNext(self,newnext):
    self.next = newnext

class UnorderedList:

def __init__(self):
    self.head = None
    self.count = 0

def isEmpty(self):
    return self.head == None

def add(self,item):
    temp = Node(item)
    temp.setNext(self.head)
    self.head = temp

def size(self):
    current = self.head
    count = 0
    while current != None:
        count = count + 1
        current = current.getNext()
    return count

def search(self,item):
    current = self.head
    found = False
    while current != None and not found:
        if current.getData() == item:
            found = True
        else:
            current = current.getNext()
    return found

def remove(self,item):
    current = self.head
    previous = None
    found = False
    while not found:
        if current.getData() == item:
            found = True
        else:
            previous = current
            current = current.getNext()

    if previous == None:
        self.head = current.getNext()
    else:
        previous.setNext(current.getNext())

def __str__(self):
    result = '['
    current = self.head
    while current != None:
        result += str(current.getData()) + ', '
        current = current.getNext()
    result += ']'
    return result

def append(self, item):
    current = self.head
    while current.getNext() != None:
        current = current.getNext()
    current.setNext(Node(item))

def pop(self):
    current = self.head
    found = False
    endOfList = None
    while current != None and not found:
        if current.getNext() == None:
            found = True
            endOfList = current.getData()
            self.remove(endOfList)
        else:
            current = current.getNext()

def clear(self):
    current = self.head
    current.setNext(None)
    endOfList = current.getData()
    self.remove(endOfList)

任何帮助都会很棒,谢谢。

为了找到最小值,只需遍历列表即可。 您可以将float('inf')用作合理的初始值,以便进行比较。

def find_min(self):
    next_node = self.head
    if next_node is None:
        return None

    minimum = float('inf')        
    while next_node:
        value = next_node.getData()
        if value < minimum:
            minimum = value

        next_node = next_node.getNext()

    return minimum

关于清除列表,您所需要做的就是将self.head设置为None 其余的将由垃圾收集器完成。

def clear(self):
    self.head = None
    self.count = 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM