帮我写一个c语言的链表
时间: 2023-03-26 17:03:00 浏览: 67
好的,我可以帮您写一个C语言的链表。链表是一种数据结构,它由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。以下是一个简单的链表示例:
```
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}
int main() {
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
printList(head);
return ;
}
```
这个程序创建了一个包含三个节点的链表,每个节点包含一个整数数据元素和一个指向下一个节点的指针。printList函数用于遍历链表并打印每个节点的数据元素。