Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct nodo *lista;
- typedef struct nodo{
- int val;
- lista next;
- } nodo;
- lista listaVuota();
- lista inserTesta(lista L,int el);
- lista cancTesta(lista L);
- lista inserCoda(lista L,int el);
- void stampaLista(lista L);
- lista leggiLista();
- int main(void)
- {
- lista L;
- L=leggiLista();
- stampaLista(L);
- return 0;
- }
- lista listaVuota()
- {
- return NULL;
- }
- lista inserTesta(lista L,int el)
- {
- nodo *n;
- n=malloc(sizeof(nodo));
- if(n==NULL) return L; //CONTROLLO SE LO SPAZIO E' STATO EFFETTIVAMENTE ALLOCATO
- n->val=el;
- n->next=L;
- return n;
- }
- lista cancTesta(lista L)
- {
- nodo *temp;
- if(L==NULL) return L;
- temp=L;
- L=L->next;
- free(temp);
- return L;
- }
- lista inserCoda(lista L,int el)
- {
- nodo *n,*temp=L;
- if(L==NULL) return inserTesta(L,el);
- n=malloc(sizeof(nodo));
- if(n==NULL) return NULL; //CONTROLLO SE LA MEMORIA E' STATA ALLOCATA
- n->val=el;
- n->next=NULL;
- while(temp->next!=NULL) temp=temp->next;
- temp->next=n;
- return L;
- }
- void stampaLista(lista L)
- {
- printf("\nLa lista contiene gli elementi:\n");
- while(L!=NULL)
- {
- printf("%d\n",L->val); //oppure (*L).val
- L=L->next; //oppure (*L).next
- }
- printf("\n\n");
- }
- lista leggiLista(lista L)
- {
- lista temp;
- L=listaVuota();
- int val=1;
- while(1)
- {
- printf("\nInserisci prossimo valore (0 per terminare): ");
- scanf("%d",&val);
- if(!val) return L;
- temp=L; //--------
- L=inserTesta(L,val); //temp=inserCoda(L,val);
- if(temp==L) //if(temp==NULL)
- {
- printf("\nMEMORIA ESAURITA! INSERIMENTO TERMINATO.\n");
- return L;
- }
- //L=temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement