Вот пример полной реализации класса DoublyLinkedList в Python:
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
def is_empty(self):
return self.head is None
def append(self, data):
new_node = Node(data)
if self.is_empty():
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
new_node.prev = current
def prepend(self, data):
new_node = Node(data)
if self.is_empty():
self.head = new_node
else:
new_node.next = self.head
self.head.prev = new_node
self.head = new_node
def delete(self, data):
if self.is_empty():
return
current = self.head
while current:
if current.data == data:
if current.prev:
current.prev.next = current.next
else:
self.head = current.next
if current.next:
current.next.prev = current.prev
return
current = current.next
def search(self, data):
current = self.head
while current:
if current.data == data:
return True
current = current.next
return False
def display(self):
elements = []
current = self.head
while current:
elements.append(current.data)
current = current.next
return elements
Эта реализация включает следующие методы:
is_empty()
: проверяет, пуст ли двусвязный список.append(data)
: добавляет новый узел с заданными данными в конец двусвязного списка.prepend(data)
: добавляет новый узел с заданными данными в начало двусвязного списка.delete(data)
: удаляет первое вхождение узла с заданными данными из двусвязного списка.search(data)
: ищет узел с заданными данными в двусвязном списке и возвращаетTrue
, если найден, иначеFalse
.display()
: возвращает список всех элементов двусвязного списка.
Обратите внимание, что предоставленная реализация является базовым примером, и могут быть добавлены дополнительные методы и функции в зависимости от конкретных требований.