스택관련 질문

조회수 404회

Stop 함수를 실행하면 가장 마지막에 들어간 데이터가 보여져야하는데 나오질 않네요ㅠㅠ 고수님들 무엇인 문제인지 아시나요?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define TRUE 1
#define FALSE 0  
#define STACK_LEN 10001


typedef struct _Stack
{
    int arr[STACK_LEN];
    int topIndex;
}  ST;

void SstackInit(ST *pstack)
{
    pstack->topIndex = -1;
}

void Spush(ST *pstack, int X)
{
    pstack->topIndex += 1;
    pstack->arr[pstack->topIndex] = X;
}

int Sempty(ST *pstack)
{
    if (pstack->topIndex == -1)
        return TRUE;
    else
        return FALSE;
}

int Spop(ST *pstack)
{
    int imsi;

    if (Sempty(pstack))
    {
        printf("error!");
        return -1;
    }

    imsi = pstack->topIndex;
    pstack->topIndex -= 1;
    return pstack->arr[imsi];

}   

int Ssize(ST *pstack)
{   
    return pstack->topIndex + 1;     //-1부터 시작이니깐

}

int Stop(ST *pstack)
{
    if (Sempty(pstack))
    {
        return -1;
    }

    return pstack->arr[pstack->topIndex];
}


int   main()
{
    ST stack;
    char str[10];
    int N = 0, i, num;


    scanf("%d", &N);

    SstackInit(&stack);

    for (i = 0; i < N; i++)
    {
        scanf("%s", str);


        if (!strcmp(str, "push"))
        {
            scanf("%d", &num);

            Spush(&stack, num);
        }

        else if (!strcmp(str, "pop"))
        {
            printf("%d\n", Spop(&stack));
        }


        else if (!strcmp(str, "size"))
        {
            printf("%d\n", Ssize(&stack));
        }


        else if (!strcmp(str, "empty"))
        {
            printf("%d\n",Sempty(&stack));
        }

        else if (!strcmp(str, "top"))
        {
            printf("%d\n", Stop(&stack));
        }



    }

    return 0;
}
  • 디버깅 공부를 해볼 수 있는 좋은 기회입니다. 이걸 타인에게 부탁을 하면 학습할 기회도 놓치는 겁니다. ide을 사용한다면 ide에 내장된 디버거를 이용할 수 있고 원격지에서 개발한다면 gdb는 설치되어 있을 겁니다. 디버거 튜터리얼을 한번 보고 그냥 단순하게 라인단위로 트레이스 해보는 겁니다. 라인단위로 트레이스 하면서 변하는 변수값을 확인하세요. 그렇게 실력이 향상되는 것이지 이런식의 모르겠으니 알려주세요 식은 질문자께 하나도 도움이 안됩니다. 정영훈 2019.11.30 01:01

1 답변

  • 코드가 실행이 잘되는군요. 다만 else if (!strcmp(str, "top")) { printf("%d\n", Stop(&stack)); }에서 "top" 가 "stop"으로 바뀌면 딱이겠군요.

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)