用c语言写一个最基本的链表
时间: 2023-03-25 12:01:08 浏览: 58
可以使用以下代码来实现一个最基本的链表:
```
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->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 ;
}
```
这个程序创建了一个包含三个节点的链表,每个节点包含一个整数和一个指向下一个节点的指针。然后,它遍历链表并打印每个节点的数据。