codice:
#include <stdio.h>
#include <stdlib.h>
#define item_type_lista float
typedef void * Lista;
Lista crea_lista(void);
int inserisci(Lista, int, item_type_lista *);
int inserisci_testa(Lista, item_type_lista *);
int inserisci_coda(Lista, item_type_lista *);
int inserimento_ordinato(Lista, item_type_lista *, int (*c)(item_type_lista *, item_type_lista *));
int elimina(Lista, int, item_type_lista *);
int elimina_elemento(Lista, item_type_lista *, item_type_lista *, int (*c)(item_type_lista *, item_type_lista *));
int elimina_occorrenza(Lista, int, item_type_lista *, item_type_lista *, int (*c)(item_type_lista *, item_type_lista *));
int elimina_ultimo(Lista, item_type_lista *, item_type_lista *, int (*c)(item_type_lista *, item_type_lista *));
int elimina_testa(Lista, item_type_lista *);
int elimina_coda(Lista, item_type_lista *);
int assegna(Lista, int, item_type_lista *);
int modifica_elemento(Lista, item_type_lista *, item_type_lista *, int (*c)(item_type_lista *, item_type_lista *));
int modifica_occorrenza(Lista, int, item_type_lista *, item_type_lista *, int (*c)(item_type_lista *, item_type_lista *));
int modifica_ultimo(Lista, item_type_lista *, item_type_lista *, int (*c)(item_type_lista *, item_type_lista *));
int leggi(Lista, int, item_type_lista *);
int leggi_testa(Lista, item_type_lista *);
int leggi_coda(Lista, item_type_lista *);
int lista_vuota(Lista);
int lista_piena(Lista);
int lunghezza(Lista);
int stampa_lista(Lista, void (*s)(item_type_lista *));
int cerca_occorrenza(Lista, int, item_type_lista *, int (*c)(item_type_lista *, item_type_lista *));
int cerca(Lista, item_type_lista *, int (*c)(item_type_lista *, item_type_lista *));
int cerca_ultimo(Lista, item_type_lista *, int (*c)(item_type_lista *, item_type_lista *));
void ottieni_elemento(item_type_lista *ritorno);
int compara(item_type_lista *uno, item_type_lista *due);
void stampa(item_type_lista *x);
int main()
{
Lista L = crea_lista();
float dati[] = {23,40,33,3,6,90,95,2,4,37,89};
int i ;
for(i = 0 ; i < 11 ; i++)
inserisci(L,i+1,&dati[i]);
stampa_lista(L,stampa);
float da_cercare = 4;
int eliminato = cerca_occorrenza(L, 1, &da_cercare, compara);
if (eliminato)
printf("Elemento trovato in posizione %d\n", eliminato);
else
printf("Elemento NON trovato\n");
float da_modificare =90;
float nuovo_elemento = 4;
eliminato = modifica_occorrenza(L, 1, &da_modificare, &nuovo_elemento, compara);
if (eliminato)
printf("Elemento modificato\n");
else
printf("Elemento NON trovato\n");
stampa_lista(L,stampa);
return 0;
}
struct campo_lista {
item_type_lista info;
struct campo_lista *next, *prev;
};
typedef struct LISTA {
struct campo_lista *Head, *Tail;
unsigned lunghezza;
} *Lista_ls;
Lista crea_lista(void)
{
Lista_ls l = (Lista_ls)malloc(sizeof(struct LISTA));
l->lunghezza = 0;
l->Head = l->Tail = NULL;
return (Lista) l;
}
void stampa(item_type_lista *x)
{
printf("%.2f\t", *x);
}
int compara(item_type_lista *uno, item_type_lista *due)
{
return (int) (*uno - *due);
}
int inserisci(Lista L, int pos, item_type_lista *elemento)
{
if (!lista_piena(L))
{
Lista_ls l = (Lista_ls) L;
if (pos > 0 && pos <= l->lunghezza + 1)
{
struct campo_lista *nuovo = (struct campo_lista *)malloc(sizeof(struct campo_lista));
if (nuovo != NULL)
{
nuovo->info = *elemento;
if (pos == 1)
{
nuovo->prev = NULL;
nuovo->next = l->Head;
if (l->Head)
l->Head->prev = nuovo;
if (l->Tail == NULL)
l->Tail = nuovo;
l->Head = nuovo;
}
else if (pos == l->lunghezza + 1)
{
nuovo->next = NULL;
nuovo->prev = l->Tail;
l->Tail->next = nuovo;
l->Tail = nuovo;
}
else if (pos == l->lunghezza)
{
nuovo->next = l->Tail;
nuovo->prev = l->Tail->prev;
l->Tail->prev->next = nuovo;
l->Tail->prev = nuovo;
}
else
{
struct campo_lista *temp = l->Head->next;
int i = 2;
while (i != pos)
{
temp = temp->next;
i++;
}
nuovo->next = temp;
nuovo->prev = temp->prev;
temp->prev->next = nuovo;
temp->prev = nuovo;
}
l->lunghezza++;
return 1;
}
}
}
return 0;
}
int lista_piena(Lista L)
{
return 0;
}
int leggi(Lista L, int pos, item_type_lista *ritorno)
{
if (!lista_vuota(L))
{
Lista_ls l = (Lista_ls) L;
if (pos > 0 && pos <= l->lunghezza)
{
if (pos == 1)
*ritorno = l->Head->info;
else if (pos == l->lunghezza)
*ritorno = l->Tail->info;
else
{
int i = 2;
struct campo_lista *temp = l->Head->next;
while (i++ != pos)
temp = temp->next;
*ritorno = temp->info;
}
return 1;
}
}
return 0;
}
int lunghezza(Lista L)
{
Lista_ls l = (Lista_ls) L;
return l->lunghezza;
}
int stampa_lista(Lista L, void (*stampa)(item_type_lista *))
{
if (!lista_vuota(L))
{
Lista_ls l = (Lista_ls) L;
struct campo_lista *temp = l->Head;
while (temp)
{
(*stampa)(&(temp->info));
temp = temp->next;
}
printf("\n");
return 1;
}
return 0;
}
int assegna(Lista L, int pos, item_type_lista *el)
{
item_type_lista temp;
int eliminato = elimina(L, pos, &temp);
if (eliminato)
return inserisci(L, pos, el);
return 0;
}
int elimina(Lista L, int pos, item_type_lista *ritorno)
{
if (!lista_vuota(L))
{
Lista_ls l = (Lista_ls) L;
if (pos > 0 && pos <= l->lunghezza)
{
struct campo_lista *temp = NULL;
if (pos == 1)
{
*ritorno = l->Head->info;
temp = l->Head;
l->Head = l->Head->next;
if (l->Head)
l->Head->prev = NULL;
else
l->Tail = NULL;
free(temp);
}
else if (pos == l->lunghezza)
{
*ritorno = l->Tail->info;
temp = l->Tail;
l->Tail = l->Tail->prev;
l->Tail->next = NULL;
}
else
{
int i = 2;
temp = l->Head->next;
while (i++ != pos)
temp = temp->next;
*ritorno = temp->info;
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
free(temp);
}
l->lunghezza--;
return 1;
}
}
return 0;
}
int cerca_occorrenza(Lista L, int occ, item_type_lista *el, int (*compare)(item_type_lista *, item_type_lista *))
{
if (!lista_vuota(L))
{
Lista_ls l = (Lista_ls) L;
struct campo_lista *temp = l->Head;
int pos = 1;
int occorrenza = 0;
while (temp)
{
if ((*compare)(&temp->info, el) == 0)
if (++occorrenza == occ)
return pos;
pos++;
temp = temp->next;
}
}
return 0;
}
int lista_vuota(Lista L)
{
Lista_ls l = (Lista_ls) L;
return l->lunghezza == 0;
}
int modifica_occorrenza(Lista L, int occ, item_type_lista *vecchio, item_type_lista *nuovo, int (*compare)(item_type_lista *, item_type_lista *))
{
if (!lista_vuota(L))
{
int pos = cerca_occorrenza(L, occ, vecchio, compare);
printf("pos:\t%d\n", pos);
if (pos)
return assegna(L, pos, nuovo);
}
return 0;
}