Skip to content

Instantly share code, notes, and snippets.

@aciceri
Created April 22, 2016 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aciceri/daf9a337c9f822ebc38b93d69ce66fa1 to your computer and use it in GitHub Desktop.
Save aciceri/daf9a337c9f822ebc38b93d69ce66fa1 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
struct lista_nodo {
char valore;
struct lista_nodo * prossimo;
};
typedef struct lista_nodo elemento;
elemento * carica_lista();
void stampa_lista(elemento * testa);
void aggiungi_fine(elemento * testa);
elemento * aggiungi_inizio(elemento * testa);
elemento * cancella_elemento(elemento * testa);
elemento * inverti_lista(elemento * testa);
int main(void) {
char scelta;
elemento * lista; //la lista viene gestita tramite il puntatore al suo primo elemento
lista = NULL; //inizialmente la lista è vuota
do {
printf("---Cosa vuoi fare?---\n");
printf("1-Inserisci lista\n");
printf("2-Stampa lista\n");
printf("3-Aggiungi alla fine della lista\n");
printf("4-Aggiungi all'inizio della lista\n");
printf("5-Cancella elemento dalla lista (prima occorrenza)\n");
printf("6-Inverti lista\n");
printf("0-Esci\n");
printf(">>> ");
scanf(" %c", &scelta);
switch(scelta) {
case '1':
printf("Inserisci gli elementi della lista (inserisci 0 per uscire)\n");
lista = carica_lista();
break;
case '2':
stampa_lista(lista);
break;
case '3':
aggiungi_fine(lista);
break;
case '4':
lista = aggiungi_inizio(lista);
break;
case '5':
lista = cancella_elemento(lista);
break;
case '6':
lista = inverti_lista(lista);
break;
}
} while(scelta != '0');
printf("Programma terminato\n");
return 0;
}
elemento * carica_lista() {
elemento * attuale;
char valore_attuale;
printf(">>> ");
scanf("%s", &valore_attuale);
if (valore_attuale == '0')
return NULL;
else {
attuale = (elemento *) malloc (sizeof(elemento));
attuale->valore = valore_attuale;
attuale->prossimo = carica_lista();
return attuale;
}
}
void stampa_lista(elemento * testa) {
if (testa == NULL)
printf("La lista e' vuota!\n");
else {
while (testa != NULL) {
printf("%c ", testa->valore);
testa = testa->prossimo;
}
printf("\n");
}
}
void aggiungi_fine(elemento * testa) {
elemento * nuova_lista;
if (testa == NULL)
printf("La lista e' vuota!\n");
else {
while (testa->prossimo != NULL) {
testa = testa->prossimo;
}
printf("Inserisci gli elementi da aggiungere alla lista (inserisci 0 per uscire)\n");
nuova_lista = carica_lista();
testa->prossimo = nuova_lista;
}
}
elemento * aggiungi_inizio(elemento * testa) {
elemento * nuova_lista, * attuale;
if (testa == NULL) {
printf("La lista e' vuota!\n");
return testa;
}
else {
printf("Inserisci gli elementi da aggiungere alla lista (inserisci 0 per uscire)\n");
nuova_lista = carica_lista();
attuale = nuova_lista;
while (attuale->prossimo != NULL) {
attuale = attuale->prossimo;
}
attuale->prossimo = testa;
return nuova_lista;
}
}
elemento * cancella_elemento(elemento * testa) {
elemento * attuale;
char valore;
if (testa == NULL) {
printf("La lista e' vuota!\n");
return testa;
}
else {
printf("Che elemento vuoi cancellare?\n>>> ");
scanf(" %c", &valore);
if (testa->valore == valore)
return testa->prossimo;
attuale = testa;
while (attuale->prossimo != NULL) {
if (attuale->prossimo->valore == valore) {
attuale->prossimo = attuale->prossimo->prossimo;
break;
}
else
attuale = attuale->prossimo;
}
return testa;
}
}
elemento * inverti_lista(elemento * testa) {
elemento * nuovo_elemento, * nuova_lista;
if (testa == NULL) {
printf("La lista e' vuota!\n");
return testa;
}
else {
nuovo_elemento = NULL;
while (testa != NULL) {
nuova_lista = (elemento *) malloc(sizeof(elemento));
nuova_lista->valore = testa->valore;
nuova_lista->prossimo = nuovo_elemento;
nuovo_elemento = nuova_lista;
testa = testa->prossimo;
}
return nuova_lista;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment