class Node:
def __init__(self, value):
self.value = value
self.next = None
self.prev = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
self.length = 0
def append(self, value):
node = Node(value)
if self.head is None:
self.head = node
self.tail = node
else:
self.tail.next = node
node.prev = self.tail
self.length += 1
def print(self):
node = self.head
while node is not None:
print(node.value)
node = node.next
def search(self):
count = 0
if node.prev is not None and node.next is not None:
if node.prev.value < 0 and node.next.value < 0:
count += 1
return "Количество элементов, у которых соседние элементы справа и слева отрицательны: " + str(count)
if __name__ == '__main__':
list = LinkedList()
list.append(1)
list.append(2)
list.append(-3)
list.append(4)
list.append(-5)
list.append(6)
list.append(-7)
list.append(8)
list.append(-9)
list.append(10)
list.print()
print(list.search())
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Verified answer
class Node:
def __init__(self, value):
self.value = value
self.next = None
self.prev = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
self.length = 0
def append(self, value):
node = Node(value)
if self.head is None:
self.head = node
self.tail = node
else:
self.tail.next = node
node.prev = self.tail
self.tail = node
self.length += 1
def print(self):
node = self.head
while node is not None:
print(node.value)
node = node.next
def search(self):
node = self.head
count = 0
while node is not None:
if node.prev is not None and node.next is not None:
if node.prev.value < 0 and node.next.value < 0:
count += 1
node = node.next
return "Количество элементов, у которых соседние элементы справа и слева отрицательны: " + str(count)
if __name__ == '__main__':
list = LinkedList()
list.append(1)
list.append(2)
list.append(-3)
list.append(4)
list.append(-5)
list.append(6)
list.append(-7)
list.append(8)
list.append(-9)
list.append(10)
list.print()
print(list.search())