public void enqueue ( RepairDigramRecord digramRecord ) { // System.out.println("before == " + this.toString()); // if the same key element is in the queue - something went wrong with tracking... if ( elements . containsKey ( digramRecord . str ) ) { throw new IllegalArgumentException ( "Element with payload " + digramRecord . str + " already exists in the queue..." ) ; } else { // create a new node RepairQueueNode nn = new RepairQueueNode ( digramRecord ) ; // System.out.println(nn.payload); // place it into the queue if it's empty if ( this . elements . isEmpty ( ) ) { this . head = nn ; } // if new node has _higher than_ or _equal to_ the head frequency... this going to be the new head else if ( nn . getFrequency ( ) >= this . head . getFrequency ( ) ) { this . head . prev = nn ; nn . next = this . head ; this . head = nn ; } // in all other cases find an appropriate place in the existing queue, starting from the head else { RepairQueueNode currentNode = head ; while ( null != currentNode . next ) { // the intent is to slide down the list finding a place at new node is greater than a node // a tracking pointer points to... // ABOVE we just checked that at this loop start that the current node is greater than new // node // if ( nn . getFrequency ( ) >= currentNode . getFrequency ( ) ) { RepairQueueNode prevN = currentNode . prev ; prevN . next = nn ; nn . prev = prevN ; currentNode . prev = nn ; nn . next = currentNode ; break ; // the element has been placed } currentNode = currentNode . next ; } // check if loop was broken by the TAIL condition, not by placement if ( null == currentNode . next ) { // so, currentNode points on the tail... if ( nn . getFrequency ( ) >= currentNode . getFrequency ( ) ) { // insert just before... RepairQueueNode prevN = currentNode . prev ; prevN . next = nn ; nn . prev = prevN ; currentNode . prev = nn ; nn . next = currentNode ; } else { // or make a new tail nn . prev = currentNode ; currentNode . next = nn ; } } } // also save the element in the index store this . elements . put ( nn . payload . str , nn ) ; } // System.out.println("before == " + this.toString()); }
Places an element in the queue at the place based on its frequency .