简体   繁体   English

如何修复链表位置中间的添加值

[英]how to fix the adding value in middle of linked list location

I'm creating a single linked list insert beginning, end, and middle of the linked list.我正在创建一个单链表插入链表的开头、结尾和中间。 After running code inserting the middle is not working in the linked list changing location value randomly and after running it's not getting.运行代码后,插入中间的链接在随机更改位置值的链表中不起作用,并且在运行后它没有得到。 Can anyone suggest what is wrong with the code?谁能建议代码有什么问题?

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


class SLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    def __iter__(self):
        node = self.head
        while node:
            yield node
            node = node.next

    def insertsll(self, value, location):
        new_node = Node(value)
        if self.head is None:
            self.head = new_node
            self.tail = new_node
        else:
            if location == 0:
                new_node.next = self.head
                self.head = new_node
            elif location == 1:
                new_node.next = None
                self.tail.next = new_node
                self.tail = new_node
            else:
                temp_node = self.head
                index = 0
                while index < location - 1:
                    temp_node = temp_node.next
                    index += 1
                    next_node = temp_node.next
                    temp_node.next = new_node
                    new_node.next = next_node
                    # if temp_node == self.tail:
                    #     self.tail = new_node




sll = SLinkedList()
sll.insertsll(1, 1)
sll.insertsll(2, 1)
sll.insertsll(3, 1)
sll.insertsll(4, 1)
sll.insertsll(0, 0)
sll.insertsll(60, 3)---> run this random changing location not working in correct location
sll.insertsll(50, 4)---> run this random changing location  not working in correct location

print([node.value for node in sll])

output: [0, 1, 50, 2, 3, 4] output:[0、1、50、2、3、4]

Process finished with exit code 0进程以退出代码 0 结束

I think the iteration in the while loop was a little bit too complicated.我认为while循环中的迭代有点太复杂了。 I think with this code it is more understandable what the insert does.我认为使用这段代码更容易理解插入的作用。 Also, I don't know why you had the location === 1 check.另外,我不知道您为什么要进行location === 1检查。 It seems to be not necessary.似乎没有必要。

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


class SLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    def __iter__(self):
        node = self.head
        while node:
            yield node
            node = node.next

    def insertsll(self, value, location):
        new_node = Node(value)
        if self.head is None:
            self.head = new_node
            self.tail = new_node
        else:
            if location == 0:
                new_node.next = self.head
                self.head = new_node
            else:
                temp_node = self.head
                index = 0
                
                # Iterate to insert location
                while index < location - 1:
                    index += 1
                    temp_node = temp_node.next

                # Insert new node
                new_node.next = temp_node.next
                temp_node.next = new_node
                
                # Check if new node is tail
                if new_node.next is None:
                    self.tail = new_node



sll = SLinkedList()
sll.insertsll(1, 1)
sll.insertsll(2, 1)
sll.insertsll(3, 1)
sll.insertsll(4, 1)
sll.insertsll(0, 0)
sll.insertsll(60, 3)
sll.insertsll(50, 4)

print([node.value for node in sll])

Output: Output:

[0, 1, 4, 60, 50, 3, 2]

PS: Such a structure really benefits from a length attribute. PS:这样的结构确实受益于长度属性。 This can be used to check if an insert operation is even allowed.这可用于检查是否允许插入操作。 Probably, you should consider adding it.可能,您应该考虑添加它。

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

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