もっとコードが必要ですが、試してみます:
この関数を次のように使用すると思います。
char pop(Node* top)
{
char result ;
if (size == 0) // The stack is empty.
{
return '\0' ;
}
else //The stack contains at least one element .
{
result = top->opp ;
top = top->next ;
}
size-- ;
return result;
}
int main()
{
// this is the top of the stack
Node *stack; // let's say there already are some elements in this stack
pop(stack);
return 0;
}
問題は、ポインター値を変更したい場合stack
、スタックの一番上を指すことです。そのためには、次のようにポインターへのポインターを使用する必要があります。
char pop(Node** top)
{
char result ;
Node *ptr;
ptr = *top;
if (size == 0) // The stack is empty.
{
return '\0' ;
}
else //The stack contains at least one element .
{
result = (*top)->opp ;
(*top) = (*top)->next ;
// You should free the pointer, like user2320537 said in his answer.
free(ptr);
}
size-- ;
return result;
}
int main()
{
// this is the top of the stack
Node *stack; // let's say there already are some elements in this stack
pop(&stack); // You give the pointer address
return 0;
}