【问题标题】:Inserting and getting the position in a Linked List class using python使用python在链接列表类中插入和获取位置
【发布时间】:2021-03-21 10:21:22
【问题描述】:

我是数据结构和算法的新手。我是一名自学成才的 Python 程序员。我正在做一个关于它的课程,我想制作一个链表,获取链表中的特定位置,插入和删除列表中的元素。 所以我写了我的代码,对我来说,这似乎很好。它没有给我任何错误,但也没有执行。

这是我写的代码,

class Element(object):
    def __init__(self, value):
        self.value = value
        self.next = None
        
class LinkedList(object):
    def __init__(self, head=None):
        self.head = head
        
    def append(self, new_element):
        current = self.head
        if self.head:
            while current.next:
                current = current.next
            current.next = new_element
        else:
            self.head = new_element
            
    def get_position(self, position):
        """Get an element from a particular position.
        Assume the first position is "1".
        Return "None" if position is not in the list."""
        
        current = self.head
        if self.head:
            while current.next:
                if current == position:
                    return current
                else:
                    continue
        
        else:
            return None
    
    def insert(self, new_element, position):
        """Insert a new node at the given position.
        Assume the first position is "1".
        Inserting at position 3 means between
        the 2nd and 3rd elements."""
        
        current = self.head
        if self.head:
            while current.next:
                if current.next == position:
                    current.next = new_element
                    break
                else:
                    continue
        else:
            self.head = new_element

错误在get position函数和insert函数

谁能告诉我我做错了什么? 循环有问题吗? 请帮助我,我将不胜感激!!谢谢。

【问题讨论】:

  • “错误在...”:请提供错误信息并指出在哪一行。
  • 我没有收到错误消息,但代码也没有执行。我正在使用 Jupyter Notebook,它只是在我开始执行给定功能时不断加载。

标签: python list algorithm data-structures linked-list


【解决方案1】:

get_position中的一些问题:

  • current == position 不是您需要验证的条件。 position 是一个数字,current 是一个元素,所以它们并没有真正进行比较。您需要验证 position 是 1 还是 2,...取决于您在列表中的位置。
  • 循环永远不会将current 前进到下一个节点。所以这代表了一个无限循环。
  • while 条件不应该检查current.next,而是current。否则,您将永远不会检查列表中的最后一个节点。

这里是您更正的代码:

def get_position(self, position):
    if position < 1:  # Just in case the position is too small
        return
    current = self.head
    while current and position > 1:
        position -= 1
        current = current.next
    return current

因此,只要位置减少到 1,或者没有更多节点,循环就会结束。在后一种情况下,返回值将是None。

虽然您的问题是关于get_position 函数,但您的insert 也有同样的问题。最重要的是,它还错误地处理了head 的情况。当提供的位置为 1 时,它还应该更改 head。

insert方法其实可以利用上面的函数找到应该在要插入的节点之前的节点:

def insert(self, new_element, position):
    if position == 1:
        new_element.next = self.head
        self.head = new_element
    else:
        before = self.get_position(position-1)
        if before is None:
            raise ValueError("invalid position")
        new_element.next = before.next
        before.next = new_element

【讨论】:

  • 这行得通,非常感谢。不过我有一些问题......首先,我创建的链表函数在while循环条件'current.next'下工作正常,为什么我不能使用相同的条件来循环通过相同的链表,而不是使用'当前和位置> 1'?因为我已将当前变量声明为 head。那么我可以只为位置 1 写一个条件,然后用我写的相同 while 循环遍历下一个位置吗?非常感谢您的帮助,非常感谢!
  • current.next 的循环旨在查找列表中的最后一个节点。如果在append 中我们将使用current 作为条件,那么您将在循环之后留下None,并且仍然不知道最后一个节点是什么。对于其他两种方法,目标是不同的,因为您需要考虑position,而您在append 中没有处理它。当然,还有一种方法可以使用current.next 进行循环,但是当您需要第一个节点时,您需要有一个特殊情况。通常有多种方法可以得出相同的结果。
  • 这消除了疑问!非常感谢你,我很感激!
猜你喜欢
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多