### Inserting a node in an ordered list

• Ordered lists

• Ordered list:

 Ordered list = a list whose nodes are ordered by the values stored inside each node

Example:

• An ordered list:

The values stored in the nodes are non-decreasing

• Not an ordered list:

The values 7, 4, 12 are not ordered.

• Inserting in an ordered list --- general case (head is not updated)

• Example: insert a node e containing the value 9 into this ordered list:

• Before we can perform the insertion:

• First, we have to find the location where the inserted node must occupy:

We must find the nodes ptr1 and ptr2 that "embraces" the new node

(the new node is sandwiched between ptr1 and ptr2)

• How to find these nodes:

• Use 2 pointer variables: ptr1 and ptr2

• ptr1 always points to the node that is prior to ptr2

• ptr1 and ptr2 will initially point to the first pair of nodes:

• ptr2.value is the first value that is the inserted value

So:

 ``` if ( ptr2.value >= insertedValue ) stop the search ! ```

Algorithm:

 ``` /* ================================================================= Find the node ptr1 and ptr2 that "embraces" the inserted value ================================================================= */ ptr1 = head; // ptr1 points to the first node ptr2 = head.next; // ptr2 points to the second node while ( ptr2 != null ) // Loop until no more nodes.... { if ( ptr2.value >= e.value ) { break; // Stop ! We have found the location !! } ptr1 = ptr2; // Move ptr1 and ptr2 to the next pair ptr2 = ptr2.next; } ```

• Insert the new node between the nodes ptr1 and ptr2:

• List before insertion:

• List after insertion:

• Statements to achieve this:

 ``` ptr1.next = e; // Link e after ptr1 e.next = ptr2; // Link ptr2 after e ```

• After the insertion has taken place, the list will look like this:

• I deleted the help variables used in the insert operation to let you see the actual list more clearly:

Note:

 This list is ordered: 4, 7, 9, 12 !!!

• Algorithm in Java (I simply put the 2 code segments above together !):

 ``` /* ================================================================= Find the node ptr1 and ptr2 that "embraces" the inserted value ================================================================= */ ptr1 = head; // ptr1 points to the first node ptr2 = head.next; // ptr2 points to the second node while ( ptr2 != null ) // Loop until no more nodes .... { if ( ptr2.value >= e.value ) { break; // Stop ! We have found the location !! } ptr1 = ptr2; // Move ptr1 and ptr2 to the next pair ptr2 = ptr2.next; } /* ============================================== Link new node e (elem) between ptr1 and ptr2 ============================================== */ ptr1.next = e; // e follows ptr1 e.next = ptr2; // ptr2 follows e ```

• Exception cases

• The general case takes cares of almost every scenario...

Except these 2 cases where the new node is inserted at the start of the list:

• The first node contains a value that is larger than the inserted value:

• The list is empty:

• Inserting in an ordered list --- special case #2 (head is updated)

• The first exception case:

• How to detect the special case:

 ``` head.value < insertedValue ```

• What must you do in when this happens:

• Before th einsertion:

• After th einsertion:

• Statements that accomplishes this:

 ``` if ( e.value ≤ head.value ) { e = head;; // The whole list is now "after" node e head = e; // Node e is now the first node } ```

• Inserting in an ordered list --- special case #2 (head is updated)

• Special case 2: empty list

• Before the insertion takes place:

• We have a empty list and a Node object:

• After the insertion has taken place:

• We must have a list where the Node object (with a valid value 4) is chained like this:

• Statements that accomplishes this:

 ``` if ( head == null ) { head = e; // Link the Node at head e.next = null; // Mark the Node as "last node" } ```

• The put() method to insert at the start of a list

• I can now show you the complete method that insert into an ordered list:

 ```public class List { /* ========================================== Node "inner class" ========================================== */ public class Node { double value; Node next; public Node( double x ) { value = x; next = null; } public String toString() { return "" + value; } } /* ========================================================== put(x ): store value x into the list in increasing order ========================================================== */ public void put( double x ) { Node e; e = new Node( x ); // Make node with value x if ( head == null ) { /* -------------------------------------------- Special case 2: Insert e into an empty list -------------------------------------------- */ head = e; // e is the first elem. e.next = null; // Mark e as the last Node. } else if ( x <= head.value ) { /* ---------------------------------------------- Special case 1: Insert e at start of list ---------------------------------------------- */ e.next = head; // Make e point to the rest of the list head = e; // Make e the new first elem. } else { /* --------------------------------------------- General case --------------------------------------------- */ Node ptr1, ptr2; /* ========================================= First, we must find this Node ptr1: value=x [..] --> [..] --> [..] --> [..] ^ ^ | | ptr1 ptr2 The value x goes between ptr1 and ptr2 ========================================= */ ptr1 = head; ptr2 = head.next; while ( ptr2 != null ) // Loop until no more nodes .... { if ( ptr2.value >= e.value ) { break; // Stop ! We have found the location !! } ptr1 = ptr2; // Move ptr1 and ptr2 to the next pair ptr2 = ptr2.next; } /* ================================================= Now link [x] between ptr1 and ptr2: value=x [..] --> [..] --> [..] --> [..] ^ ^ | | ptr1 ptr2 e = [x] ================================================= */ ptr1.next = e; // Make ptr1 point to e e.next = ptr2; // Make e point to ptr2 } } } ```

• How to use the method:

 ``` public static void main( String[] args ) throws Exception { List myList = new List(); double x; myList.put(5.0); System.out.println("put(5.0): " + "myList = " + myList); myList.put(1.0); System.out.println("put(1.0): " + "myList = " + myList); myList.put(3.0); System.out.println("put(3.0): " + "myList = " + myList); } ```

Output:

 ``` put(5.0): myList = [5.0] put(1.0): myList = [1.0 , 5.0] put(3.0): myList = [1.0 , 3.0 , 5.0] // Ordered !!! ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link(s) and save in a scratch directory To compile:   javac testProg.java To run:          java testProg