class Node1: def init(self, data): self.data = data self.next = None self.prev = None class DoublyLinkedList: def init(self): self.head = None def append(self, data): new_node = Node1(data) if self.head is None: self.head = new_node else: cur = self.head while cur.next: cur = cur.next cur.next = new_node new_node.prev = cur def prepend(self, data): new_node = Node1(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 print_list(self): cur = self.head while cur: print(" <- ", end="") print(cur.data, end="") print(" -> ", end="") cur = cur.next print("None")
时间: 2024-03-11 17:45:23 浏览: 148
这段代码实现了一个双向链表,其中包括一个节点类 Node1 和双向链表类 DoublyLinkedList。节点类中包含数据和前继节点、后继节点。双向链表类中包含头指针 head 和三个方法:append、prepend 和 print_list。
- append 方法:在链表末尾添加节点。如果链表为空,则将头指针指向新节点;否则,遍历链表找到末尾节点,将其后继节点指向新节点,并将新节点的前继节点指向末尾节点。
- prepend 方法:在链表头部添加节点。如果链表为空,则将头指针指向新节点;否则,将新节点的后继节点指向头节点,将头节点的前继节点指向新节点,并将头指针指向新节点。
- print_list 方法:遍历链表并打印每个节点的数据,其中箭头表示节点间的前后关系,None 表示链表尾部的空指针。
相关问题
检查代码: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
这段代码定义了两个类,一个是Node类,一个是DoublyLinkedlist类。Node类有三个属性,分别是data、prev和next,其中data表示节点存储的数据,prev表示前一个节点,next表示后一个节点。DoublyLinkedlist类是双向链表类,用于存储Node节点。
检查代码并修改: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
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
阅读全文