Recent Posts
None | 39 sec ago
C | 1 min ago
None | 1 min ago
None | 1 min ago
None | 2 min ago
C | 2 min ago
None | 2 min ago
C | 2 min ago
None | 3 min ago
None | 5 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By joey on the 9th of Feb 2010 11:58:47 PM Download | Raw | Embed | Report
  1. public void remove(DataElement deleteItem)
  2.     {
  3.         if ( first == null )
  4.         {
  5.             // List empty, report it as an error
  6.             System.err.println("Cannot delete from an empty list.");
  7.         }
  8.         else
  9.         {
  10.             // So we know the list is not empty.
  11.             if ( first.data.equals(deleteItem) ) {
  12.                 // The item we wish to delete is the first element
  13.                 // of the list, simply move head of the list to the
  14.                 // next element in the list.
  15.                 first = first.link;
  16.                 if ( first == null ) {
  17.                     // There was only one element in the list,
  18.                     // so make the list empty
  19.                     last = null;
  20.                 }
  21.                 count--;
  22.             }
  23.             else
  24.             {
  25.                 // Okay so we have to search the list for the item
  26.                 // we wish to delete.
  27.                 boolean found = false; // assume worst case
  28.                 // We know it’s not the first element of the list,
  29.                 // so lets start looking from the second element.
  30.                 Node current = first.link;
  31.                 // In order to remove the node we need to keep track
  32.                 // of the node which proceeds the current node so
  33.                 // we can modify its link correctly.
  34.                 // As we are staring with the second node,
  35.                 // that must be the first node.
  36.                 Node previous = first;
  37.                 // While we have not found the node, and we have not
  38.                 // reached the end of the list.
  39.                 while ( current != null && ! found )
  40.                 {
  41.                     if ( current.data.equals(deleteItem) )
  42.                     {
  43.                         // we have found our target
  44.                         found = true;
  45.                     }
  46.                     else
  47.                     {
  48.                         // keep looking
  49.                         previous = current;
  50.                         current = current.link;
  51.                     }
  52.                 }
  53.                 // Did we find the blasted thing
  54.                 if ( found )
  55.                 {
  56.                     // Yes, the remove it !
  57.                     // Modify the link on the previous node to refer
  58.                     // to the next node, skipping the current node
  59.                     // all together.
  60.                     previous.link = current.link;
  61.                     // If it was the last node, make the previous
  62.                     // node the last node.
  63.                     if ( last == current )
  64.                     {
  65.                         last = previous;
  66.                     }
  67.                     count--;
  68.                 }
  69.                 else
  70.                 {
  71.                     // Target node was not found !
  72.                     // Report an error to the standard error output
  73.                     System.out.println("Item to be deleted is "
  74.                     + "not in the list.");
  75.                 }
  76.             }
  77.             // We really should invoke the garbage collector,
  78.             // but it’s not worth it for a single object.
  79.         }
  80.     }
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: