用C语言建立十个节点的单链表
时间: 2023-05-21 09:03:01 浏览: 95
建立单链表-数据结构C语言版教材讲义
好的,以下是用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;
}
```
希望这个代码能够帮到你!
阅读全文