创建一个由六个节点的单向链表
时间: 2023-11-13 12:05:24 浏览: 27
好的,以下是创建一个由六个节点的单向链表的代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *createList(int *arr, int size) {
Node *head = NULL;
Node *tail = NULL;
for (int i = 0; i < size; i++) {
Node *node = (Node *)malloc(sizeof(Node));
node->data = arr[i];
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}
return head;
}
void printList(Node *head) {
Node *p = head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6};
int size = sizeof(arr) / sizeof(int);
Node *head = createList(arr, size);
printList(head);
return 0;
}
```
以上就是创建一个由六个节点的单向链表的代码。