// #include // #include class List { int value; List next; }; public class delete { /* ==================================================== delete(h): delete the first elem from the list h return new list ==================================================== */ public static List delete (List h) { if ( h != null ) { return (h.next); // Return new list that starts at h.next // **** **** You MUST free(h) in C !!! ********* } else return null; } public static void main(String[] args) { List head = makeList( ); // Make an list for demo print(head); /* ------------------------------------------------- Delete the first list element from list at head ------------------------------------------------- */ head = delete(head); // Delete first elem in head print(head); /* ------------------------------------------------------- Delete the first list element from list at head AGAIN ------------------------------------------------------- */ head = delete(head); // Delete first elem in head print(head); } // Make a list static List makeList( ) { List h = null; List p; for ( int i = 1; i <= 5; i++ ) { p = new List( ); p.value = i; p.next = h; h = p; } return h; } // Print the list public static void print(List h) { while (h != null) { System.out.printf("%d ", h.value); h = h.next; } System.out.printf("\n\n"); } }