스택 출력에 대해 문의드립니다.

vellus의 이미지

#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;
}

코드에 오류는 없다고 하는데 실행을 하면 "Enter a positive real number: <-1> to stop: \n" 내용이 출력되야하는데 공백화면이 뜹니다. 공백화면에다가 아무 숫자(3.2, 1,1 5.4) 넣고 -1을 입력해주면 그제야 Enter a positive real number: <-1> to stop: \n 이 문구가 3줄 뜨고 입력값이 5.4 1.1 3.2 순으로 뜹니다. 디버깅 해봣는데 STACK* myStack = createStack(100); 이부분에서 걸리던데 잘 모르겟습니다. 좀 도와주세요
익명 사용자의 이미지

잘 되는데요

root@ip-172-31-5-82:~/test# ./a 
Enter a positive real number: <-1> to stop: 
3.2
Enter a positive real number: <-1> to stop: 
1
Enter a positive real number: <-1> to stop: 
1
Enter a positive real number: <-1> to stop: 
4.2
Enter a positive real number: <-1> to stop: 
33.2
Enter a positive real number: <-1> to stop: 
-1
 
 
The list of numbers reversed: 
33.200000
4.200000
1.000000
1.000000
3.200000