无序双链表Python

时间:2015-09-16 16:26:24

标签: python

我正在处理名为insertAt的DoublyLinkedList类中的函数。正如你从我下面的代码中看到的那样,我已经获得了一些小的方法,但由于我无法弄清楚在插入新节点后如何将节点移到右边这一事实。我目前的输出是     6 9 7 -4 虽然它应该是     6 9 7 25 -4
如果有人能指出我正确的方向,我真的很感激帮助!

__author__ = 'admin'


class DoublyLinkedNode:
""" A single node in a doubly-linked list.
    Contains 3 instance variables:
        data: The value stored at the node.
        prev: A pointer to the previous node in the linked list.
        next: A pointer to the next node in the linked list.
"""

    def __init__(self, value):
        """
        Initializes a node by setting its data to value and
        prev and next to None.
        :return: The reference for self.
        """
        self.data = value
        self.prev = None
        self.next = None


class DoublyLinkedList:
"""
The doubly-linked list class has 3 instance variables:
    head: The first node in the list.
    tail: The last node in the list.
    size: How many nodes are in the list.
"""

    def __init__(self):
        """
        The constructor sets head and tail to None and the size to zero.
        :return: The reference for self.
        """
        self.head = None
        self.tail = None
        self.size = 0

    def addFront(self, value):
        """
        Creates a new node (with data = value) and puts it in the
        front of the list.
        :return: None
        """
        newNode = DoublyLinkedNode(value)
            if (self.size == 0):
            self.head = newNode
            self.tail = newNode
            self.size = 1
        else:
            newNode.next = self.head
            self.head.prev = newNode
            self.head = newNode
            self.size += 1

    def addRear(self, value):
        """
        Creates a new node (with data = value) and puts it in the
        rear of the list.
        :return: None
        """
        newNode = DoublyLinkedNode(value)
        if (self.size == 0):
            self.head = newNode
            self.tail = newNode
            self.size = 1
        else:
            newNode.prev = self.tail
            self.tail.next = newNode
            self.tail = newNode
            self.size += 1

    def removeFront(self):
        """
        Removes the node in the front of the list.
        :return: The data in the deleted node.
        """
        value = self.head.data
        self.head = self.head.next
        if self.head != None:
            self.head.prev = None
        self.size -= 1
        return value

    def removeRear(self):
        """
        Removes the node in the rear of the list.
        :return: The data in the deleted node.
        """
        value = self.tail.data
        self.tail = self.tail.prev
        if self.tail != None:
            self.tail.next = None
        self.size -= 1
        return value

    def printItOut(self):
        """
        Prints out the list from head to tail all on one line.
        :return: None
        """
        temp = self.head
        while temp != None:
            print(temp.data, end=" ")
            temp = temp.next
        print()

    def printInReverse(self):
        """
        Prints out the list from tail to head all on one line.
        :return: None
        """
        temp = self.tail
        while temp != None:
            print(temp.data, end=" ")
            temp = temp.prev
        print()


     def indexOf(self, value):
        counter = 0
        current = self.head
        while current.data != None:
            if current.data == value:
               return counter
            else:
               current = current.next
            counter += 1
        return -1


    def insertAt(self, index, value):
        newNode = DoublyLinkedNode(value)
        counter = 0
        current = self.head
        while counter != self.size:


            if counter == index:
                self.size += 1
                current.data = current.next
                current.data = newNode.data

            else:

                current = current.next
            counter += 1



    def main():
        dll = DoublyLinkedList()
        dll.addRear(6)
        dll.addRear(9)
        dll.addRear(25)
        dll.addRear(-4)

        dll.insertAt(2,7)
        dll.printItOut()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

除了缩进之外,insertAt()函数还有一些问题。

实际上你不能将DoublyLinkedNode分配给current.data,所以这一行肯定是错误的:

current.data = current.next

此外,由于您的列表是强关联的,因此您必须保留上一个节点,您要添加的节点和下一个节点之间的链接

这是insertAt()函数的一个潜在解决方案(我将其保存在您的代码附近),输出6 9 7 25 -4

def insertAt(self, index, value):
    newNode = DoublyLinkedNode(value)
    counter = 0
    current = self.head
    while counter != self.size:
        if counter == index:
            newNode.prev = current.prev
            newNode.next = current
            current.prev.next = newNode
            current.prev = newNode
        else:
            current = current.next
        counter += 1      
    self.size += 1