Python链表按索引号将节点插入喜欢列表时出现问题

问题描述 投票:0回答:1

我正在尝试将节点插入到喜欢的列表中的特定索引中,尽管我让它适用于边界之外的索引以及前端和末尾的索引,但我似乎无法将节点插入到中间列表正确我已将 11,22,33 插入到链接列表中,试图将“77”插入索引 1(索引 1 为“22”),并且在打印列表时我仍然得到 11,22,33

下面是我的代码

class _List_Node:

    def __init__(self, value, next_):
        """
        -------------------------------------------------------
        Initializes a list node that contains a copy of value
        and a link to the next node in the list.
        Use: node = _List_Node(value, _next)
        -------------------------------------------------------
        Parameters:
            _value - value value for node (?)
            _next - another list node (_List_Node)
        Returns:
            a new _List_Node object (_List_Node)
        -------------------------------------------------------
        """
        self._value = deepcopy(value)
        self._next = next_


class List:

    def __init__(self):
        """
        -------------------------------------------------------
        Initializes an empty list.
        Use: lst = List()
        -------------------------------------------------------
        Returns:
            a new List object (List)
        -------------------------------------------------------
        """
        self._front = None
        self._rear = None
        self._count = 0

    def is_empty(self):
        """
        -------------------------------------------------------
        Determines if the list is empty.
        Use: b = lst.is_empty()
        -------------------------------------------------------
        Returns:
            True if the list is empty, False otherwise.
        -------------------------------------------------------
        """
        # your code here
        return
    def printf(self):
        curr = self._front
        while curr is not None:
            print(curr._value)
            curr = curr._next
    def __len__(self):
        """
        -------------------------------------------------------
        Returns the number of values in the list.
        Use: n = len(lst)
        -------------------------------------------------------
        Returns:
            the number of values in the list.
        -------------------------------------------------------
        """
        # your code here
        return

    def prepend(self, value):
        """
        -------------------------------------------------------
        Adds a copy of value to the front of the List.
        Use: lst.prepend(value)
        -------------------------------------------------------
        Parameters:
            value - a data element. (?)
        Returns:
            None
        -------------------------------------------------------
        """
        # your code here
        Mnode = _List_Node(value,None)
        if self._front is None:
            self._front = Mnode
            self._rear = Mnode
            self._count += 1
        else:
            self._front = _List_Node(value,self._front)
            self._count += 1
            
        return

    def append(self, value):
        """
        ---------------------------------------------------------
        Adds a copy of value to the end of the List.
        Use: lst.append(value)
        -------------------------------------------------------
        Parameters:
            value - a data element (?)
        Returns:
            None
        -------------------------------------------------------
        """
        # your code here
        Mnode = _List_Node(value,None)
        if self._front is None:
            self._front = Mnode
            self._rear = Mnode
            self._count += 1
        else:
            Enode = _List_Node(value,None)
            self._rear._next = Enode
            self._rear = Enode
            
            self._count += 1
        return

    def insert(self, i, value):
        """
        -------------------------------------------------------
        A copy of value is added to index i, following values are pushed right.
        If i outside of range of -len(list) to len(list) - 1, the value is 
        prepended or appended as appropriate.
        Use: lst.insert(i, value)
        -------------------------------------------------------
        Parameters:
            i - index value (int)
            value - a data element (?)
        Returns:
            None
        -------------------------------------------------------
        """
        # your code here

        #If index is to large
        if i > self._count or i == self._count:
            self.append(value)
        elif i < 0 or i == 0:
            self.prepend(value)
        else:
            count = 0
            prev = None
            curr = self._front
            while count !=  i and curr._next is not None:
                count += 1
                prev = curr
                curr = curr._next
            curr = _List_Node(value,prev)
            
                
        return


from List_linked import List

LD = List()
LD.append(11)
LD.append(22)
LD.append(33)
LD.insert(1, 77)
LD.printf()

输出: 11 22 33

预期输出: 11 77 22 33

python linked-list insert nodes
1个回答
0
投票

解决方案:

prev._next = _List_Node(value, curr)

curr/prev 存储对链表中节点对象的引用。这是您设置之前的内容

curr = _List_Node(value,prev)
:

   LD = Node(11, Node(22, Node(33, None)))
     prev^     curr^
LD._front^  

prev 存储对第一个节点的引用,curr 存储对第二个节点的引用。作业完成后:

  LD = Node(11, Node(22, Node(33, None)))
     prev^
LD._front^

curr = Node(77, Node(11, Node(22, Node(33, None)))
              prev^
         LD._front^

您将新节点链接分配给 prev,然后将其存储在 curr 中。 插入函数解析后,curr被遗忘,LD保持不变。

© www.soinside.com 2019 - 2024. All rights reserved.