双链表反向函数不存在

2024-03-28 10:42:20 发布

您现在位置:Python中文网/ 问答频道 /正文

除反转功能外,其他功能都正常。while循环不会结束。我补充了更多细节。我很抱歉。这是我第二次从堆栈溢出获得帮助。你知道吗

这是完整的代码。我不知道如何发送反向功能的尾巴。你知道吗

非常感谢。我真的很感激你的帮助。你知道吗

    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)
                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 search(self, val):
            p = self.head
            while p is not None:
                if p.data == val:
                    return p
                p = p.next

        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= 


        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()
print("_________________")
list.reverse()
list.traverse()

Tags: selfnoneifdefcountvalcurrenttemp