codice:
#include <stdio.h>
#include <stdlib.h>
struct linked{
int value;
struct linked *next;
} *first = NULL, *new;
void add(int);
void show(struct linked *);
struct linked * reverse(void);
void main(){
add(10);
show(first);
first = reverse();
show(first);
}
//Parametri: nr elementi da aggiungere
void add(int nr){
int i = 1;
while(nr--){
if(!(new = (struct linked *)malloc(sizeof(struct linked))))
abort();
(*(new)).value = i++;
(*(new)).next = first;
first = new;
}
}
//Parametri: puntatore al primo elemento
void show(struct linked *current){
for( ; current != NULL; current = (*(current)).next)
fprintf(stdout, "%d ", (*(current)).value);
fprintf(stdout, "\n");
}
//Valore di ritorno: puntatore al nuovo primo elemento
struct linked * reverse(){
struct linked *current = first, *temp, *newfirst = NULL;
while(current){
temp = current->next;
current->next = newfirst;
newfirst = current;
current = temp;
}
return newfirst;
}
10 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10