在链表末尾连续插入多个三元组
时间: 2023-07-12 21:21:42 浏览: 46
好的,我理解了你的问题。要在链表末尾插入多个三元组,你需要先定义一个三元组的数据结构,然后遍历链表,找到末尾节点,将新的三元组插入到末尾节点后面即可。
以下是一个示例代码,假设你的三元组数据结构为 `Triple`,链表节点数据结构为 `Node`:
```python
class Triple:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
class Node:
def __init__(self, data):
self.data = data
self.next = None
def insert_triples_at_end(head, triples):
# 遍历链表,找到末尾节点
curr = head
while curr.next != None:
curr = curr.next
# 将多个三元组插入到末尾节点后面
for triple in triples:
new_node = Node(triple)
curr.next = new_node
curr = new_node
```
你可以调用这个函数来在链表末尾插入多个三元组。例如:
```python
head = Node(None) # 头节点为空
triples = [Triple(1,2,3), Triple(4,5,6), Triple(7,8,9)]
insert_triples_at_end(head, triples)
```
这样,链表中就会依次插入三元组 `(1,2,3)`、`(4,5,6)` 和 `(7,8,9)`。