小编Sen*_*Sen的帖子

如何使用最少数量的赋值运算符构建OneTwoThree链表?

我在准备面试时遇到了这个问题,并且很想知道它可以写出的不同方式.我在http://cslibrary.stanford.edu/103/找到了这个,并且已经给出了问题.

这是构建列表{1,2,3}的代码

struct node* BuildOneTwoThree() {
    struct node* head = NULL;
    struct node* second = NULL;
    struct node* third = NULL;
    head = malloc(sizeof(struct node)); // allocate 3 nodes in the heap
    second = malloc(sizeof(struct node));
    third = malloc(sizeof(struct node));
    head->data = 1; // setup first node
    head->next = second; // note: pointer assignment rule
    second->data = 2; // setup second node
    second->next = third;
    third->data = 3; // setup third link
    third->next = NULL;
    // At this …
Run Code Online (Sandbox Code Playgroud)

c initialization

4
推荐指数
1
解决办法
2205
查看次数

标签 统计

c ×1

initialization ×1