편집 기록

편집 기록
  • 프로필 buttercrab님의 편집
    날짜2019.11.30

    스택관련 질문


    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;
    }
    
  • 프로필 이태현님의 편집
    날짜2019.11.29

    스택관련 질문


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

    include

    include

    include

    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;
    

    }