我需要将一个 int 或一个字符串传递给堆栈的推送函数。通常我只会重载该函数,并有一个接受字符串参数和一个接受 int 参数的函数,以便仅根据参数调用适当的函数。我在评论中写了我通常会包含该类型的位置。我只是被困在那里。
void push(Stack *S, /* int or a string */ element)
{
/* If the stack is full, we cannot push an element into it as there is no space for it.*/
if(S->size == S->capacity)
{
printf("Stack is Full\n");
}
else
{
/* Push an element on the top of it and increase its size by one*/
if (/* element is an int*/)
S->elements[S->size++] = element;
else if (/* element is a string */)
S->charElements[S->size++] = element;
}
return;
}