C ++ Linked List结构被引用为Double Pointer,需要访问Next Node

时间:2012-08-26 00:26:29

标签: c++ pointers linked-list double-pointer

好的,所以我设置了一个Linked List结构:

struct ListNode {
    ListNode* next;
    int data;
    ListNode(int in) {
        data = in;
        next = NULL;
    }
    ListNode(int in, ListNode* n) {
        data = in;
        next = n;
    }
};

连同插入功能:

bool insertNode(ListNode **head, int position, int data) {
    if (position == 0) {
        ListNode *element = new ListNode(data, *head->next);
        *head->next = element;
        return true;
    }
    else if (head == NULL)
        return false;
    else {
        insertNode(head->next, position-1, data);
    }
}

我如何访问头部的下一个元素?使用当前的代码,我收到此错误消息:

request for member ‘next’ in ‘* head’, which is of non-class type ‘ListNode*’

1 个答案:

答案 0 :(得分:4)

这应该可以解决问题

(*head)->next

编辑:有关运营商优先级http://en.cppreference.com/w/cpp/language/operator_precedence

的更多信息