### Inserting a node at the tail of a list

• Inserting at the tail of a non-empty list --- general case (head is not updated)

• Before the insertion takes place:

• We have a non-empty list and a Node object (with a valid value 4):

• After the insertion has taken place:

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

Note:

 You do not know which node is the last node in the linked list !!!

• Therefore, we must first find the last node in the list in order to make the necessary links !!!

• Algorithm in Pseudo code:

 ``` 1. Use a reference variables ptr to traverse the list: 2. Initially: ptr points to the first Node 3. While ( ptr ≠ last node ) do: { move ptr forward (to the next node) } 4. Link the new node AFTER ptr (ptr points to the last node !) ```

Note:

 The last node is the node whose "next" field == null

• Algorithm in Java:

 ``` Node ptr; /* ========================================== 1. Find the last node in the List ========================================== */ ptr = head; // start the search at head while ( ptr.next != null ) // Stop when you found: ptr.next == null ptr = ptr.next; // Go to next node if ptr.next != null // When program reaches this point, we have: // ptr points to the last Node in list /* =========================================== 2. Link the new node AFTER the last node ============================================ */ ptr.next = e; // Make the last node point to e e.next = null; // Mark e as the last Node ```

• Inserting at the tail of a empty list --- special case (head is updated)

• Fact:

 Because an empty list does not have any element, inserting at the tail of an empty list is the same as inserting at the head of an empty list !!!

• Before the insertion takes place:

• We have a empty list and a Node object (with a valid value 4):

• 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:

 ``` head = help; // Link the Node at head help.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 method that insert at the tail of a list:

 ``` public class List { Node head; // Head is used to access the nodes // in the linked list /* ================================================ Constructor: initialize head to represent an empty list ================================================= */ public List() { head = null; // null means: no valid nodes } /* ===================================================== The put(x) method: store value x into the list - at tail ===================================================== */ public void put( double x ) { Node e; e = new Node( x ); // Make node with value x if ( head == null ) { /* ------------------------------ Insert e into an empty list ------------------------------ */ head = e; // e is the first elem. e.next = null; // Mark e as the last Node. } else { /* ------------------------------ Insert e into a non-empty list ------------------------------ */ Node ptr; /* ========================================== 1. Find the last node in the List ========================================== */ ptr = head; // start the search at head while ( ptr.next != null ) // Stop when you found: ptr.next == null ptr = ptr.next; // Go to next node if ptr.next != null // When program reaches this point, we have: // ptr points to the last Node in list /* =========================================== 2. Link the new node AFTER the last node ============================================ */ ptr.next = e; // Make the last node point to e e.next = null; // Mark e as the last Node } } } ```

• How to use the method:

 ``` public static void main( String[] args ) { List myList1 = new List(); // Create 2 empty lists List myList2 = new List(); myList1.put(1.0); // Insert in first list myList1.put(2.0); myList1.put(3.0); myList2.put(1.11); // Insert in second list myList2.put(2.22); myList2.put(3.33); } ```

Output:

 ``` myList1 = [] myList2 = [] myList1 = [1.0] myList2 = [] myList1 = [1.0 , 2.0 , 3.0] // Later values inserted AFTER first entry ! myList2 = [] myList1 = [1.0 , 2.0 , 3.0] myList2 = [1.11 , 2.22 , 3.33] ```

• 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