The effect of addLast(item) --- inserting item at the end of a linked list:
|
|
|
|
Let's implement the addLast(item) method:
// Inserts a new node to the end of this list public void addLast(T item) { if(isEmpty()) // Edge case: empty list { (1) Find the last element in the linked list (2) Make a new Node with data and link to the last element } else { Node |
Start the search at the first node in the linked list:
// Inserts a new node to the end of this list
public void addLast(T item)
{
if(isEmpty()) // Edge case: empty list
{
(1) Find the last element in the linked list
(2) Make a new Node with data and link to the last element
}
else
{
Node<T> current = first;
while(current.next!=null) // Find last lsit element
{
current = current.next;
}
current.next = new Node<T>(item,null); // Make list elem + link
}
}
|
Find the last element in the linked list: the next variable in this element is null
// Inserts a new node to the end of this list public void addLast(T item) { if(isEmpty()) // Edge case: empty list { (1) Find the last element in the linked list (2) Make a new Node with data and link to the last element } else { Node<T> current = first; while ( current.next != null ) // Find last list element { current = current.next; } current.next = new Node<T>(item,null); // Make list elem + link } } |
Make a new Node containing the item and next = null (because it will be the last element)
// Inserts a new node to the end of this list public void addLast(T item) { if(isEmpty()) // Edge case: empty list { (1) Find the last element in the linked list (2) Make a new Node with data and link to the last element } else { Node<T> current = first; while ( current.next != null ) // Find last list element { current = current.next; } new Node<T>(item,null); // Make list elem } } |
Link the new Node to the last element in the list:
// Inserts a new node to the end of this list public void addLast(T item) { if(isEmpty()) // Edge case: empty list { (1) Find the last element in the linked list (2) Make a new Node with data and link to the last element } else { Node<T> current = first; while ( current.next != null ) // Find last list element { current = current.next; } current.next = new Node<T>(item,null); // Make list elem + link } } |
Check for edge case(s):
// Inserts a new node to the end of this list public void addLast(T item) { if(isEmpty()) // Edge case: empty list { } else { Node<T> current = first; // null while ( current.next != null ) // CRASH (because current = null) { current = current.next; } current.next = new Node<T>(item,null); // Make list elem + link } } |
We must handle the edge case separately:
// Inserts a new node to the end of this list public void addLast(T item) { if( isEmpty() ) // Edge case: empty list { first = new Node<T>(item,null); // We studied this before.. //equivalent: addFirst(item) } else { Node<T> current = first; // not null while ( current.next != null ) { current = current.next; } current.next = new Node<T>(item,null); // Make list elem + link } } |
The effect of removeLast() --- removing an item at the end of a linked list:
|
|
|
|
|
Let's implement the removeLast( ) method:
// Delete the node at the end of this list
public T removeLast( )
{ // General case (take care of edge cases later)
// Find the last node while remembering the previous node
Node<T> current = first;
Node<T> previous = first;
while( current.next != null )
{
previous = current;
current = current.next;
}
// Unlink the last node (from its predecessor)
previous.next = null;
// Return item in the last node
return current.item;
}
|
Start the search at the first node:
// Delete the node at the end of this list
public T removeLast( )
{ // General case (take care of edge cases later)
// Find the last node while remembering the previous node
Node<T> current = first;
Node<T> previous = first;
while( current.next != null )
{
previous = current;
current = current.next;
}
// Unlink the last node (from its predecessor)
previous.next = null;
// Return item in the last node
return current.item;
}
|
Find the last node while remembering its previous node:
// Delete the node at the end of this list public T removeLast( ) { // General case (take care of edge cases later) // Find the last node while remembering the previous node Node<T> current = first; Node<T> previous = first; while( current.next != null ) { previous = current; current = current.next; } // Unlink the last node (from its predecessor) previous.next = null; // Return item in the last node return current.item;} |
Unlink the last node from the previous node:
// Delete the node at the end of this list public T removeLast( ) { // General case (take care of edge cases later) // Find the last node while remembering the previous node Node<T> current = first; Node<T> previous = first; while( current.next != null ) { previous = current; current = current.next; } // Unlink the last node (from its predecessor) previous.next = null; // Return item in the last node return current.item;} |
Return the item in the last node:
// Delete the node at the end of this list public T removeLast( ) { // General case (take care of edge cases later) // Find the last node while remembering the previous node Node<T> current = first; Node<T> previous = first; while( current.next != null ) { previous = current; current = current.next; } // Unlink the last node (from its predecessor) previous.next = null; // Return item in the last node return current.item;} |
Check for edge cases....
// Delete the node at the end of this list public T removeLast( ) { // Detect and handle edge case // General case (take care of edge cases later) ... code omitted for brevity... (The general case updates previous.next) } |
Edge case 1: empty list.... you cannot remove something from an empty list --- error.
// Delete the node at the end of this list public T removeLast( ) { // Detect and handle edge case if( isEmpty() ) // Edge case 1: empty list { // empty list can't remove anything throw new NoSuchElementException(); } // General case (take care of edge cases later) ... code omitted for brevity... (The general case updates previous.next) } |
Edge case 2: list with a single element ---> removal results in an empty list
// Delete the node at the end of this list public T removeLast( ) { // Detect and handle edge case if( isEmpty() ) // Edge case 1: empty list { // empty list can't remove anything throw new NoSuchElementException(); } if( first.next == null ) // Edge case 2: list has 1 elem { Node<T> ret = first; // Save for return first = null; // Unlink by updating first return ret.item; } // General case (take care of edge cases later) ... code omitted for brevity... updates previous.next } |
DEMO: demo/11-linked-list/03-insert+remove-end/Demo.java + GenericLinkedList.java
|
|