我想使用链表实现堆栈.
为了实现我的堆栈的pop(),我让调用者传递一个双指针(指针指针),它最终指向我的堆栈顶部(链表中的第一个条目).
我这样做的原因是因为这样调用者可以保持指向堆栈的静态指针.
我的链表元素结构:
struct Element {
int value;
struct Element *next;
};
Run Code Online (Sandbox Code Playgroud)
pop()实现:
int pop (struct Element **stack) {
int popped_value = *stack->value;
*stack = *stack->next;
return popped_value;
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是试图取消引用双指针**堆栈.此代码生成以下错误:
error: request for member ‘value’ in something not a structure
error: request for member ‘next’ in something not a structure
Run Code Online (Sandbox Code Playgroud)
在我看来,*stack-> value或**stack.value应该可以检索popped_value,但是我得到了相同的错误.