c++单向链表有一个表头节点没有尾节点,并且只有一个指向表头的指针,写一个类
时间: 2024-06-06 12:07:02 浏览: 106
class LinkedList:
def __init__(self):
self.head = Node(None)
def add(self, data):
new_node = Node(data)
new_node.next_node = self.head.next_node
self.head.next_node = new_node
def remove(self, data):
current_node = self.head.next_node
previous_node = self.head
while current_node is not None:
if current_node.data == data:
previous_node.next_node = current_node.next_node
return True
previous_node = current_node
current_node = current_node.next_node
return False
def search(self, data):
current_node = self.head.next_node
while current_node is not None:
if current_node.data == data:
return True
current_node = current_node.next_node
return False
def print_list(self):
current_node = self.head.next_node
while current_node is not None:
print(current_node.data)
current_node = current_node.next_node
class Node:
def __init__(self, data):
self.data = data
self.next_node = None
阅读全文