Puntatore di testa, puntatore di coda e nodi con informazioni
Dichiarazioni
typedef struct nodo { int info; nodo *link; } NODO; typedef NODO *pnodo; typedef struct listatc { pnodo testa; pnodo coda; } LISTATC;
Operazioni
Aggiungere un nodo in Testa
void aggInTesta(LISTATC *L, int N) { pnodo p=(pnodo)malloc(sizeof(NODO)); p->info=N; p->link=L->testa; if(L->testa == NULL) L->coda=p; L->testa:=p; }
Aggiungere un nodo in Coda
void aggInCoda(LISTATC *L, int N) { pnodo p=(pnodo)malloc(sizeof(NODO)); p->info=N; p->link=NULL; if(L->testa == NULL) L->testa=p; else L->coda->link=p; L->coda=p; }
Aggiungere un nodo in Ordine
void aggInOrdine(LISTATC *L, int N) { pnodo p=(pnodo)malloc(sizeof(NODO)); p->info=N; if(L->testa == NULL || N <= L->testa->info) { p->link=L->testa; L->testa=p; L->coda=p; } else { pnodo attuale =L->testa; pnodo prossimo=attuale->link; while(prossimo != NULL && prossimo->info < N) { attuale=prossimo; prossimo=prossimo->link; } p->link=prossimo; attuale->link=p; } }
Eliminare un nodo in Testa
void eliInTesta(LISTATC *L) { if(L->testa != NULL) { pnodo p=L->testa; L->testa=L->testa->link; free(p); if(L->testa == NULL) L->coda=NULL; } }
Eliminare TUTTI i nodi
void rilascia(LISTATC *L) { pnodo p=L->testa; while(p != NULL) { pnodo q=p; p=p->link; free(q); } L->testa=NULL; L->coda=NULL; }
La più lunga
int piuLunga(LISTATC *L1, LISTATC *L2) { pnodo p1=L1->testa; pnodo p2=L2->testa; while(p1 != NULL && p2 != NULL) { p1=p1->link; p2=p2->link; } if(p1 == NULL && p2 == NULL) return 0; else if(p2 != NULL) return -1; else return +1; }