我正在尝试实现堆栈。我有以下堆栈结构:
struct stackNode
{
char data;
struct stackNode *nextPtr;
};
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;
当我尝试将此用于我的pop方法时,我收到了许多错误消息。我的pop方法是:
char pop(StackNodePtr *topPtr )
{
if (IsEmpty(topPtr))
{
printf("Can't pop element from stack: stack is empty.\n");
return 'n'; // arbitrary char to end if, will adjust this later.
}
char c = topPtr->data; //save data to be returned
// temporary StructNodePtr to save data
StackNodePtr temp; // temporary StackNodePtr
temp = malloc(sizeof(StackNodePtr));
temp->data = topPtr->data; //line 52, first error
temp->nextPtr = topPtr->nextPtr;
//replace values in topPtr, this section I have yet to debug, is likely faulty.
topPtr->data = temp->nextPtr->data; //line 56, third error
topPtr->nextPtr = temp->nextPtr;
free(temp);
return (c);
}
我收到以下错误消息:
52:22: error: request for member ‘data’ in something not a structure or union
53:25: error: request for member ‘nextPtr’ in something not a structure or union
56:10: error: request for member ‘data’ in something not a structure or union
57:10: error: request for member ‘nextPtr’ in something not a structure or union
如果我将一个StackNode设为temp(并相应地调整 - >。),我会收到错误"request for member ‘nextPtr’ or ’data’ in something not a structure or union"
。在给我的问题中,topPtr
必须是StackNodePtr
。
有人可以帮我解决这个问题吗?
答案 0 :(得分:2)
您的topPtr
是指向struct(StackNodePtr *topPtr
= struct stackNode **topPtr
)指针的指针。所以你应该写(*topPtr) -> data
而不是topPtr -> data
。
实际上,行char c = topPtr->data;
也应该导致错误。