feat: Initial commit
This commit is contained in:
73
Laboratorio 7/Esercizio 2/Diagonali.c
Normal file
73
Laboratorio 7/Esercizio 2/Diagonali.c
Normal file
@@ -0,0 +1,73 @@
|
||||
// Laboratorio 7 - Esercizio 2 - Diagonali.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "Elementi.h"
|
||||
#include "Diagonali.h"
|
||||
|
||||
typedef struct node_s *link;
|
||||
|
||||
typedef struct node_s {
|
||||
Diagonale diag;
|
||||
link next;
|
||||
} node;
|
||||
|
||||
typedef struct diagonali
|
||||
{
|
||||
link head;
|
||||
int N;
|
||||
} *Diagonali;
|
||||
|
||||
link new_node(Diagonale diag, link next) {
|
||||
link t = malloc(sizeof(node));
|
||||
if (t == NULL) {
|
||||
printf("Errore: impossibile allocare memoria");
|
||||
exit(2);
|
||||
}
|
||||
t->diag = diag;
|
||||
t->next = next;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
void free_node(link p) {
|
||||
free(p);
|
||||
}
|
||||
|
||||
/* creatore e distruttore */
|
||||
Diagonali DiagionaliInit() {
|
||||
Diagonali new = malloc(sizeof(struct diagonali));
|
||||
new->head = NULL;
|
||||
new->N = 0;
|
||||
return new;
|
||||
}
|
||||
|
||||
void DiagonaliFree(Diagonali diags) {
|
||||
link p, x;
|
||||
for (p = diags->head, x = diags->head->next; x != NULL; p = x, x = x->next){
|
||||
free_node(p);
|
||||
}
|
||||
free(diags);
|
||||
}
|
||||
|
||||
void DiagonaliInsert(Diagonali diags, Diagonale newDiag) {
|
||||
link nd = new_node(newDiag, diags->head);
|
||||
diags->head = nd;
|
||||
diags->N++;
|
||||
}
|
||||
|
||||
link DiagonaliTraverse(Diagonali diags, link prev, Diagonale ** elem) {
|
||||
link next;
|
||||
|
||||
if (prev == NULL) {
|
||||
next = diags->head;
|
||||
} else {
|
||||
next = prev->next;
|
||||
}
|
||||
|
||||
if (next == NULL)
|
||||
return NULL;
|
||||
|
||||
*elem = &next->diag;
|
||||
return next;
|
||||
}
|
||||
Reference in New Issue
Block a user