从双向链表中间删除元素时得到错误的结果

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

用于双重LinkedList的代码:(主要关注else函数的最后一个delete_from_list部分。)

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

def create_list():
    seq = input("Enter the sequence: ").split()
    int_seq = [int(i) for i in seq]
    head = None
    tail = None
    for i in int_seq:
        if i == -1:
            break
        n = Node(i)
        if head is None:
            head = n
            tail = n
        else:                # Tail must be pointing to some node already
            tail.next = n
            n.prev = tail
            tail = n
    return head, tail 

def traverse_list_forward(head):
    if head is None:
        return -1
    curr = head
    arr = []
    while curr:
        arr.append(curr.data)
        curr = curr.next
    print(' '.join(map(str, arr)))
    return head

def traverse_list_backward(tail):
    if tail is None:
        return -1
    curr = tail
    arr = []
    while curr:
        arr.append(curr.data)
        curr = curr.prev
    print(' '.join(map(str, arr)))
    return tail

def delete_from_list(head, tail, elem):
    if head is None or tail is None:
        return -1, -1

    if head.data == elem:     # Delete first element 
        if head == tail:
            head = tail = None
            return -1, -1

        curr = head
        head = head.next
        head.prev = None
        curr = None
        return head, tail

    elif tail.data == elem:   # Delete last element
        curr = tail
        if tail == head:
            tail == None
            return -1, -1

        tail = tail.prev
        tail.next = None
        curr = None
        return head, tail

    else:                     # Delete an element in between head and tail ptrs
        curr = head
        while curr:
            if curr.data == elem:
                break
            else:
                curr = curr.next

        if not curr:
            return -1, -1

        curr.prev = curr.next.prev
        curr.next = curr.prev.next
        print(curr.prev.next.data)
        print(curr.next.prev.data)
        curr.next = curr.prev = None
        curr = None
        return head, tail

删除功能将删除elem的特定节点,并返回更新后的链表的headtail指针。

关于尝试运行:

list_head, list_tail = delete_from_list(list_head, list_tail, 12)

使用输入:1 3 5 6 7 2 12 4 8 0 9,我得到最后else部分(在delete func内部)的print语句的输出为4 12打印声明的输出是否应为2 12,因为curr.next.prev现在指向2?

删除后,如果我现在尝试使用代码遍历列表:

list_head = traverse_list_forward(list_head)
list_tail = traverse_list_backward(list_tail)

我得到这个奇怪的输出为:

1 3 5 6 7 2 12
9 0 8 4 12

头尾指针正确指向其位置,但发生了一些混乱,遍历结果显示。

为什么这行:

        curr.prev = curr.next.prev
        curr.next = curr.prev.next

工作不正常?我在这里想念什么吗?

PS。我正在使用Jupyter Notebook。

试图删除输入121 3 5 6 7 2 12 4 8 0 9后的输出:如图所示>]

enter image description here

用于双重LinkedList的代码:(主要关注delete_from_list函数的最后一个其他部分。)类节点:def __init __(self,data):self.data =数据self.prev = None ...

python python-3.x doubly-linked-list
2个回答
1
投票
在这里if块的内部,您从函数返回,从而阻止了函数内的任何其他代码运行:

0
投票
发现错误,
© www.soinside.com 2019 - 2024. All rights reserved.