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

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

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.