#include #include // procedure list struct list * init(); void print(struct list * head); int length(struct list * head); int search(struct list * head, int key); // list data type struct list { int value; struct list *next; }; // init a list interacting with the user struct list * init() { // a pointer to the head, to be returned struct list * head; // a pointer always pointing to the end of the list struct list * list_pointer = NULL; // auxiliary variabiles int input = -1; // ask for input, untill it is "0" while( input != 0 ) { // ask input printf("Inserisci un numero (0 per terminare) : "); scanf("%d", &input); // if it is not 0, add it to the list, otherwise exit from while if(input != 0) { // special care for empty list when list_pointer is NULL if(list_pointer == NULL) { // create the head object head = (struct list *) malloc( sizeof(struct list *) ); // and point to the head list_pointer = head; } else // the general case, when the list is not empty, and list_pointer points to the last inserted element { // allocate memory for a new record list_pointer->next = (struct list *) malloc( sizeof(struct list *) ); // shift the pointer list_pointer = list_pointer->next; } // store input value list_pointer->value = input; } } // set the end of the list list_pointer->next = NULL; // return the pointer to the head return head; } // print a list void print(struct list * head) { // declare a pointer pointing to the head struct list * list_pointer = head; // loop over whole list printf("["); while (list_pointer != 0) { // print the pointed value printf ("\t%d", list_pointer->value); // shift the pointer list_pointer = list_pointer->next; } printf("\t]\n"); } // search for an element in a list, if it is found, return its index in the list, otherwise return -1 int search(struct list * head, int key) { // declare a pointer pointing to the head struct list * list_pointer = head; int i = 0; // loop over whole list while (list_pointer != 0) { // if it is found if(list_pointer->value == key) return i; // then return its index else // otherwise shift the pointer { i++; list_pointer = list_pointer->next; } } return -1; } // compute the length of a list int length(struct list * head) { // declare a pointer pointing to the head struct list * list_pointer = head; int count = 0; // loop over whole list while (list_pointer != 0) { count++; // update counter // and shift the pointer list_pointer = list_pointer->next; } return count; } /*****************************************/ void main () { struct list *list_pointer; int i, j; printf("Questo e' un programma semplice che dovete essere in grado di capire alla fine di questo semplice corso sulla programmazione in C. \n\nAdesso tu, caro utente, interagirai con me per creare una lista di interi...\n"); list_pointer = init(); printf("\nBenissimo, ti stampo un riepilogo della lista che hai inserito: "); print(list_pointer); printf("Hai inserito %d elementi.\n", length(list_pointer)); printf("Adesso inserisci un valore intero qualunque, ed io lo cerchero nella lista: "); scanf("%d",&i); j = search(list_pointer,i); if(j == -1) printf("\nElemento NON trovato nella lista!!"); else { printf("\nElemento trovato in posizione %d nella lista!!",j); } }