C语言 单向链表,如何返回指向新节点的指针


struct nodeStruct {
    int value;
    struct nodeStruct *next;
}
struct nodeStruct* List_createNode(int item) {
    nodeStruct* tempNode;
    tempNode->value = item;
    nodeStruct* node = malloc ( sizeof(tempNode) );
    return nodeStruct;
}

这就是我目前所拥有的,我将如何为新节点分配内存并初始化值,然后返回指向它的指针?不确定我是否有正确的方法。

校正:

struct nodeStruct* List_createNode(int item) {
    struct nodeStruct node* = (struct nodeStruct *) malloc(sizeof(struct nodeStruct));
    if (node == NULL) {return NULL;}
    node->value = item;
    return node;
}

这会返回指向新节点的指针吗?

struct nodeStruct* List_createNode(int item) {
    struct nodeStruct node* = (struct nodeStruct *) malloc(sizeof(struct nodeStruct));
    if (node == NULL) {return NULL;}
    node->value = item;
    node->next=*head;  head is the current state pointer
    head=node;
    return head;
}

这应该有效

相关内容

  • 没有找到相关文章