Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- struct nodo{
- int info;
- struct nodo *next;
- };
- typedef struct nodo Nodo;
- Nodo* inserisciNodoInTesta(Nodo* first, int value);
- void stampaLista(Nodo* first);
- void stampaListaRicorsiva(Nodo* first);
- void eliminaLista(Nodo* first);
- void eliminaListaRicorsiva(Nodo* first);
- Nodo* inserisciNodoInCoda(Nodo* first);
- int main()
- {
- Nodo* lista=NULL;
- int n,i,num;
- srand(time(NULL));
- printf("Inserisci il numero di elementi\n");
- scanf("%d",&n);
- for(i=0;i<n;i++){
- printf("Inserisci valore\n");
- scanf("%d",&num);
- lista=inserisciNodoInTesta(lista, num);
- }
- stampaLista(lista);
- printf("\n");
- stampaListaRicorsiva(lista);
- printf("\n");
- lista=inserisciNodoInCoda(lista);
- stampaLista(lista);
- return 0;
- }
- Nodo* inserisciNodoInTesta(Nodo* first, int value){
- Nodo* tmp=NULL;
- tmp=(Nodo*)malloc(sizeof(Nodo));
- if(tmp==NULL){
- exit(-1);
- }
- tmp->info=value;
- tmp->next=first;
- return tmp;
- }
- void stampaLista(Nodo* first){
- while(first!=NULL){
- printf("%d ",first->info);
- first=first->next;
- }
- }
- void stampaListaRicorsiva(Nodo* first){
- if(first!=NULL){
- printf("%d ",first->info);
- stampaListaRicorsiva(first->next);
- }
- }
- void eliminaLista(Nodo* first){
- Nodo* tmp;
- while(first!=NULL){
- tmp=first->next;
- free(first);
- first=tmp;
- }
- }
- void eliminaListaRicorsiva(Nodo* first){
- if (first!=NULL){
- free(first);
- eliminaListaRicorsiva(first->next);
- }
- }
- Nodo* inserisciNodoInCoda(Nodo* first){
- Nodo* tmp;
- tmp=(Nodo*)malloc(sizeof(Nodo));
- if(tmp==NULL){
- exit(-1);
- }
- tmp->info=3;
- tmp->next=NULL;
- while(first->next!=NULL){
- first=first->next;
- }
- first->next=tmp;
- return first;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement