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

0

How do I show my vector without the numbers that were removed? Because when I show him, even removing 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);  

    }
    
asked by anonymous 16.08.2018 / 03:23

1 answer

1

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

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

Note that here I used fim instead of MAX .

    
16.08.2018 / 03:31