Stack does not remove values

0

Hello, I'm doing a Stack (LIFO) and it does not replace the values by 0 at the time of removing the values from the stack. This was a way I found to "remove" the data from it and the display is shown with the values "1.9".

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

#define TAMANHO_MAX_PILHA 10

typedef struct {
  int totalElementos;
  float pilha[TAMANHO_MAX_PILHA];
} PILHA;

PILHA *CriarPilha() {
  PILHA *ppilha;
  ppilha = (PILHA *) malloc(sizeof(PILHA));
  ppilha->totalElementos = 0;
  return ppilha;
}

void Listar(PILHA *ppilha) {
  int totalElementos = ppilha->totalElementos;
  int indiceTopoPilha = totalElementos - 1;
  for (int indiceVetor = indiceTopoPilha; indiceVetor >= 0 ; indiceVetor--) {
    float  elemento = ppilha->pilha[indiceVetor];
    printf("Posição %i = %f \n", indiceVetor + 1, elemento);
  }
}

float Push(PILHA *ppilha, float novoElemento) {
  int totalElementos = ppilha->totalElementos;
  if(totalElementos == TAMANHO_MAX_PILHA){
    printf("Pilha Cheia");
  } else {
      for (int indiceVetor = totalElementos; indiceVetor <= TAMANHO_MAX_PILHA - 1; indiceVetor++) {
        ppilha->pilha[indiceVetor] = novoElemento;
        ppilha->totalElementos++;
      }
    }
}

float Pop(PILHA *ppilha) {
  int totalElementos = ppilha->totalElementos;
  int indiceTopoPilha = totalElementos - 1;
  if(totalElementos != 0) {
  for (int indiceVetor = indiceTopoPilha; indiceVetor < 0 ; indiceVetor--) {
      ppilha->pilha[indiceVetor] = 0.5;
      ppilha->totalElementos--;
    }
  } else {
      printf("Pilha Vazia");
    }
  }

void main() {
  PILHA *ppilha;
  float novoElemento = 1.9;

  ppilha = CriarPilha();
  Push(ppilha, novoElemento);
  Listar(ppilha);
  printf("-------------------------------------------------------------------\n");
  Pop(ppilha);
  Listar(ppilha);
}
    
asked by anonymous 06.09.2016 / 15:09

2 answers

1

Following its rationale, follows a simplified (and tested) code that implements a LIFO stack of float values.

/* ****************************************************************** */
/* *                           pilha_float.c                        * */
/* ****************************************************************** */

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


#define PILHA_MAX_TAM        (10)

#define SUCESSO              (0)
#define ERRO_PILHA_CHEIA     (-1)
#define ERRO_PILHA_VAZIA     (-2)


typedef struct pilha_s pilha_t;

struct pilha_s
{
    float item[ PILHA_MAX_TAM ];
    int qtd;
};


void pilha_inicializar( pilha_t * p )
{
    memset( p, 0, sizeof(pilha_t) );
}


int pilha_push_item( pilha_t * p, float n )
{
    if( p->qtd == PILHA_MAX_TAM )
        return ERRO_PILHA_CHEIA;

    p->item[ p->qtd++ ] = n;

    return SUCESSO;
}


float pilha_pop_item( pilha_t * p )
{
    if( p->qtd == 0 )
        return ERRO_PILHA_VAZIA;

    return p->item[ --p->qtd ];
}


void pilha_listar_itens( pilha_t * p )
{
    int i = 0;

    if( !p->qtd )
    {
        printf("Pilha Vazia!\n");
        return;
    }

    printf( "Pilha:\n" );

    for( i = p->qtd - 1; i >= 0; i-- )
        printf( "   Posicao %d / Valor: %f\n", i, p->item[i] );
}


int main( int argc, char * argv[] )
{
    pilha_t p;

    pilha_inicializar( &p );

    /* Empilha 4 Constantes Matematicas... */

    pilha_push_item( &p, 3.14159 ); /* Push PI */
    pilha_push_item( &p, 2.71828 ); /* Push Euler's Number */
    pilha_push_item( &p, 1.61803 ); /* Push Golden Ratio */
    pilha_push_item( &p, 0.66274 ); /* Push Laplace Limit */

    pilha_listar_itens( &p );

    /* Desempilha apenas 2 constantes... */

    pilha_pop_item( &p ); /* Pop Laplace Limit */
    pilha_pop_item( &p ); /* Pop Golden Ratio */

    pilha_listar_itens( &p );

    return 0;
}

/* fim-de-arquivo */

Output:

Pilha:
   Posicao 3 / Valor: 0.662740
   Posicao 2 / Valor: 1.618030
   Posicao 1 / Valor: 2.718280
   Posicao 0 / Valor: 3.141590
Pilha:
   Posicao 1 / Valor: 2.718280
   Posicao 0 / Valor: 3.141590

I hope I have helped!

    
06.09.2016 / 19:58
0

Look, because of your code it's hard to have to tinker with it all to do Last In, First Out, I think if you put a marker on the struct to know if it was given a push or pop, maybe it helps to know who is the last element of the vector. A more elegant solution would be to make a list chained, where the root of the list has the pointer to last element, when pushing, you put this as the last of the root and as previous the last, when you do the pop, put the previous as the last of the root and free in it.

Element1

06.09.2016 / 15:29