#include <stdio.h>
#include <stdlib.h>
typedef struct{
char nome [20];
short eta;
}INFO_FIELD;
void *creaLista();
void insL_testa (short,INFO_FIELD *,void **);
void insL_nodo (short,INFO_FIELD *,void **);
void print (struct PERSONA*);
void main () {
struct PERSONA {
INFO_FIELD info;
struct PERSONA *p_next;
};
struct PERSONA* head;
//+p_next;
int len_info = sizeof(INFO_FIELD);
INFO_FIELD *p_nuovodato;
strcpy (p_nuovodato->nome,"pene");
p_nuovodato->eta = 5;
head = (struct PERSONA *) creaLista();
puts ("Inserisci dato in testa");
insL_testa (len_info,p_nuovodato,&head);
print (&head);
puts ("Inserisci dato dopo nodo corrente");
//insL_nodo (len_info,p_nuovodato,&punt);
puts ("Elimina in testa");
eliL_testa (&head);
puts ("Elimina nodo corrente");
//eliL_nodo (&prec);
//prec č nodo precedente a quello da eliminare
puts("");
system ("pause");
}
void *creaLista()
{char *testa;
testa=NULL;
return testa;
}
void insL_testa (short len_info,INFO_FIELD *p_dato, void **p_head)
{struct lista
{INFO_FIELD info;
struct lista *p_next;
} *ptr;
ptr=calloc (1,sizeof (struct lista));
memcpy (ptr,p_dato,len_info);
ptr->p_next=(struct lista *)*p_head;
*p_head = ptr;
}
void insL_nodo(short len_info,INFO_FIELD *p_dato,void** p_punt){
struct lista {
INFO_FIELD info;
struct lista *p_next;
} *ptr; //condivide solo INFOFIELD con il main
ptr = calloc (1,sizeof(struct lista));
memcpy (ptr,p_dato,len_info);
/* copia len info byte da
* p dato a ptr senza tener conto
* della struttura delle informazioni */
ptr->p_next = ((struct lista*)*p_punt)->p_next;
((struct lista*)*p_punt)->p_next = ptr;
*p_punt = ptr;
/* Il cast č obbligatorio prima di usare un puntatore
* generico, ma non quando lo si definisce */
}
void eliL_testa (void** p_head) {
struct lista {
INFO_FIELD info;
struct lista *p_next;
}*ptr;
ptr = ((struct lista*)*p_head)->p_next;
free ((struct lista*)*p_head);
*p_head = ptr;
}
void eliL_nodo (void ** p_punt) {
struct lista {
INFO_FIELD info;
struct lista *p_next;
}*ptr;
ptr = ((struct lista*)*p_punt) -> p_next;
((struct lista*)*p_punt) -> p_next = ptr->p_next;
free (ptr);
}
void print (struct PERSONA* head) {
while (head->next != NULL) {
printf ("%s",head->nome);
printf (" %d",head->eta);
head = head->next;
}
}