检查代码并修改: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 21:03:54 浏览: 122
查看class代码
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList: # 类名应该是DoublyLinkedList而不是DoublyLinkedlist
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
current = self.head
while current.next is not None:
current = current.next
current.next = new_node
new_node.prev = current
def prepend(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
self.head.prev = new_node
new_node.next = self.head
self.head = new_node
def delete(self, data):
current = self.head
while current is not None:
if current.data == data:
if current.prev is not None:
current.prev.next = current.next
else:
self.head = current.next
if current.next is not None:
current.next.prev = current.prev
return
current = current.next
def print_list(self):
current = self.head
while current is not None:
print(current.data)
current = current.next
阅读全文