检查代码并修改:class Node: def init(self, data): self.data = data self.prev = None self.next = None Class DoublyLinkedList: # def init(self) self.head = None def remove(self,item): current = self.head previous = None found = False while not found: if current.getData() == item found = True else: previous = current current = current.getNext() if previous == None: self.head = current.getNext() current.prev = None else: previous.setNext(current.getNext()) current.prev = previous
时间: 2023-05-20 16:03:55 浏览: 101
单链表双链表python示例.rar
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
def add_node(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
self.tail = new_node
else:
new_node.prev = self.tail
self.tail.next = new_node
self.tail = new_node
def remove_node(self, node):
if node == self.head:
self.head = node.next
self.head.prev = None
elif node == self.tail:
self.tail = node.prev
self.tail.next = None
else:
node.prev.next = node.next
node.next.prev = node.prev
def print_list(self):
current_node = self.head
while current_node:
print(current_node.data)
current_node = current_node.next
阅读全文