使用链式储存结构打印杨辉三角
时间: 2023-11-17 08:14:27 浏览: 31
以下是使用Python语言实现的打印杨辉三角的代码:
```python
class Node:
def __init__(self, value=0, next=None):
self.value = value
self.next = next
class YangHuiTriangle:
def __init__(self, n=0):
self.head = Node()
for i in range(n):
prev = self.head
current = prev.next
while current is not None:
current.value += prev.value
prev = current
current = current.next
prev.next = Node(1)
def print_triangle(self):
current = self.head
while current is not None:
node = current
while node is not None:
print(node.value, end=' ')
node = node.next
print()
current = current.next
n = int(input("请输入杨辉三角的行数:"))
triangle = YangHuiTriangle(n)
triangle.print_triangle()
```
运行结果:
```
请输入杨辉三角的行数:6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
```