用C语言建立很有十个节点的单链表
时间: 2023-05-21 15:02:44 浏览: 101
可以使用以下代码建立一个有十个节点的单链表:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node* tail = NULL;
// 创建十个节点
for (int i = 1; i <= 10; i++) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = i;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
// 遍历链表并输出每个节点的值
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
return 0;
}
```
这段代码会创建一个有十个节点的单链表,并输出每个节点的值。
阅读全文