用c语言建立有10个结点的单链表
时间: 2023-05-20 18:05:50 浏览: 46
好的,以下是用 C 语言建立有 10 个结点的单链表的代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
struct Node* tail = NULL;
// 创建 10 个结点
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;
}
```
希望能对您有所帮助!