可以使用链表来存储多个双精度数。每个节点包含一个双精度数和指向下一个节点的指针。
下面是一个使用链表存储多个双精度数的示例代码:
class Node:
def __init__(self, value):
self.value = value
self.next = None
class DoubleLinkedList:
def __init__(self):
self.head = None
def add(self, value):
new_node = Node(value)
if self.head is None:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
def print_values(self):
current = self.head
while current:
print(current.value)
current = current.next
# 使用示例
list = DoubleLinkedList()
list.add(3.14)
list.add(2.718)
list.add(1.618)
list.print_values()
输出结果为:
3.14
2.718
1.618
这样,我们就可以使用链表来存储多个双精度数,而不需要使用数组。
下一篇:不使用数组的C语言中的矩阵