简体   繁体   中英

Unordered Doubly Linked List Python

I am working on a function inside the DoublyLinkedList class called insertAt. As you can see from my code below, I have gained a little head way but am stuck due to the fact that I cannot figure out how to shift the nodes over to the right after inserting my new node. My output currently is 6 9 7 -4 though it should be 6 9 7 25 -4
If anyone could point me in the right direction I would really appreciate the help!

__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()

Besides of indentation, you have some problems in your insertAt() function.

Indeed you can't assign a DoublyLinkedNode to current.data, so this line is definitely wrong:

current.data = current.next

Moreover because your list is strongly linked, you have to keep the link between the previous Node, the Node you're adding, and the next Node

Here is a potential solution for your insertAt() function (I keeped it close to your code on purpose) which output 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM