/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ed_ficha08; /** * * @author franc */ public class OrderedList extends LinkedList implements OrderedListADT { LinearNode front; LinearNode rear; private int size; public void add(T element) { LinearNode newNode = new LinearNode((Comparable) element); //If there's no elements yet if (this.size == 0) { this.front = new LinearNode((Comparable) element); this.rear = new LinearNode((Comparable) element); this.rear = this.front = newNode; this.front.setNext(this.rear); this.rear.setNext(null); this.size++; } else { //There are elements in the list already LinearNode searchingNode = this.front; LinearNode previousNode = new LinearNode(); //Iterate the list while (searchingNode != null) { if (searchingNode.compareTo(newNode) < 0) { previousNode = searchingNode; searchingNode = searchingNode.getNext(); } else { //If it's the first item in the order, it has to be inserted in the front if (searchingNode.equals(this.front)) { LinearNode temp = searchingNode; this.front = newNode; this.front.setNext(temp); } else { //If it belongs in the middle newNode.setNext(previousNode.getNext()); previousNode.setNext(newNode); } this.size++; return; } } //It's the last item in the order this.rear.setNext(newNode); this.rear = newNode; this.size++; } } }