我应该如何在C中打印StackArray?
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct
{
void** stackAry;
int count;
int stackMax;
int top;
}STACK;
STACK* createStack(int maxSize)
{
STACK* stack;
stack = (STACK*)malloc(sizeof(STACK));
if(!stack)
return NULL;
stack->count = 0;
stack->top = -1;
stack->stackMax = maxSize;
stack->stackAry = (void**)calloc(stack->stackMax, sizeof(void*));
if(!stack->stackAry)
{
free(stack);
return NULL;
}
return stack;
}
bool pushStack(STACK* stack, void* itemPtr)
{
if(stack->count == stack->stackMax)
return false;
(stack->count)++;
(stack->top)++;
stack->stackAry[stack->top] = itemPtr;
return true;
}
void* popStack(STACK* stack)
{
void* dataPtrOut;
if(stack->count == 0)
dataPtrOut = NULL;
else
{
dataPtrOut = stack->stackAry[stack->top];
(stack->count)--;
(stack->top)--;
}
return dataPtrOut;
}
void* stackTop(STACK* stack)
{
if(stack->count == 0)
return NULL;
else
return stack->stackAry[stack->top];
}
bool emptyStack(STACK* stack)
{
return(stack->top == -1);
}
bool fullStack(STACK* stack)
{
return (stack->count == stack->stackMax);
}
int stackCount(STACK* stack)
{
return stack->count;
}
STACK* destroyStack(STACK* stack)
{
int i;
if(stack)
{
for(i=0 ; i<stack->count ; i++)
free(stack->stackAry[i]);
free(stack->stackAry);
free(stack);
}
return NULL;
}
int main(void)
{
STACK* myStack = createStack(100);
double* ptr;
while(1)
{
ptr = (double*)malloc(sizeof(double));
if(ptr == NULL)
return 0;
else
{
printf("Enter a positive real number: <-1> to stop: \n");
scanf("%lf", ptr);
if(*ptr == -1.0 || fullStack(myStack))
{
break;
}
else
pushStack(myStack, ptr);
}
}
printf("\n\nThe list of numbers reversed: \n");
while(!emptyStack(myStack))
{
ptr = (double*)popStack(myStack);
printf("%lf\n", *ptr);
free(ptr);
}
myStack = destroyStack(myStack);
return 0;
}
当我运行stackAry时,主要部分无法打印。 堆栈部分似乎没有问题,但是如果您打印它,则不会打印“输入正实数:<-1>来停止:”。如果您在黑屏上输入数字并按-1,将被打印。 我不确定哪一部分不对。请帮助我