#include #include struct List // This is the List class definition in C { int value; struct List *next; }; struct List *insert( struct List *head, struct List *e ); struct List *delete (struct List *h); void print(struct List *h); // We must declare print to avoid compile error void main(int argc, char *argv[]) { struct List *head = NULL; // Nul pointer NULL instead of null struct List *p; for ( int i = 0; i < 10; i++ ) { p = malloc( sizeof(struct List) ); p->value = i; head = insert(head, p); // Insert p in head print(head); } for ( int i = 0; i < 10; i++ ) { head = delete(head); // Insert p in head print(head); } } struct List *insert( struct List *head, struct List *e ) { /* -------------------------------------------- Base case: insert at the tail of an empty -------------------------------------------- */ if ( head == NULL ) { e->next = NULL; // Mark e as the last list elem return(e); // e is the first list elem ! } else { /* =========================================================== Solve the problem USING the solution of a smaller problem =========================================================== */ head->next = insert( head->next, e ); // Link directly to helpSol return head; // Return MY solution } } /* ==================================================== delete(h): delete the LAST elem from the list h return new list ==================================================== */ struct List *delete (struct List *h) { if ( h == NULL ) return NULL; // deleting from empty list: returns an empty list else if ( h->next == NULL ) { // Delete from list with ONLY 1 element free(h); // De-allocate deleted element return NULL; } else { struct List * helpSol; helpSol = delete( h->next ); h->next = helpSol; return(h); } } // Print the list void print(struct List *h) { while (h != NULL) { printf("%d ", h->value); h = h->next; } printf("\n"); }