








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!









#include <stdio.h> #include <stdlib.h> // node creation struct Node { int data; struct Node* next; struct Node* prev; }; ————————— // insert node at the front void insertFront(struct Node** head, int data) { // allocate memory for newNode struct Node* newNode = (struct Node)malloc(sizeof(struct Node)); // assign data to newNode newNode->data = data; // make newNode as a head newNode->next = (head); // assign null to prev newNode->prev = NULL; // previous of head (now head is the second node) is newNode if ((head) != NULL) (head)->prev = newNode; // head points to newNode (head) = newNode; } ————————— // insert a node after a specific node void insertAfter(struct Node prev_node, int data) { // check if previous node is null if (prev_node == NULL) { printf("previous node cannot be null"); return; } // allocate memory for newNode struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // assign data to newNode newNode->data = data; // set next of newNode to next of prev node newNode->next = prev_node->next; // set next of prev node to newNode prev_node->next = newNode; // set prev of newNode to the previous node newNode->prev = prev_node;
// set prev of newNode's next to newNode if (newNode->next != NULL) newNode->next->prev = newNode; } ————————— // insert a newNode at the end of the list void insertEnd(struct Node** head, int data) { // allocate memory for node struct Node* newNode = (struct Node)malloc(sizeof(struct Node)); // assign data to newNode newNode->data = data; // assign null to next of newNode newNode->next = NULL; // store the head node temporarily (for later use) struct Node temp = head; // if the linked list is empty, make the newNode as head node if (head == NULL) { newNode->prev = NULL; head = newNode; return; } // if the linked list is not empty, traverse to the end of the linked list while (temp->next != NULL) temp = temp->next; // now, the last node of the linked list is temp // assign next of the last node (temp) to newNode temp->next = newNode; // assign prev of newNode to temp newNode->prev = temp; } ————————— // delete a node from the doubly linked list void deleteNode(struct Node* head, struct Node* del_node) { // if head or del is null, deletion is not possible if (head == NULL || del_node == NULL) return; // if del_node is the head node, point the head pointer to the next of del_node if (head == del_node) *head = del_node->next; // if del_node is not at the last node, point the prev of node next to del_node to the previous of del_node if (del_node->next != NULL) del_node->next->prev = del_node->prev; // if del_node is not the first node, point the next of the previous node to the next node of del_node
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; }