Stack does not remove values

Asked

Viewed 73 times

0

Hello, I am making a Stack (LIFO) and it does not replace the values by 0 when 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);
}
  • What is the question ?

2 answers

1

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

/* ****************************************************************** */
/* *                           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 */

Exit:

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’ve helped!

  • Thank you very much! It helped a lot!

0

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

Element1 <--- Element2 <--- Element3 <--- Root. Hugs.

  • It is an exercise of Faculty, matter of Data Structure, elements with 0 = Empty Position

  • Creates a function that starts all elements with 0, every push you search for the last element with 0 and replace the value, every pop takes the element before 0 found and Zera the value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.