双链表中间插入不起作用

时间:2019-03-01 08:18:33

标签: python python-3.x linked-list doubly-linked-list

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

class LinkedList:
    def __init__(self):
        self.head = None
        self.tail = None
        self.count = 0

    def StartInsert(self, val):
        newNode = Node(val)
        if self.count == 0:
            self.head = newNode
            self.tail = newNode
        else:
            self.head.prev = newNode
            newNode.next = self.head
            self.head = newNode
        self.count += 1

    def EndInsert(self, val):
        newNode = Node(val)
        if self.count == 0:
            self.head = newNode
            self.tail = newNode
        else:
            self.tail.next = newNode
            newNode.prev = self.tail
            self.tail = newNode
        self.count += 1

    def MiddleInsert(self, val):
        newNode = Node(val)
        if self.count == 0:
            self.head = newNode
            self.tail = newNode
        else:
            index = math.ceil(self.count/2)-1
            temp = self.head
            while index > 0:
                temp = temp.next
                index -= 1
            temp.next = Node(val, temp.next)
            temp.prev = Node(temp.prev.data, temp)
        self.count +=1


    def delete(self, val):
        curNode = self.head
        while curNode != None:
            if curNode.data == val:
                if curNode.prev != None:
                    curNode.prev.next = curNode.next
                else:
                    self.head = curNode.next

                if curNode.next != None:
                    curNode.next.prev = curNode.prev
                else:
                    self.tail = curNode.prev

                self.count -= 1

            curNode = curNode.next

    def reverse(self):
        temp = None
        current = self.head
        while current != None:
            temp = current.prev
            current.prev = current.next
            current.next = temp
            current = current.prev
        if temp:
            self.head = temp.prev
            self.tail = temp.next



    def traverse(self):
        s = ""
        p = self.head
        while p is not None:
            s += str(p.data) + ' ';
            p = p.next
        print(s + "| count: " + str(self.count))

list = LinkedList()
list.EndInsert("a")
list.StartInsert("b")
list.StartInsert("c")
list.EndInsert("d")
list.MiddleInsert("c")
list.traverse()

list.reverse()
list.traverse()

中间插入可提供正确的返回值,但不会停止。我对单链列表做了相同的方法,但对于双链列表似乎无法正常工作。它会返回适当的值,但会一直卡在while循环中。

我试图弄清楚如何连接newNode()。请通过显示代码和出现此错误的原因来帮助我。

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

在双向链接列表中,您必须维护上一个和下一个指针。在MiddleInsert中,选择了要在其后添加新节点的元素后,必须在该元素及其跟随者之间插入新元素。

让我们将C称为新节点,将A称为所选节点,然后说B=A.next。插入之前,您有A.next == BB.prev == A;插入后,您想要 A.next == CC.prev == AC.next == BB.prev == C

只需将其写在MiddleInsert中(不相关,但这里不需要math模块,而for ... in range(...)是计数循环的Python方式):

def MiddleInsert(self, val):
    newNode = Node(val)
    if self.count == 0:
        self.head = newNode
        self.tail = newNode
    elif self.count == 1:
        self.tail = newNode
        self.head.next = newNode
        newNode.prev = self.head
    else:
        index = (self.count-1) // 2
        temp = self.head
        for i in range(index):
            temp = temp.next
        temp.next.prev = newNode
        newNode.next = temp.next
        newNode.prev = temp
        temp.next = newNode
    self.count +=1

答案 1 :(得分:1)

最初的错误是,您正在Node方法中创建更多的MiddleInsert

这可能会导致您在代码中发现错误。

在删除了这些额外的创建内容之后,您只需切换prev和next指针,并检查temp实际上不是最后一个元素:

def MiddleInsert(self, val):
    newNode = Node(val)
    if self.count == 0:
        self.head = newNode
        self.tail = newNode
    else:
        index = math.ceil(self.count/2)-1
        temp = self.head
        while index > 0:
            temp = temp.next
            index -= 1
        newNode.next = temp.next
        temp.next = newNode
        newNode.prev = temp
        if newNode.next is not None:
            newNode.next.prev = newNode
    self.count +=1