数据结构:创建一个含有10个结点的单链表
时间: 2023-05-21 09:03:01 浏览: 51
好的,这是一个创建含有10个结点的单链表的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createLinkedList(int n) {
struct Node *head = NULL, *temp = NULL, *newNode = NULL;
int i;
for (i = 1; i <= n; i++) {
newNode = (struct Node*)malloc(sizeof(struct Node));
printf("请输入第 %d 个结点的值: ", i);
scanf("%d", &newNode->data);
newNode->next = NULL;
if (head == NULL) {
head = newNode;
temp = newNode;
} else {
temp->next = newNode;
temp = temp->next;
}
}
return head;
}
int main() {
struct Node* head = NULL;
int n = 10;
head = createLinkedList(n);
return 0;
}
```
这个程序会提示用户输入10个结点的值,然后创建一个含有10个结点的单链表,并返回链表的头结点。