


























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!



























1 Tipi di dati in C
Per stampare variabili si utilizzano delle stringhe di formato:
Ci sono due modi per definire una struct:
1 struct studente { 2 char nome [10]; 3 char cognome [10]; 4 int anno; 5 }; 6 7 //dichiarazione variabile di tipo struct studente 8 struct studente studente1;
2 Alcuni frammenti di codice utili
1 int array[] = {1, 2, 3, 4, 5}; 2 //oppure 3 int array[5] = {1, 2, 3, 4, 5};
Nel secondo caso il compilatore assegna in automatico la dimensione. Per conoscere la dimensione di un array: sizeof(a)/sizeof(a[0])
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() { 5 int d; 6 printf("Inserire dimensione: "); 7 scanf("%d", &d); 8 printf("\n"); 9 int* a = malloc(d*sizeof(int)); 10 11 for (int i = 0; i < d; i++) { 12 a[i] = i; 13 }
Input: Inserire dimensione: 5 Output: 0 1 2 3 4
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(void) 5 { 6 int r = 3, c = 4; 7 8 int* ptr = malloc((r * c) * sizeof(int)); 9
10 /* Putting 1 to 12 in the 1D array in a sequence */ 11 for (int i = 0; i < r * c; i++) 12 ptr[i] = i + 1; 13 14 /* Accessing the array values as if it was a 2D array */ 15 for (int i = 0; i < r; i++) { 16 for (int j = 0; j < c; j++) 17 printf("%d ", ptr[i * c + j]); 18 printf("\n"); 19 } 20 21 free(ptr); 22 23 return 0; 24 }
1 char element = '500' 2 int convertito = element - '0'
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() { 5 int r, c; 6 scanf("%d %d", &r, &c); 7 scanf("\n") //newline consumed 8 for (int i = 0; i < r; i++) { 9 scanf("\n"); 10 for (int j = 0; j < c; j++) { 11 char element; 12 scanf("%c", &element); 13 matrice[i][j] = element - '0'; 14 } 15 } 16 }
La matrice viene inizializzata con il carattere "."
1 char **creaMatrice(int n) { 2 char m; 3 int r, c; 4 m = malloc(nsizeof(char)); 5 for (r = 0; r < n; r++) { 6 *(m+r) = malloc(n * sizeof(char)); 7 } 8 9 for (r = 0; r < n; r++) { 10 for (c = 0; c < n; c++) { 11 m[r][c] = '.'; 12 } 13 } 14 return m; 15 }
Esempio di risoluzione di una parte dell’esercizio 1 del tema d’esame del 16 giugno 2022.
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() { 5 int r, c; 6 scanf("%d %d", &r, &c); 7 int matrice = malloc((r * c)sizeof(int)); 8 for (int i = 0; i < r; i++) { 9 for (int j = 0; j < c; j++) { 10 scanf("%1d", &matrice[i*c+j]); 11 } 12 } 13 }
Non funziona del tutto ma l’idea è giusta
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 typedef struct stack { 6 int top; 7 int capacity; 8 char *array; 9 } *Stack; 10 11 typedef struct robot { 12 char sx; 13 char dx; 14 int freeSx; 15 int freeDx; 16 } *Robot; 17 18 char *read_line(void) { 19 char c; 20 int n = 0; 21 int size = 2; 22 char *line = malloc(size); 23 while((c = getchar()) != EOF) { 24 if(n >= size) { 25 size *= 2; 26 line = realloc(line, size); 27 } 28 if (c == '\n') { 29 line[n] = '\0'; 30 break; 31 } 32 line[n++] = c; 33 34 } 35 return line; 36 } 37 38 int check(Robot robot, Stack pila) {
80 if(!(robot->freeSx)&&robot->freeDx) { //se sx occupato e dx libero 81 if((pila->top != -1)&&(pila->array[pila->top] != robot->sx)) { 82 robot->dx = pila->array[pila->top]; 83 printf("dx pop %c\n", robot->dx); 84 robot->freeDx = 0; 85 pila->top--; 86 coppie += check(robot, pila); 87 } else { 88 robot->dx = nastro[i]; 89 printf("dx %c\n", robot->dx); 90 robot->freeDx = 0; 91 i++; 92 coppie += check(robot, pila); 93 } 94 } 95 } 96 97 printf("%d ", coppie); 98 if(!(robot->freeSx)){ 99 printf("%c ", robot->sx); 100 } else { 101 printf("2 "); 102 } 103 if(pila->top != -1) { 104 for (int j = 0; j<=pila->top; j++) { 105 printf("%c ", pila->array[j]); 106 } 107 } 108 109 printf("\n"); 110 111 if(robot->freeSx && robot->freeDx && pila->top == -1) { 112 return 0; 113 } else { 114 return 1; 115 } 116 117 118 } 119 120 int main() {
121 printf("%d\n", robot()); 122 }
Procedura divisa in 3 fasi:
4 Liste doppiamente concatenate
Le liste doppiamente concatenate sono come le liste concatenate singolarmente, con la differenza che i nodi hanno anche un puntatore al nodo precedente.
Costo O(1)
1 struct node *add_to_list(int n, struct node *list) { 2 struct node *new_node = malloc(sizeof(struct node)); 3 new_node -> data = n; 4 new_node -> prev = NULL; 5 new_node -> next = head; 6 head -> prev = new_node; 7 head = new_node; 8 return new_node; 9 }
Costo O(n)
1 struct node *search(struct node *list, int item) { 2 while (list != NULL) { 3 if(list -> data == item) { 4 return list; 5 } 6 list = list -> next; 7 } 8 return NULL; 9 }
Costo O(1)
1 void *beginning_delete(struct node *first) { 2 if (first == NULL) { 3 return; 4 } 5 if (first -> next == NULL) { //se il primo elemento è l'unico della lista 6 free(first); 7 } else { 8 first = first -> next; 9 free(first -> prev); 10 first -> prev = NULL; 11 } 12 }
Costo O(1)
1 void last_delete(struct node *last) { 2 if (last == NULL) { 3 return; 4 } else if (last -> prev == NULL) { //se l'ultimo elemento è l'unico della lista 5 free(last); 6 } else { 7 last = last -> prev; 8 free(last -> next); 9 last -> next = NULL; 10 } 11 }
5 Stack (Pila)
Le pile sono delle strutture dati con organizzazione LIFO (Last-In-First-Out). Possono essere implementate tramite array o tramite liste lineari. Sono preferibili le liste concatenate singolarmente.
Le operazioni che possono essere eseguite su una pila sono:
6 Queue (Coda)
Le code sono delle strutture dati con organizzazione FIFO (First-In-First-Out). Possono essere imple- mentate tramite array o tramite liste concatenate. Sono preferibili le liste doppiamente concatenate.
Le operazioni che possono essere eseguite su una coda sono:
7 Alberi
Gli alberi sono collezioni non vuote di nodi e archi. Ricorsivamente un albero è una foglia o una radice connessa ad un insieme di alberi. Alcuni termini rilevanti quando si parla di alberi:
8 Alberi binari
Gli alberi binari sono una tipologia di alberi ordinati. Un albero binario è una foglia oppure una radice connessa ad un albero binario destro e ad un albero binario sinistro. ALcune proprietà degli alberi binari:
Un albero binario può essere visitato con visite
1 typedef struct BiTreeNode_ { 2 void *data; 3 struct BiTreeNode_ *left; 4 struct BiTreeNode_ *right; 5 } BiTreeNode;
1 void bitree_init(BiTree tree, void (destroy)(void *data)) { 2 // Initialize the binary tree. 3 tree->size = 0; 4 tree->destroy = destroy; 5 tree->root = NULL; 6 return; 7 }
1 void bitree_destroy(BiTree *tree) { 2 // Remove all the nodes from the tree. 3 bitree_rem_left(tree, NULL); 4 // No operations are allowed now, but clear the structure as a precaution. 5 memset(tree, 0, sizeof(BiTree)); 6 return; 7 }
1 int bitree_ins_left(BiTree *tree, BiTreeNode *node, const void *data) { 2 BiTreeNode *new_node, **position; 3 // Determine where to insert the node 4 if (node == NULL) { 5 // Allow insertion at the root only in an empty tree. 6 if (bitree_size(tree) > 0) 7 return -1; 8 position = &tree->root; 9 } else { 10 // Normally allow insertion only at the end of a branch. 11 if (bitree_left(node) != NULL) 12 return -1; 13 position = &node->left; 14 } 15 // Allocate storage for the node. 16 if ((new_node = (BiTreeNode *)malloc(sizeof(BiTreeNode))) == NULL) 17 return -1;
18 // Insert the node into the tree. 19 new_node->data = (void *)data; 20 new_node->left = NULL; 21 new_node->right = NULL; 22 *position = new_node; 23 // Adjust the size of the tree to account for the inserted node. 24 tree->size++; 25 return 0; 26 }
Per l’inserimento a destra basta sostituire left con right alle righe 11 e 13
1 void bitree_rem_left(BiTree *tree, BiTreeNode *node) { 2 BiTreeNode *position; 3 // Do not allow removal from an empty tree. 4 if (bitree_size(tree) == 0) 5 return; 6 // Determine where to remove nodes. 7 if (node == NULL) 8 position = &tree->root; 9 else 10 position = &node->left; 11 // Remove the nodes. 12 if (position != NULL) { 13 bitree_rem_left(tree, position); 14 bitree_rem_right(tree, position); 15 if (tree->destroy != NULL) { 16 // Call a user-defined function to free dynamically allocated data. 17 tree->destroy((position)->data); 18 } 19 free(position); 20 *position = NULL; 21 // Adjust the size of the tree to account for the removed node. 22 tree->size--; 23 } 24 return; 25 }
Per l’inserimento a destra basta sostituire left con right alla riga 10