


















































Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!



















































Appunti di Programmazione.
void remove_val(struct list_t *list, int value){ struct node_t *temp; if((list->head)->value == value){ temp = list->head; //parto dalla testa e salvo il valore list->head = (list->head)->next; free(temp); } else { struct node_t *current = list->head; while(current->next != NULL){ //se fosse vero, dobbiamo eliminare il nodo che viene puntato dopo current if(current->next->value == value){ temp = current->next; //tolgo il nodo dalla lista current->next = current->next->next; free(temp); break; } //altrimenti lo sposto e continuo else current = current->next; } } } int main(void){ struct list_t *mylist = newlist(); struct list_t *secondlist = newlist(); int val = 0; int searchvalue = 0; int val1 = 0; int val2 = 0; for(int i=0; i<5; i++){ printf("Aggiungi l'elemento %i in coda: ", i+1); scanf("%i", &val); aggiungi_coda(mylist, val); } print(mylist); for(int i=0; i<5; i++){ printf("Aggiungi l'elemento %i in testa: ", i+1); scanf("%i", &val); aggiungi_testa(secondlist, val); } print(secondlist); printf("Inserisci il valore da cercare nella lista 1 e lista 2: "); scanf("%i", &searchvalue); printf("Il valore %i ha dato come risultato:\n\t%i nella lista 1\n\t%i nella lista 2\n", searchvalue, ricerca(mylist, searchvalue), ricerca(secondlist, searchvalue)); printf("\nInserisci il valore da eliminare nella lista 1: "); scanf("%i", &val1); printf("\nInserisci il valore da eliminare nella lista2: "); scanf("%i", &val2); remove_val(mylist, val1); remove_val(secondlist, val2); printf("\nStampo le liste modificate: "); print(mylist); print(secondlist);
clean(mylist, "della prima lista"); clean(secondlist, "della seconda lista"); return 0; } ————————————— LISTA NON ORDINATA #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // malloc(), free() struct node_t { int value; struct node_t *next; }; struct list_t { struct node_t *head; }; void check_malloc(void *p); struct list_t *new_list(); void insert_on_head(struct list_t *list, int value); void print(struct list_t *list); int main() { struct list_t *list = new_list(); for (int v = 0; v < 10; v++) { insert_on_head(list, v); } print(list); // free_list(list); // La memoria andrebbe liberata return 0; } void check_malloc(void *p) { if (p == NULL) { printf("Could not allocate memory!\n"); exit(-1); } } struct list_t *new_list() { struct list_t *list = (struct list_t *)malloc(sizeof(struct list_t)); check_malloc(list); list->head = NULL; return list; } void insert_on_head(struct list_t *list, int value) { struct node_t *node = (struct node_t *)malloc(sizeof(struct node_t)); check_malloc(node); node->value = value; node->next = list->head;
struct list_t *new_list() { struct list_t *list = (struct list_t *)malloc(sizeof(struct list_t)); list->head = NULL; return list; } void insert_into_list(struct list_t *list, int value){ struct node_t *node = (struct node_t *)malloc(sizeof(struct node_t)); node->value = value; struct node_t *current = list->head; struct node_t *previous = NULL; while (current != NULL) { if (current->value >= value) { break; } previous = current; current = current->next; } node->next = current; if (previous == NULL) { list->head = node; } else { previous->next = node; } } void print(struct list_t *list) { if (list->head == NULL) { printf("La lista è vuota\n"); return; } struct node_t *current = list->head; while (current != NULL) { printf("%i ", current->value); current = current->next; } printf("\n"); } void empty_list(struct list_t *list) { struct node_t *current = list->head; struct node_t *previous = NULL; while (current != NULL) { if (previous != NULL) { free(previous); } previous = current; current = current->next; } if (previous != NULL) { free(previous); } list->head = NULL; } void free_list(struct list_t *list) { empty_list(list);
free(list); } void remove_from_list(struct list_t *list, int value) { struct node_t *current = list->head; struct node_t *previous = NULL; while (current != NULL) { if (current->value == value) { if (current == list->head) { list->head = current->next; free(current); } else { previous->next = current->next; free(current); } return; } else if (current->value > value) { return; } previous = current; current = current->next; } } bool contains(struct list_t *list, int value) { struct node_t *current = list->head; while (current != NULL) { if (current->value == value) { return true; } else if (current->value > value) { return false; } current = current->next; } return false; } int main(void){ //creo la lista struct list_t *list = new_list(); system("clear"); //inserisci numeri a caso for(int i=0; i<15; i++){ insert_into_list(list, (i)); } printf("Stampo la lista:\n"); print(list); printf("\nRimuovo l'elemento uguale a 3 --> "); remove_from_list(list, 3); print(list); printf("\nControllo se contiene l'elemento uguale a 7 --> %i\n", contains(list, 7)); printf("\n--- Svuoto la lista ---\n"); free_list(list); printf("Ho svuotato la lista.\nControllo: --> "); print(list); return 0; }
head = head->next; } } // Dealloca gli accordi della canzone void free_canzone(struct accordo_t head) { if(head!=NULL){ free_canzone(head->next); free(head); } } ——————————————————————— ES LISTA SULLE VOCALI /*
*/ #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct node_t{ char word; struct node_t *next; }node_t; struct node_t *new_list(char *s); void double_vowels(struct node_t *head); struct node_t *new_node(char value); struct node_t *new_node(char value) { struct node_t *new = (struct node_t *)malloc(sizeof(struct node_t)); if (new == NULL){ printf("Memory allocation failed\n"); exit(EXIT_FAILURE); } new->word = value; new->next = NULL; return new; } struct node_t *new_list(char *s){ struct node_t *head = NULL; struct node_t prev = NULL; while (s) { struct node_t new = new_node(s); if (head == NULL) { head = new; } else { prev->next = new;
prev = new; s++; } return head; } void print_list(struct node_t *list){ if(list == NULL){ printf("Lista vuota.\n"); return; } while( list != NULL){ printf("%c", list->word); if(list->next == NULL){ printf("."); } list = list->next; } printf("\n"); } void double_vowels(struct node_t *head){ struct node_t *mine = head; while(mine){ if(mine->word == 'a' || mine->word == 'e' || mine->word == 'i' || mine->word == 'o' || mine-
word == 'u'){ struct node_t *new = new_node(mine->word); new->next = mine->next; mine->next = new; mine = new->next; } else { mine = mine->next; } } } int main() { char s[] = "hello darkness my old friend"; struct node_t *head = new_list(s); print_list(head); printf("\n"); double_vowels(head); print_list(head); printf("\n"); free(head); return 0; }
#include<stdio.h> #include<stdlib.h> int main (void){ int x = 5; float y = 7.12; char c = 'k'; int *p = &x; float *py = &y; char pc = &c; / PER STAMPARE IL VALORE DEL POINTER: %d e *p PER STAMPARE L'INDIRIZZO IN MEMORIA: %p e p PER STAMPARE LA DIMENSIONE: %d e sizeof(p) dove p è un pointer dichiarato a int (int *p = &x) */ printf("\nIndirizzo in memoria di p riferito a %i: %p\nvalore memorizzato: %d\nDimensione: %lu", x, p, *p, sizeof(p)); printf("\nIndirizzo in memoria di py riferito a %i: %p\nvalore memorizzato: %.2f\nDimensione: %lu", x, py, py, sizeof(py)); printf("\nIndirizzo in memoria di pc riferito a %i: %p\nvalore memorizzato: %c\nDimensione: %lu", x, pc, pc, sizeof(pc)); return 0; } ————————————————————————————————————————- TOGLIERE RIPETIZIONI E ZERI /
*s = sum; } int main(void){ double x = 0; double y = 0; double somma = x+y; double s; s = &somma; printf("Inserisci due double: "); scanf("%lf %lf", &x, &y); sum(s, x, y); printf("Somma ritornata tra %.2lf e %.2lf: %.2lf", x, y, s); } ——————————————————————————— SCAMBIO TRA DUE POINTERS /
if(n<=0 || n>= actualsize){ do{ printf("\nn incorretto.\n\tInserisci un numero n intero: "); scanf("%i", &n); } while(n<=0 || n>=actualsize); } char arr2[n+1]; int i=0; for(p = arr; p<arr+n; p++, i++){ *(arr2+i) = *p; } arr2[n+1] = '\0'; char arr3[actualsize-n+1]; for(int j=0, i=n; i<actualsize; j++, i++){ *(arr3+j) = (arr+i); } arr3[actualsize-n+1] = '\0'; printf("\n\nStringa modificata:\n\t\t%s%s", arr3, arr2); return 0; } ———————————————————————————— ORDINARE TRE POINTERS / Scrivere una funzione sort che ordina 3 interi in ordine crescente.
t->npezzi = 342; n++; } void stampa(prodotto arr[], int length, prodotto *t){ t = arr; printf("\n"); for(int i=0; i<n; i++, t++){ printf("Nome: %s\n", t->nome); printf("Codice: %d\n", t->codice); printf("Prezzo: %.2f\n", t->prezzo); printf("N. venduti: %d\n", t->npezzi); printf("\n"); } } void max_vendite(prodotto arr[], int length, prodotto t){ int maxi = 0; int pos = 0; t = arr; for(int i=0; i<n; i++){ for(int j = 1; j<n-1; j++){ if(arr[i].npezzi >= arr[j].npezzi){ pos = i; } else { pos = j; } } } printf("\nProdotto con il massimo delle vendite: "); printf("\nNome: %s\n", arr[pos].nome); printf("Codice: %d\n", arr[pos].codice); printf("Prezzo: %.2f\n", arr[pos].prezzo); printf("N. venduti: %d\n", arr[pos].npezzi); printf("\n"); } int main(void){ prodotto prodotti[maxpr]; prodotto t; prodotto_func(prodotti,maxpr, t); stampa(prodotti, maxpr, t); max_vendite(prodotti, maxpr, t); } ————————————————————————————— STRUTTURA PER L’ORA /
typedef struct time{ int sec_iniziali; int ora; int minuti; int secondi; } time; void split_time(long int tot_sec, struct time *t); void split_time(long int tot_sec, struct time *t){ printf("Inserire l'orario in secondi: "); scanf("%lu", &tot_sec); int sec = tot_sec; int ora = tot_sec/3600; int minuti = (tot_sec%3600)/60; int secondi = (tot_sec%3600)%60; t->sec_iniziali = sec; t->ora = ora; t->minuti = minuti; t->secondi = secondi; } int main(void){ long int tot_sec; struct time myTime; struct time t = &myTime; long int psec = &tot_sec; split_time(tot_sec, t); printf("da %i secondi a:\n\t%i ore, %i minuti e %i secondi", myTime.sec_iniziali, myTime.ora, myTime.minuti, myTime.secondi); return 0; } ————————————————————————— SCORRERE ARRAY CON POINTERS /