/* * File OrdList.java * * This class create a sorted list. * We don't want to redefine methods such as print() * which would be the same as in the 'List' class. That's why * we subclass List. * 'OrdList' is a subclass of 'List'. * 'OrdList' extends 'List'. * */ /* That where we use the keyword extends to specify */ /* that 'OrdList' is a subclass of 'List'. */ public class OrdList extends List { /* This is the constructor method. */ OrdList(int[] array) { super(); // Fist we call the constructor method of the superclass for (int i=0; i < array.length; i++) { this.insert(array[i]); } } /* When we insert a value. We need to place it at the correct */ /* place in the list. */ public void insert(int value) { Node toInsert = new Node(); Node current = this.getFirst(); boolean InsertDone = false; if ( current == null ) { toInsert.setValue(value); setFirst(toInsert); } else { while ( current != null && !InsertDone) { if ( current.getValue() > value ) { // Look for correct place to insert. /* Do the insertion here. */ toInsert.setValue(current.getValue()); current.setValue(value); toInsert.setNext(current.getNext()); current.setNext(toInsert); InsertDone=true; } current=current.getNext(); } if (! InsertDone) { append(value); } } } }