public class List { public List() { head = null; // Start with an empty list of BankAccount objects } // ******************************************************************* // insert(amount): make an object and insert into list at this.head // ******************************************************************* public void insert(double amount) { BankAccount elem; elem = new BankAccount(amount); // Make BankAccount object if ( this.head == null || amount <= this.head.getBalance() ) { // Insert in empty list or at the start of list elem.setNext(head); head = elem; } else { // Insert after the start of the (non-empty) list BankAccount p1, p2; // Find enclosing elements p1 & p2 p1 = head; p2 = head.getNext(); while ( p2 != null && amount > p2.getBalance() ) { p1 = p2; p2 = p2.getNext(); } // Link new element between p1 & p2 p1.setNext(elem); elem.setNext(p2); } } // Method returns a String with balances in the whole list public String toString() { String out = ""; BankAccount p; p = head; while (p != null) { out = out + "[" + p.getBalance() + "] "; p = p.getNext(); // move down the chain !!! } return(out); } }