Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- typedef struct Nodo{ //typedef serve a dichiarare un nuovo tipo di variabile, in questo caso la variabile è struct nodo.
- char nome[20];
- char cognome[30];
- int anno; //Valore contenuto nella strutture, è il valore che andrò a modificare nelle funzioni e che si vede quando stampo la lista.
- Nodo *next; //è una variabile di tipo nodo che punta alla struttura successiva.
- };
- Nodo *inserimentoUtenteOrdinato(Nodo *, char[], char[], int);
- void ricercaUtente(Nodo *, char[]);
- void visualizzaLista(Nodo *);
- int main()
- {
- Nodo *testa;
- testa=0;
- int scelta,anno;
- char strNome[20];
- char strCognome[30];
- char ricercaNome[20];
- do{
- system("CLS");
- printf("1 -> Inserire utente.\n");
- printf("2 -> Ricerca utente.\n");
- printf("3 -> Visualizza rubrica.\n");
- printf("Scelta: ");
- scanf("%d",&scelta);
- fflush(stdin);
- switch(scelta)
- {
- case 1:{
- printf("Inserisci nome: ");
- scanf("%s",&strNome);
- fflush(stdin);
- printf("Inserisci cognome: ");
- scanf("%s",&strCognome);
- fflush(stdin);
- printf("Inserisci anno: ");
- scanf("%d",&anno);
- fflush(stdin);
- testa=inserimentoUtenteOrdinato(testa,strNome,strCognome,anno);
- break;
- }
- case 2:{
- printf("Inserisci il nome da cercare: ");
- scanf("%s",&ricercaNome);
- fflush(stdin);
- ricercaUtente(testa,ricercaNome);
- break;
- }
- case 3:{
- visualizzaLista(testa);
- break;
- }
- }
- getchar();
- }while(scelta!=0);
- return 0;
- }
- //Case 1
- Nodo *inserimentoUtenteOrdinato(Nodo *testa, char nome[20], char cognome[30], int _anno)
- {
- if(testa==0) //se la lista è vuota.
- {
- testa=(Nodo *)malloc(sizeof(Nodo)); //alloca una nuova struttura, creo lo spazio per la struttura. malloc crea un nuovo spazio della grandezza di Nodo.
- strcpy(testa->nome,nome);
- strcpy(testa->cognome,cognome);
- testa->anno=_anno;
- testa->next=0;
- }
- else //altrimenti quando ho già dei nodi nella lista
- {
- /*Nodo *tmp, *tmp2;
- char box[20];
- tmp=testa;
- testa=(Nodo *)malloc(sizeof(Nodo));
- strcpy(testa->nome,nome);
- strcpy(testa->cognome,cognome);
- testa->anno=_anno;
- testa->next=tmp;
- while(tmp->next!=0){
- tmp2=tmp;
- for(int i=0;i<strlen(tmp->nome);i++){
- if(strcmp(tmp->nome,tmp2->nome)!=0){
- strcpy(box,tmp->nome);
- strcpy(tmp->nome,tmp2->nome);
- strcpy(tmp2->nome,box);
- }
- }
- tmp=tmp->next;
- }*/
- Nodo *nuova, *previous, *current;
- nuova = (Nodo *)malloc(sizeof(Nodo));
- strcpy(nuova->nome,nome);
- strcpy(nuova->cognome,cognome);
- nuova->anno=_anno;
- /*if (nuova->nome == NULL) {
- printf ("Memoria non disponibile\n");
- exit(1);
- }*/
- previous = NULL;
- current = testa;
- while(current != NULL && strcmp(nome, current->nome) > 0){
- previous = current;
- current = current->next;
- }
- if(previous == NULL){
- nuova->next = testa;
- testa = nuova;
- }
- else{
- previous->next = nuova;
- nuova->next = current;
- }
- }
- return testa;
- }
- //Case 2
- void ricercaUtente(Nodo *testa, char nomeDaCercare[20])
- {
- if(testa==0){
- printf("Lista vuota!");
- getchar();
- }
- else{
- Nodo *tmp;
- tmp=testa;
- while(tmp!=0){
- if(strcmp(tmp->nome,nomeDaCercare)==0){
- printf("%s %s %d\n",tmp->nome,tmp->cognome,tmp->anno);
- }
- tmp=tmp->next;
- }
- }
- }
- //Case 3
- void visualizzaLista(Nodo *testa)
- {
- if(testa==0)
- {
- printf("Lista Vuota!\n");
- getchar();
- }
- else
- {
- Nodo *tmp;
- printf("\n\nTesta: %d\n\n",testa); //Stampo il valore di testa.
- tmp=testa; //metto tmp uguale a testa
- while(tmp!=0) //giro finchè tmp è diverso da zero. Quando tmp è 0 esco dal ciclo. Nel while si incrementa dentro il ciclo (cnt++) e nel for direttamente nella prima riga.
- {
- printf("%d %s %s %d %d \n",tmp,tmp->nome, tmp->cognome, tmp->anno,tmp->next); //stampo il valore di tmp, il valore di val e il valore di next.
- tmp=tmp->next; //metto tmp uguale a tmp->next per passare al nodo successivo.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement