0

好的,所以我设置了一个链接列表结构:

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);
    }
}

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

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

1 回答 1

4

这应该可以解决问题

(*head)->next

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

于 2012-08-26T00:32:22.160 回答