c语言链表打印杨辉三角代码
时间: 2023-09-29 21:08:30 浏览: 92
打印杨辉三角形-C++教程(PPT)
好的,以下是 C 语言链表打印杨辉三角代码:
```
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
void printPascalTriangle(int numRows) {
Node *curr;
Node *prev = NULL;
for (int i = 1; i <= numRows; i++) {
curr = malloc(sizeof(Node));
if (prev) {
prev->next = curr;
}
curr->data = 1;
curr->next = NULL;
Node *temp = curr;
for (int j = 1; j < i; j++) {
Node *newNode = malloc(sizeof(Node));
newNode->data = temp->data * (i - j) / j;
temp->next = newNode;
temp = newNode;
}
while (curr) {
printf("%d ", curr->data);
Node *temp = curr;
curr = curr->next;
free(temp);
}
prev = NULL;
printf("\n");
}
}
int main() {
int numRows = 5;
printPascalTriangle(numRows);
return 0;
}
```
希望对你有帮助!现在,可以问我下一个问题了。
阅读全文