Stacks with vectors in C, is showing the elements that have already been taken

How do I show my vector without the numbers that have been taken? For when I show it, even if I pull out a number in the function, it still appears inside.

#include <stdio.h>
#include <stdlib.h>
#define MAX 10

int pilha[MAX];
int inicio,fim;

int pilhaCheia(){
    return (fim == MAX);    
}
int pilhaVazia(){
    return (inicio == fim);
}
void push(int x){
    if( !pilhaCheia() ){
        pilha[fim++] = x;
    }else{
    printf("Pilha cheia \n");   
    }
}
int pop(){
    int aux;
    if( !pilhaVazia() ){
        aux=pilha[fim];
        fim--;
        return aux;

        }else{
            printf("Pilha vazia \n");
        return -1;  
        }
    }


void exibe(int pilha[MAX]){
    int x;
    for( x=0; x < MAX; x++){
        printf("%d",pilha[x]);
    }
}

main(){

    inicio = 0;
    fim = 0;
    int escolha,valor;
    do{
    printf("\n1 EMPILHA:\n");
    printf("\n2 DESEMPILHA:\n");
    printf("\n3 Mostra:\n");
    printf("\n4 Sair:\n");
    scanf("%d",&escolha);
    int x;
    switch(escolha){
        case 1:
            printf("Escolha o valor:");
            scanf("%d",&valor);
            push(valor);
            break;
            case 2:
            printf("%d",pop());
            break;
            case 3:
            exibe(pilha);   
            break;
            default:
            break;
    }



    }while( escolha != 4);  

    }
Author: Victor Stafusa, 2018-08-16

1 answers

Just change the condition of your for in the function exibe:

for( x=0; x < fim; x++){
    printf("%d",pilha[x]);
}

Note that here I used fim instead of MAX.

 1
Author: Victor Stafusa, 2018-08-16 01:31:14