#include struct nodo_s { int valore; struct nodo_s * next; }; struct nodo_s * head=NULL; void mostra_lista() { struct nodo_s * scan; scan = head; /* if(scan==NULL)return; */ printf("Visualizzazione degli elementi della lista\n"); while(scan != NULL) { printf("Il nodo contiene %d\n",scan->valore); scan = scan->next; /* portati sul prossimo nodo */ } printf("Fine\n"); } int main(int argc, char *argv[]) { struct nodo_s * tmp; struct nodo_s * salvato; mostra_lista(); /* inserire nodo nella lista inizialmente vuota*/ tmp = (struct nodo_s *)malloc(sizeof(struct nodo_s)); tmp->valore = 5; tmp->next = NULL; head = tmp; mostra_lista(); tmp = (struct nodo_s *)malloc(sizeof(struct nodo_s)); tmp->valore = 2; tmp->next = head; head = tmp; mostra_lista(); tmp = (struct nodo_s *)malloc(sizeof(struct nodo_s)); tmp->valore = 7; tmp->next = NULL; head->next->next = tmp; /* h->2->5->7->||| */ mostra_lista(); tmp = (struct nodo_s *)malloc(sizeof(struct nodo_s)); tmp->valore=4; /* SBAGLIATO */ /* head->next=tmp; */ /* CORRETTO 1 */ tmp->next=head->next; head->next = tmp; /* CORRETTO 2 (peggiore) */ /* salvato=head->next; head->next = tmp; tmp->next = salvato; */ mostra_lista(); /* rimozione del terzo nodo (contiene 5) */ salvato=head->next->next->next; /* ricorda pos. nodo 7 (quarto) */ free(head->next->next); /* rilascia nodo 5 (terzo) */ head->next->next = salvato; /* collega nodo 7 come succ. del nodo 4 (secondo) */ mostra_lista(); system("Pause"); return 0; } #include struct nodo_s { int valore; struct nodo_s * next; struct nodo_s * prev; }; struct nodo_s * head=NULL; struct nodo_s * tail=NULL; void mostra_lista() { struct nodo_s * scan; scan = head; /* if(scan==NULL)return; */ printf("Visualizzazione degli elementi della lista\n"); while(scan != NULL) { printf("Il nodo contiene %d\n",scan->valore); scan = scan->next; /* portati sul prossimo nodo */ } printf("Fine\n"); } void mostra_lista_contrario() { struct nodo_s * scan; scan = tail; /* if(scan==NULL)return; */ printf("Visualizzazione A RITROSO degli elementi della lista\n"); while(scan != NULL) { printf("Il nodo contiene %d\n",scan->valore); scan = scan->prev; /* portati sul prossimo nodo */ } printf("Fine\n"); } void inserisci_automaticamente_ordinato(int val) { struct nodo_s * scan=NULL; struct nodo_s * tmp=NULL; /* trovare DOVE inserire il nuovo nodo in base al valore */ scan=head; while(scan!=NULL) { if(scan->valore>=val)break; scan=scan->next; } /* scan punta al primo nodo maggiore di val oppure e' NULL */ if(scan==NULL) { /* nuovo nodo va inserito in coda */ tmp=(struct nodo_s *)malloc(sizeof(struct nodo_s)); tmp->valore=val; tmp->next = NULL; tmp->prev = tail; if(tail!=NULL) { tail->next = tmp; } if(head==NULL) { head=tmp; } tail=tmp; } /* distinguere la situazione: testa, coda o generica? */ /* 3 casi da trattare */ } int main(int argc, char *argv[]) { struct nodo_s * tmp; struct nodo_s * salvato; int valore_da_inserire; mostra_lista(); mostra_lista_contrario(); /* inserire nodo nella lista inizialmente vuota*/ tmp = (struct nodo_s *)malloc(sizeof(struct nodo_s)); tmp->valore = 5; tmp->next = NULL; head = tmp; tmp->prev = NULL; tail = tmp; mostra_lista(); mostra_lista_contrario(); /* ins. nodo 2 prima del nodo 5 */ tmp = (struct nodo_s *)malloc(sizeof(struct nodo_s)); tmp->valore = 2; tmp->next = head; head = tmp; tmp->next->prev = tmp; tmp->prev = NULL; mostra_lista(); mostra_lista_contrario(); tmp = (struct nodo_s *)malloc(sizeof(struct nodo_s)); tmp->valore = 7; tmp->next = NULL; head->next->next = tmp; tmp->prev = head->next; tail=tmp; /* h->2->5->7->||| */ mostra_lista(); mostra_lista_contrario(); tmp = (struct nodo_s *)malloc(sizeof(struct nodo_s)); tmp->valore=4; tmp->next=head->next; head->next = tmp; tmp->next->prev=tmp; tmp->prev=head; mostra_lista(); mostra_lista_contrario(); /* rimozione del terzo nodo (contiene 5) */ salvato=head->next->next->next; /* ricorda pos. nodo 7 (quarto) */ free(head->next->next); /* rilascia nodo 5 (terzo) */ head->next->next = salvato; /* collega nodo 7 come succ. del nodo 4 (secondo) */ salvato->prev=head->next; mostra_lista(); mostra_lista_contrario(); printf("Dammi un valore da inserire in lista: "); scanf("%d",&valore_da_inserire); inserisci_automaticamente_ordinato(valore_da_inserire); mostra_lista(); mostra_lista_contrario(); system("Pause"); return 0; }