Problem decreasing the size of a stack in c

This program is an exercise to work with stacks, it's all right, but when I decrease stack size to delete the last term it appears in the penultimate element of the vector.

#include <stdio.h>
#include <windows.h>

typedef struct viagem {
  char nome[50];
  char uf[2];
}v;

typedef struct pilha{
  int topo;
  int distancia;
  int visitadas;
  v cidades[10];
}p;

void empilhar (p *pilha) {
  int dist;
  system("cls");
  pilha->topo++;
  printf("Cidade destino: " );
  scanf("%[^\n]s",&pilha->cidades[pilha->topo].nome );
  printf("UF: " );
  scanf("%s",&pilha->cidades[pilha->topo].uf );
  printf("Distancia: " );
  scanf("%i",&dist);
  pilha->visitadas++;
  pilha->distancia=pilha->distancia+dist;
}
void desempilhar (p *pilha){
  pilha->topo--;
}
int main (){
  p pilha;
  pilha.topo=-1;
  pilha.visitadas = 0;
  pilha.distancia = 0;
  int flag=1;
  int opc;
  do {
    system ("cls");
    if (pilha.topo == -1) {
      printf("Voce esta em Maringa-Pr !");
    }
    else {
      printf("Voce esta em %s-%s",pilha.cidades[pilha.topo].nome,pilha.cidades[pilha.topo].uf);
    }
    printf("\nVoce visitou %i cidades", pilha.visitadas );
    printf("\nVoce percoreu %ikm\n", pilha.distancia );
    printf("\n1-Avancar");
    printf("\n2-Voltar");
    printf("\n3-Sair");
    printf("\nopcao: " );
    scanf("%i%*c",&opc );
    switch (opc) {
      case 1:
        empilhar (&pilha);
      break;
      case 2:
        desempilhar (&pilha);
      break;
      case 3:
        flag=0;
      break;
    }
  } while(flag);
  printf("Programa finalizado ! " );
  scanf("%*i",&opc);
}
 0
Author: zentrunix, 2019-04-22

2 answers

Problem 1:

typedef struct viagem {
  char nome[50];
  // char uf[2]; // <-------------- ERRO
  char uf[3];
} v;

Problem 2:

void desempilhar (p *pilha){
  if (pilha->topo >= 0)  // <------- FALTA
    pilha->topo--;
}

And @ V. Santos 'observation is correct: distance and" visited " should be part of each element of the stack, not just a value for the entire stack.

 0
Author: zentrunix, 2019-04-23 01:32:10

Good afternoon Wingeds,

I believe there is a conceptual problem in your stack, which by definition are Data Structures LIFO (Last in, first out ). They can be implemented, among other ways, through dynamic vectors and chained lists.

In the case you presented what I could notice is that you are using the instance of a TAD to store data in it, but that conceptually is not a stack.

illustration of a stack below

insert the description of the image here

Returning to your problem:

The insert (Push) and remove (Pop) operations in the stack will vary depending on the model you are using. In the case of vectors the Pop is through a "logical Delete", which is about returning a position at the" top " of the stack, without necessarily removing the value, and the Push will be overwriting these previous values or by entering values in empty spaces. In the case of chained lists the Push is through dynamic allocation, famous malloc(), where you will allocate the space of a cell (TAD) and connect at the top of the list, and the Pop is through the function free(), where you will free the space allocated to the cell you want to delete, taking care, of course, not to lose the reference of the previous value.

abstract illustration of Push and Pop

insert the description of the image here

insert the description of the image here

If my comment is incorrect and your activity is really about using your struct instance to store your data please flag so I can delete the answer!

Hugs and good studies!

 1
Author: H.Lima, 2019-04-22 20:15:27