#include <stdlib.h>
#include <stdio.h>
#define SIZE 100

typedef struct
{
    int top;
    int data[SIZE];

}Stacktype;

void init(Stacktype *S)
{
    S -> top = -1;
}

int isFull(Stacktype *S)
{
    if(SIZE - 1 == S->top)
    {
        printf("is full!");
        return 1;
    }
    else
    {
        return 0;
    }
    
}

int isEmpty(Stacktype *S)
{
    if(S->top == -1)
    {
        printf("is Empty!");
        return 1;
    }
    else
    {
        return 0;
    }
}

int push(Stacktype *S, int e)
{
    if(isFull(S))
        return -1;
    else
        S->data[++S->top] = e; 
        return 0;
}

int pop(Stacktype *S)
{
    if(isEmpty(S))
        return -1;
    else
        printf("Pop : %d\n",S->data[S->top]);
        return S->data[S->top--];
}

int peek(Stacktype *S)
{
    if(isEmpty(S))
        return -1;
    else 
        printf("Peek : %d\n", S->data[S->top]);
        return S->data[S->top];
}

int print(Stacktype *S)
{
    for(int i = 0; i <= S->top; i++)
    {
        printf("%d ",S->data[i]);
    }
    printf("\n");
    return 0;
}

int main()
{
    Stacktype S;
    init(&S);

    push(&S, 0);
    push(&S, 5);
    push(&S, 1);
    push(&S, 9);
    peek(&S);
    pop(&S);

    print(&S);
    
    return 0;
}

댓글남기기