#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
int main() {
struct Node *head = NULL;
struct Node *newNode = NULL;
struct Node *current = NULL;
// 创建第一个节点
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = 1;
newNode->next = NULL;
head = newNode;
current = head;
// 创建第二个节点
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = 2;
newNode->next = NULL;
current->next = newNode;
current = current->next;
// 创建第三个节点
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = 3;
newNode->next = NULL;
current->next = newNode;
// 打印链表数据验证
current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
// 释放链表内存
current = head;
struct Node *temp = NULL;
while (current != NULL) {
temp = current;
current = current->next;
free(temp);
}
return 0;
}