如何在C中获取指针指针?

时间:2015-11-13 09:55:23

标签: c pointers linked-list

我应该编写一个函数来删除链表中的第一个节点。 List的定义如下:

struct ListNode{
    int nInfo;
    struct ListNode *next;
};

struct ListNode *createNode(int nInfo) {
    ListNode *node;
    node->nInfo = nInfo;
    node->next = NULL;
    return node;
}

void insertNode(struct ListNode **list, struct ListNode *node) {
    //Sorting list after nInfo
    struct ListNode *temp;
    struct ListNode *tmpList = *list;
    if(*list != NULL) { //List exists
        while((tmpList->next != NULL)) {
            if((tmpList->nInfo >= node->nInfo) && (tmpList->next->nInfo < node->nInfo)) {
                break;
            }
            tmpList = tmpList->next;
        }
        //Found where to insert the node
        temp = tmpList->next;   //Saving old nextnode
        tmpList->next = node;   //Assigning new nextnode
        node->next = temp;  //Re-inserting old node
    }
    else{
        *list = node;
    }
}

删除第一个节点的功能如下所示:

void deleteFirst(struct ListNode **list) {
    //Delete first node
    struct ListNode *temppointer = *list;
    if(temppointer == NULL)
        return; //List is NULL
    *list = temppointer->next;
}

我使用像这样的函数:

struct ListNode *list = createNode(100);
struct ListNode *node1 = createNode(50);
insertNode(list, node1); //Gives error, cannot convert ListNode* to ListNode**
deleteFirst(list); //Same error 

我无法弄清楚如何获得指向列表指针的指针。

4 个答案:

答案 0 :(得分:0)

注意!您为创建节点而编写的函数不能按原样工作:节点在函数的上下文中在堆栈上分配,并且在函数退出后无效。

您必须使用(p.e.)malloc为堆上的节点分配内存。从列表中删除节点的功能对其解除分配负责,通常使用free。

答案 1 :(得分:0)

正如我们所怀疑的那样,您忘记为节点分配内存:

struct ListNode *createNode(int nInfo) {
    ListNode *node;
    node->nInfo = nInfo;
    node->next = NULL;
    return node;
}

你的*node是一个指针,但它仍然指向什么。您必须向堆请求节点的内存:

    ListNode *node = malloc(sizeof(ListNode));

然后在DeleteNode中,您必须将内存返回到堆,因为您不再需要它:

void deleteFirst(struct ListNode **list) {
    //Delete first node
    struct ListNode *temppointer = *list;
    if(temppointer == NULL)
        return; //List is NULL
    *list = temppointer->next;
    free(temppointer);    // release the memory.
}

答案 2 :(得分:0)

[发布作为获得格式正确的答案] 注意:您的insert()函数过于复杂。它可以简化为

void insertNode(struct ListNode **list, struct ListNode *node) {
    for( ; *list ; list = &(*list)->next ) { //List exists
        if(*(list)->nInfo >= node->nInfo) break;
        }
    //Found where to insert the node
    node->next = *list;
    *list = node;
}

答案 3 :(得分:0)

#include <stdlib.h>

struct ListNode{
    int nInfo;
    struct ListNode *next;
};

struct ListNode *createNode(int nInfo) {
    struct ListNode *node=malloc(sizeof(*node));
    if(node){
        node->nInfo = nInfo;
        node->next = NULL;
    }
    return node;
}

void insertNode(struct ListNode **list, struct ListNode *node) {

    // for safety
    if(!list) return;
    if(!node) return;


    //Sorting list after nInfo

    struct ListNode *temp;
    struct ListNode *tmpList = *list;

    if(tmpList!= NULL) { //List exists
        while(tmpList->next) {

            if( 
                ((tmpList->nInfo)>= (node->nInfo))  
                && 
                ((tmpList->next->nInfo) < (node->nInfo))
              ) {
                break;
            }
            tmpList = tmpList->next;
        }
        //Found where to insert the node
        temp = tmpList->next;   //Saving old nextnode
        tmpList->next = node;   //Assigning new nextnode
        node->next = temp;  //Re-inserting old node
    }
    else{
        *list = node;
    }
}


void deleteFirst(struct ListNode **plist) {
    if(!plist) return;

    struct ListNode *list=*plist;
    if(!list) return;

    *plist=list->next;
    free(list);
    return ;

}


void printNodes(char *title,struct  ListNode *list){
    printf("\n== %s\n",title);
    while(list){
        printf("\t%d\n",list->nInfo);
        list=list->next;
    }
    printf("\n");
}
int main(){
    struct ListNode *list = createNode(100);
    struct ListNode *node1 = createNode(50);
    insertNode(&list, node1); 
    printNodes("on start",list);

    insertNode(&list, createNode(70));
    printNodes("after add 70",list);

    deleteFirst(&list); 
    printNodes("after del first",list);

}