Typedef coda in C

Questa è la prima volta che implemento una coda in C (ho sempre utilizzato quelle già definite nei linguaggi ad oggetti come Java e C#) ma ho un problema sulla typedef perchè il compilatore non conosce ancora il tipo del campo next in quanto il tipo Coda non è ancora stato definito.

Mi potete dare un consiglio su come fare?

typedef struct {int this; Coda* next;}Coda;

Coda* c;
Coda* fine;

void queue(int x)
{
    Coda* temp=malloc(sizeof(Coda));
    temp->this=x;
    temp->next=NULL;
    fine->next=temp;
    fine=temp;
}

int dequeue()
{
    int temp;
    temp=c->this;
    c=c->next;
}
typedef struct Coda
{
    int value; 
    Coda *ptr;
} Coda;

Dovrebbe funzionare

2 Mi Piace

Purtroppo non funziona…

Questo è il codice

typedef struct Coda
{
    int value; 
    Coda *ptr;
} Coda;

Coda* c;
Coda* fine;

void queue(int x)
{
    Coda* temp=malloc(sizeof(Coda));
    temp->value=x;
    temp->ptr=NULL;
    fine->ptr=temp;
    fine=temp;
}

int dequeue()
{
    int temp;
    temp=c->value;
    c=c->ptr;
}

e questo è l’errore del compilatore

somme.c:7:5: error: unknown type name ‘Coda’
Coda *ptr;
^~~~

Gcc me lo compila senza problemi, che compilatore stai usando e con che parametri?

2 Mi Piace

Lo sto caricando direttamente sulla piattaforma di allenamentoCattura

Credo di aver trovato l’errore. Prova:

typedef struct Coda
{
    int value; 
    struct Coda *ptr;
} Coda;
2 Mi Piace

Ora funziona, grazie mille

1 Mi Piace