麻烦前向链表

时间:2016-02-17 21:56:29

标签: python algorithm linked-list

我已成功实现了向后链接列表,但我正在尝试找出如何实现转发链接列表,而我似乎无法弄清楚如何执行此操作。问题出在我的insert方法中。我正在尝试跟踪前一个节点,并将其指向新创建的节点,但我遗漏了一些东西。

class Node(object):
    def __init__(self, data=None, next_node=None):
        self.data = data
        self.next_node = next_node

    def set_next(self, new_next):
        self.next_node = new_next

    def get_data(self):
        return self.data

    def get_next(self):
        return self.next_node

class LinkedList(object):
    def __init__(self, head=None):
        self.head = head

    def insert(self, data):
        previous_node = self.head
        current_node = self.head
        new_node = Node(data)
        if previous_node is not None:
            previous_node.set_next(new_node)
            previous_node = self.head

3 个答案:

答案 0 :(得分:3)

目前尚不清楚您的insert方法应该做什么。如果要在列表的开头(头部之前)插入,那么您应该设置new_node.set_next(previous_node)self.head = new_node。如果您打算附加到列表的末尾,则需要扫描列表,直到找到包含current_node.get_next() == None的节点并执行current_node.set_next(new_node)

由于这看起来像家庭作业,我不想直接给你答案。我会提供一些伪代码来帮助你入门

def insert(value):
    let current = head
    until current.next_node == None:
        let current = current.next_node

    let current.next_node = Node2(value)

答案 1 :(得分:1)

试试这个。

如果列表为空,请设置头部,然后返回。否则,循环"前进"通过所有元素,直到您点击None节点。完成后,设置适当的next_node值。

class LinkedList(object):
    def __init__(self, head=None):
        self.head = head

    def empty(self):
        return self.head is None

    def insert(self, data):
        new_node = Node(data)    # Want to insert this
        if self.empty():         # Is this list empty?
            self.head = new_node #   Yes? Then add the head
        else:                    # No? There are elements in the list
            current_node = self.head                    # Save a moving reference
            while not current_node.get_next() is None:  # While there are forward nodes
                current_node = current_node.get_next()  # Move the reference forward
            current_node.set_next(new_node)             # Set the last node, thus inserting at the "end"

答案 2 :(得分:0)

让我们写出它的作用:

def insert(self, data):
    previous_node = self.head    # set to head of list
    current_node = self.head     # not used elsewhere
    new_node = Node(data)        # make a new node with the given data.
    if previous_node is not None:        # if the list isn't empty
        previous_node.set_next(new_node) #    point the list head to the new node
        previous_node = self.head        # does nothing; this was already the list head

底线:这会创建一个新节点并使其成为" next"列表头的节点。这就是所有这些;您的列表永远不会超过2个节点。实际上,您的列表无法获得第一个节点,因为您没有要插入空列表的代码。

要解决此问题,您需要一个清晰,适应性强的算法,例如:

Create a new node.
Link the existing list behind the new node:
    Set new.next to the current head node.
    Point the list head to the new node.

请注意,这甚至可以使用空列表。

如果您想要一个添加到结尾的列表,那么还要维护一个指向列表中最后一个节点的self.tail属性。然后你的插入(追加)看起来像这样:

Create a new node.
Link the new node to the existing tail:
    if tail exists:
        Set tail.next to the new node.
    else:
        Set head to the new node
    Set tail to the new node.

这是否足以明确实施?