Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- typedef struct nodo *lista;
- typedef struct nodo{
- int val;
- lista succ;
- } nodo;
- lista insCoda(lista L,int el)
- {
- nodo *n,*temp=L;
- n=malloc(sizeof(nodo));
- if(n==NULL) return L;
- n->val=el;
- n->succ=NULL;
- if(L==NULL) return n;
- while(temp->succ!=NULL) temp=temp->succ;
- temp->succ=n;
- return L;
- }
- lista leggiLista(lista L)
- {
- int val;
- char a[2];
- while(1)
- {
- printf("\nInserisci elemento: ");
- scanf("%d",&val);
- L=insCoda(L,val);
- printf("\nInserire un altro elemento? (n per fermarti): ");
- scanf("%s",a);
- if(a[0]=='n') break;
- }
- return L;
- }
- void stampaLista(lista L)
- {
- printf("INIZIO ");
- while(L!=NULL)
- {
- printf("%d -> ",L->val);
- L=L->succ;
- }
- printf("FINE\n");
- }
- lista insTesta(lista L,int el)
- {
- nodo *n;
- n=malloc(sizeof(nodo));
- if(n==NULL) return NULL;
- n->val=el;
- if(L==NULL)
- {
- n->succ=NULL;
- return n;
- }
- n->succ=L;
- return n;
- }
- lista invLista(lista L)
- {
- lista M=NULL;
- while(L!=NULL)
- {
- M=insTesta(M,L->val);
- L=L->succ;
- }
- return M;
- }
- int main(void)
- {
- lista L=NULL;
- L=leggiLista(L);
- stampaLista(L);
- L=invLista(L);
- stampaLista(L);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement