C语言struct嵌套链表

146 阅读1分钟
#include <stdio.h>
#include <stdlib.h>

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

int main(void)
{

    // 生成一个三个节点的列表(11)->(22)->(33)
    struct node *head;
    head = (node *)malloc(sizeof(struct node));

    head->data = 11;
    head->next = (node *)malloc(sizeof(struct node));

    head->next->data = 22;
    head->next->next = (node *)malloc(sizeof(struct node));

    head->next->next->data = 33;
    head->next->next->next = NULL;

    // 遍历列表
    for (struct node *cur = head; cur != NULL; cur = cur->next)
    {
        printf("%d->", cur->data);
    }

    return 0;
}