public class ContainerTerminal { int rows; // Crane lines int cols; // Container sizes public Container[] rowHead; public Container[] colHead; public ContainerTerminal(int r, int c) { rows = r; cols = c; rowHead = new Container[r]; colHead = new Container[c]; } public void add(Container cont, int crane) { // Place container at end of crane line first Container current= rowHead[crane]; cont.right= null; // Container always placed at end of crane line Container prev= null; if (current == null) rowHead[crane] = cont; else { while (current != null) { prev= current; current = current.right; } prev.right= cont; } // Place container at end of size line by weight order int size= cont.size; int index= getSizeIndex(size); int weight= cont.weight; current= colHead[index]; prev= null; if (current == null) colHead[index]= cont; else { while (current != null) { if (current.weight <= weight) { // Insert cont before current cont.down= current; if (colHead[index] == current) // Cont is first element in list colHead[index]= cont; else prev.down= cont; return; } else { prev= current; current= current.down; } } // If we reach end of list, cont has greatest weight prev.down= cont; // cont.down initialized to null already return; } } public Container getFirst(int crane) { return rowHead[crane]; } public boolean remove(String ID) { // Look for container in crane list. Not guaranteed to be found Container remove= null; // Reference to container to remove Container prevRight= null; // Ref to previous container, whose right points to it int size= 0; int crane= -1; boolean found = false; for (int i=0; i < rowHead.length; i++) { // Search thru all crane lines Container current= rowHead[i]; prevRight = null; // reset the prevRight when starting a new row while (current != null) { if (current.id.equals(ID)) { remove= current; size= current.size; crane= i; found = true; break; // Only breaks out of inner loop; would be better to break entire loop } else { prevRight= current; current= current.right; } } if (found) break; } if (remove== null) return false; // If while loop didn't break, ID not found // Find the container in the size list. Guaranteed to be found. int index= getSizeIndex(size); if (colHead[index]== remove) colHead[index]= remove.down; // If only one container in size list else { Container currDown= colHead[index]; while (currDown != null) { if (currDown.down== remove) break; else currDown= currDown.down; } currDown.down = remove.down; // Splice around container in size list remove.down = null; } // Remove the container from crane list: // If prevRight is null, the record to remove must be the first one in the list // so we set rowHead to remove.right. Otherwise we set prevRight.right to remove.right: if (prevRight == null) { rowHead[crane]= remove.right; remove.right = null; } else { prevRight.right= remove.right; remove.right = null; } return true; } public String printCraneLine(int crane) { String print = "Line for crane "+crane+" : "; Container current= rowHead[crane]; while (current != null) { print += current.id+" "; current= current.right; } print += "\n"; return print; } public String printContainerSizeLine (int size) { String print = "Line for container size "+size+" : "; int index= getSizeIndex(size); Container current= colHead[index]; while (current != null) { print += "("+current.id+", "+current.weight+")"; current= current.down; } print += "\n"; return print; } public int getWeightBySize(int size) { int totalWeight= 0; int index= getSizeIndex(size); Container current= colHead[index]; while (current != null) { totalWeight += current.weight; current= current.down; } return totalWeight; } public static int getSizeIndex(int size) { int index; if (size == 10) index= 0; else if (size == 20) index= 1; else // Returns 2 even if invalid size entered; should throw exception // but it's too much work in this homework index= 2; return index; } }