Error in my data structure [STACK]

Asked

Viewed 97 times

0

My code is presenting two errors. As I am learning, I have tried several ways to fix it. Please someone can help me ?

I need to insert a number and a string into my stack. But it’s giving error in my structure.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#define MAX 5

typedef struct Ligacacao {
        char hora[MAX];
        int numero;
        struct Ligacacao *prox;        
        };

char hora[MAX];
int numero;
Ligacacao *topo; //ERRRRRRO


void dados_ligacao() {
     printf ("\nEntre com a hora da chamada: ");
     fflush (stdin);
     fgets (hora, MAX, stdin);
     printf ("Entre com o numero do telefone: ");
     fflush (stdin);
     scanf ("%d", &numero);
}
void push_ligacao() {

     dados_ligacao();
     Ligacacao *pnovo=(Ligacacao*)malloc(sizeof(Ligacacao)); //ERRRRRRO
     strcpy(pnovo->hora,hora);
     pnovo->numero=numero;
     pnovo->prox=NULL;
     if(topo==NULL)//se a pilha estiver vazia
     topo=pnovo; //topo recebe o novo elemento
     else{
          pnovo->prox=topo; 
          topo=pnovo;
          }
}

void pop_ligacao() {
     Ligacacao *aux;
     if(topo==NULL){
                    printf ("\n\nErro, Sem ligacoes.\n\n");
                    return;
                    }
                    else{
                         aux=topo;
                         topo=topo->prox;
                         free(aux);
                         }
}

void listar_ligacao() {
     Ligacacao *aux;
     aux=topo;
     while(aux!=NULL){
                      printf ("\t\t\tDados Ligacao\n\n");
                      printf ("Numero: %d", aux->numero);
                      aux=aux->prox;
                      } 
}

int main() {
    char op;
    topo=NULL;
   do{
       system("cls");
       printf ("\t\t\Ligacoes");
       printf ("\n\n(E)mpilhar Ligacacao\n");
       printf ("(L)istar Estoque Ligacacaos\n");
       printf ("(D)esempilhar Ligacacao\n");
       printf ("(S)air do Programa\n\n");
       printf ("Digite a opcao: ");
       op=toupper(getche());
       switch(op){
                  case'E': push_ligacao();
                  break;
                  case'L': listar_ligacao();
                  break;
                  case'D': pop_ligacao();
                  break;
                  case'S': exit(0);
                  default: printf ("\n\nOpcao invalida, digite novamente.\n\n");
                  }       
                  system("PAUSE");
                  }while (op!='S');
    return (0);
}

1 answer

2


Where are you making a mistake? I circled here, used E to stack, typed an hour and a phone number (both integer), then put to list and were there.

Here appeared two messages: unknown escape sequence '\L' on line 70:

printf ("\t\t\Ligacoes");

The latter is unknown because the L is not an escape sequence (such as n and t), so just remove and leave

printf ("\t\tLigacoes");

Another message pointed out was 'typedef' foi ignorada nesta declaração, on line 12:

typedef struct Ligacacao {
        char hora[MAX];
        int numero;
        struct Ligacacao *prox;        
        };

This happened because it is not necessary to use the word typedef in this case, simply remove and leave:

struct Ligacacao {
        char hora[MAX];
        int numero;
        struct Ligacacao *prox;        
        };

With these changes, compiled without displaying any Warning.

  • now I noticed that in your struct hour is a char vector, I went around again and inserted a string, everything worked out the same way... if the error is another try to put an image to see if I can help

Browser other questions tagged

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