Python 无序双链表
我正在处理DoublyLinkedList类中名为insertAt的函数。正如您从下面的代码中看到的,我已经获得了一些优势,但由于插入新节点后无法确定如何将节点移到右侧,因此我陷入了困境。我目前的输出是 6 9 7 -4 虽然应该是这样 69725-4Python 无序双链表,python,Python,我正在处理DoublyLinkedList类中名为insertAt的函数。正如您从下面的代码中看到的,我已经获得了一些优势,但由于插入新节点后无法确定如何将节点移到右侧,因此我陷入了困境。我目前的输出是 6 9 7 -4 虽然应该是这样 69725-4 如果有人能给我指出正确的方向,我将非常感谢你的帮助 __author__ = 'admin' class DoublyLinkedNode: """ A single node in a doubly-linked list. Con
如果有人能给我指出正确的方向,我将非常感谢你的帮助
__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()
除了缩进之外,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
缩进在python中很重要。也许不是,但是我们怎么知道你是否发布了错误的代码?@juanchopanza,缩进现在看起来怎么样了?几乎可以了。不确定
main()
定义和if\uuuuuu name\uuuuuu
是否应该在类的作用域内。@juanchopanza ok进行了编辑以修复该问题,非常有用,谢谢。我没有意识到链接必须与上一个和下一个节点连接。如果可以解决您的问题,请毫不犹豫地接受我的回答