#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

int main() {
    struct Node* head = NULL;
    struct Node* node1 = NULL;
    struct Node* node2 = NULL;
    struct Node* node3 = NULL;
    
    // 创建头节点
    head = (struct Node*)malloc(sizeof(struct Node));
    head->data = 0;
    head->next = NULL;
    
    // 创建 node1
    node1 = (struct Node*)malloc(sizeof(struct Node));
    node1->data = 0;
    node1->next = NULL;
    head->next = node1;
    
    // 创建 node2
    node2 = (struct Node*)malloc(sizeof(struct Node));
    node2->data = 0;
    node2->next = NULL;
    node1->next = node2;
    
    // 创建 node3
    node3 = (struct Node*)malloc(sizeof(struct Node));
    node3->data = 0;
    node3->next = NULL;
    node2->next = node3;
    
    printf('Created a linked list with 4 nodes.\n');
    
    return 0;
}
C语言创建长度为4的空单链表 - 代码示例

原文地址: https://www.cveoy.top/t/topic/nwj8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录