Prendi questo come spunto magaricodice:/* ### Copyright (c) 2004 Luca Francesca ### This script is provided under the terms of the GNU General Public License ### (see http://www.gnu.org/licenses/gpl.html for the full license text.) */ #include <stdio.h> #include <time.h> #include <stdlib.h> #include "Common.h" typedef struct _ListNode *List; typedef int base_type; struct _ListNode { base_type data; List next; }; List Init(base_type val); List Add(base_type val, List aux); void Delete(List aux); void Show(List P); int main(int argc, char *argv[]) { srand(time(NULL)); List root = Init(10); int i = 0, ival = 0; for(i; i < 10;++i) { ival = i + (rand()%10); root = Add(ival, root); } Show(root); Delete(root); SAFE_DELETE(root) ExitFunction(); return 0; } List Init(base_type val) { List tmp; tmp = malloc(sizeof(struct _ListNode)); tmp->data = val; tmp->next = NULL; return tmp; } List Add(base_type val, List aux) { if(aux == NULL) { aux = malloc(sizeof(struct _ListNode)); aux->data = val; aux->next = NULL; } else aux->next = Add(val, aux->next); return aux; } void Show(List aux) { while(aux != NULL) { fprintf(stdout, "%d \n", aux->data); aux = aux->next; } } void Delete(List aux) { while(aux != NULL) { free(aux->next); free(aux); } aux = NULL; }![]()

Rispondi quotando
