简体   繁体   English

在 Append 之后获取 Position 并在 Python 的链表中插入

[英]Get Position After Append and Insertion in Linked List in Python

I'm a beginner self-taught programmer and I am following a course on Data Structures.我是一名初学者,自学成才的程序员,我正在学习数据结构课程。 I've run into a snag when trying to call a get_position method after I've run append and insert methods and I don't understand why it's happening.在我运行 append 和 insert 方法之后尝试调用 get_position 方法时遇到了障碍,我不明白为什么会这样。 Referencing the code below, print(ll.get_position(5).value) prints "5" before the insert method is called and after insertion, I would expect print(ll.get_position(6).value) would print "5" but it prints "3".参考下面的代码, print(ll.get_position(5).value)在调用 insert 方法之前打印“5”,在插入之后,我希望print(ll.get_position(6).value)会打印“5”但是它打印“3”。 Additionally I can pass any number as an argument in the get_position method and it will still print "3" or "4" even if the number is outside the bounds of the linked list.此外,我可以在 get_position 方法中将任何数字作为参数传递,即使数字超出链表的范围,它仍会打印“3”或“4”。 I'm assuming the while loop of the insert method is stuck?我假设插入方法的while循环被卡住了? How can I reattach e5 to the linked list?如何将e5重新附加到链表? Why is it getting lost?为什么会迷路?

Thank you!谢谢!

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):

        counter = 1
        current = self.head

        while current and counter <= position:
            if counter == position:
                return current

            current = current.next
            counter += 1

    def insert(self, new_element, position):

        counter = 1
        current = self.head

        if position < 1:
            return None

        elif position == 1:
            new_element.next = self.head
            self.head = new_element

        while current and counter < position:
            if counter == position - 1:
                new_element.next = current.next
                current.next = new_element
                return

            current = current.next
            counter += 1


#  Elements in list
e1 = Element(1)
e2 = Element(2)
e3 = Element(3)
e4 = Element(4)
e5 = Element(5)

# Linked List Setup
ll = LinkedList(e1)
ll.append(e2)
ll.append(e3)
ll.append(e4)
ll.append(e5)

# Should print 5
print(ll.get_position(5).value)

ll.insert(e4, 3)

# Expected Ouput: 1,2,4,3,4
print(ll.get_position(1).value)
print(ll.get_position(2).value)
print(ll.get_position(3).value)
print(ll.get_position(4).value)
print(ll.get_position(5).value)
# Expected output: 5 (Actual Output 3)
print(ll.get_position(6).value)
# Expected output: Error. (Actual Output 4)
print(ll.get_position(7).value)

You are using the existing e4 node which is pointing to node e5.您正在使用指向节点 e5 的现有 e4 节点。 create a new element with value 4 and then pass it to insert method.创建一个值为 4 的新元素,然后将其传递给 insert 方法。

new_e4 = Element(4)
l1.insert(new_e4, 3)

After insertion of e4 you are expecting the linked list to look like插入 e4 后,您期望链表看起来像

1 -> 2 -> 4 -> 3 -> 4 -> 5

But actually the linked list has become:但实际上链表变成了:

1 -> 2 -> 4 <=> 3 5 ( 5 is lost and 3 is pointing back to 4 and hence infinite loop)

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

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