如何在C

时间:2018-02-11 14:46:20

标签: python c struct linked-list

我尝试使用Struct在C中创建LinkedList数据结构。下面我有两个例子,一个有效,另一个没有。

首先,python版本是我尝试用C(在python下面)重新创建的,是的,这是代码的和平工作。我试图让我的C版本工作,但我不确定它有什么问题。有人可以尝试解决我的问题吗?甚至提供更好的解决方案?提前谢谢!

Python版

class Node:
    pass

start = Node()
point = start

point.val = 1
point.next = Node()
point = point.next

point.val = 2
point.next = Node()
point = point.next

point.val = 3
point.next = None

尝试C

#include <stdio.h>

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

int main() {
    struct Node start;
    struct Node pointer = start;    
    int i;

    for (i = 0; i < 4; i++) {
        struct Node another;
        another.val = i;

        pointer.next = &another;

        pointer = *(pointer.next);      
    }   

    pointer = start;
    // Print out the value
    for (i = 0; i < 4; i++) {
        printf("%d\n", pointer.val);
        pointer = *(pointer.next);
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

$sql= $this->db->query($sql);
$data['brunches'] = $sql->result();