Sei sulla pagina 1di 3

#include <stdio.

h>
#include <malloc.h>
#include <stdbool.h>

#define OK (0)
#define ERRORE (-1)
#define CODA_VUOTA (-1)
#define DIM_INIZIALE 10
#define CODA_PIENA (12)
#define ERRORE_ALLOCAZIONE (-100)

struct persona_struct {
long codice_id;
char SPID[64];
char nome[64];
char CF[13];
unsigned short eta;
};
typedef struct persona_struct Persona;
typedef Persona Elemento;

typedef unsigned int Index;


typedef unsigned int Size;
typedef struct coda_struct {
Elemento * valori;
Index primo;
Index ultimo;
Size dimensione;
} Queue;
typedef Queue* pQueue;

int create_queue(pQueue* ppCoda) {


Queue* queue = malloc(sizeof(Queue));
if (queue == NULL) {
return ERRORE_ALLOCAZIONE;
}

queue->valori = malloc(DIM_INIZIALE * sizeof(Elemento));


queue->dimensione = DIM_INIZIALE;

queue->primo = CODA_VUOTA;
queue->ultimo = CODA_VUOTA;

*ppCoda = queue;

return OK;
}

bool is_queue_empty(const Queue* pCoda) {


// return (pCoda->primo == CODA_VUOTA) ? true: false;
return (pCoda->primo == CODA_VUOTA);
}

bool is_queue_full(const Queue* pCoda) {


if (pCoda->primo > pCoda->ultimo) {
return ((pCoda->primo -pCoda->ultimo) == 1);
}
// if (pCoda->primo < pCoda->ultimo)
return ((pCoda->ultimo - pCoda->primo) == (pCoda->dimensione - 1));
}

int in_queue(Queue* pCoda, const Elemento* pElemento) {


if (is_queue_empty(pCoda)) {
pCoda->primo = 0;
pCoda->ultimo = 0;
}
else if (is_queue_full(pCoda)) {
return CODA_PIENA;
}
else {
pCoda->ultimo = (pCoda->ultimo + 1) % pCoda->dimensione;
}
// Elemento === (*pElemento)
pCoda->valori[pCoda->ultimo] = *pElemento;

return OK;
}

// Restituisce l'elemento in testa alla coda


int head_of_queue(const Queue* pCoda, Elemento* pElemento) {
if (is_queue_empty(pCoda)) return CODA_VUOTA;

*pElemento = pCoda->valori[pCoda->primo];

return OK;
}

// Completare chiamando head_of_queue e rimuovendo l'elemento


int out_queue(Queue* pCoda, Elemento* pElemento) {
int codice_errore = head_of_queue(pCoda, pElemento);
if (codice_errore != OK) return codice_errore;

if (pCoda->primo != pCoda->ultimo) {
pCoda->primo = (pCoda->primo + 1) % pCoda->dimensione;
} else {
pCoda->primo = CODA_VUOTA;
pCoda->ultimo = CODA_VUOTA;
}
return OK;
}

void destroy_queue(pQueue * p) {
if (p == NULL) return;
// free((**p).valori);
free((*p)->valori);
free(*p);

*p = NULL;
}

int main() {
int codice_errore;
Queue* pCoda;

codice_errore = create_queue(&pCoda);
if (codice_errore == ERRORE_ALLOCAZIONE) {
printf("Errore allocazione della memoria\n");
}
else if (codice_errore == OK) {
printf("Coda creata con successo\n");
}

Persona persona1 = {
.CF="CF1",
.codice_id=1,
.eta=20,
.nome="Nome1",
.SPID="SPID1",
};

codice_errore = in_queue(pCoda, &persona1);


if (codice_errore == CODA_PIENA) {
printf("Coda piena\n");
}
else if (codice_errore == OK) {
printf("Elemento inserito con successo\n");
}

Persona persona;
head_of_queue(pCoda, &persona);
// codice_errore = out_queue(pCoda, &persona);

destroy_queue(&pCoda);

return 0;
}

Potrebbero piacerti anche