CS 163 Lecture-Module #10:
Linked Lists

(presented — but I think not clearly — in Sections 14.4-14.5 of our textbook
    - mixed with presentation of "Bag" vs. as an independent  class LinkedList
)

point here is for you to see more dynamic data structures

you've used arrays
regarding searching, I showed copying an array to a larger one — dynamic
    +----+----+----+----+----+----+----+----+
    | 17 | 20 | -2 |  5 | 10 | 33 |  6 | ...|
    +----+----+----+----+----+----+----+----+
       |    |    |    |    |    |    |  . . .
       V    V    V    V    V    V    V  . . .
    +----+----+----+----+----+----+----+----+----+
    | 17 | 20 | -2 |  5 | 10 | 33 |  6 | ...| NEW|
    +----+----+----+----+----+----+----+----+----+
why not allocate huge space?
        int[] ia = new int[];    // 
or vice-versa imagine work to remove an element:
                  .-.  .-.  .-.  .-.  .-.
                  | |  | |  | |  | |  | | . . .
                  V |  V |  V |  V |  V | . . .
    +----+----+----+----+----+----+----+----+----+
    | 17 | 20 | -2 |  5 | 10 | 33 |  6 | ...| ...|
    +----+----+----+----+----+----+----+----+----+
ArrayList  does this (could consider its source code)

advantages:
+ right amount of space
+ easy to add at end
+ even easier to add at front
    vs. array:
        .-.  .-.  .-.  .-.  .-.  .-.  .-.
        | |  | |  | |  | |  | |  | |  | | . . .
        | V  | V  | V  | V  | V  | V  | V . . .
    +----+----+----+----+----+----+----+----+----+
    |    |    |    |    |    |    |    | ...| ...|
    +----+----+----+----+----+----+----+----+----+
+ relatively easy to add in middle
    vs. array:
                   40
                    |  .-.  .-.  .-.  .-.
                    V  | |  | |  | |  | | . . .
                       | V  | V  | V  | V . . .
    +----+----+----+----+----+----+----+----+----+
    | ...| ...| 33 | 44 | 55 |    |    | ...| ...|
    +----+----+----+----+----+----+----+----+----+
+ easy to delete any item

linked list disadvantages:
- but more complex code
        rem. metrics for programs include time for development, maintenance, ...
- actually occupies more space than array of data

general scheme:
    class LinkedList
    +-------------------------------------------------------------------+
    |                                                                   |
    |   first           class ~Node     class ~Node     class ~Node     |
    |   +-------+       +-------+       +-------+       +-------+       |
    |   |    *--+------>|   33  |   +-->|   22  |   +-->|   55  |       |
    |   +-------+       +-------+   |   +-------+   |   +-------+       |
    |                   |    *--+---+   |    *--+---+   |    /  |       |
    |                   +-------+       +-------+       +-------+       |
    |                                                                   |
    +-------------------------------------------------------------------+
linkList

the following didn't fit in the photocopies handed out:
   public void displayList()
      {
      System.out.println("List (first-->last): ");
      System.out.println("  first=" + (first == null ? "null" : first.toString0()));
      Link current = first;       // start at beginning of list
      while(current != null)      // until end of list,
         {
         System.out.print("  ");
         current.displayLink();   // print data
         System.out.println();
         current = current.next;  // move to next link
         }
      System.out.println("");
      }
since we'll need functionality like this to do things such
as deleting in the middle, let's also write the following:
   public Link find(int key)
      {
      
      }
to ≈emulate the Java API, let's also write the following:
   public boolean contains(int key)
      {
      
      }
to ≈emulate the Java API, let's also write the following:
   public void addLast(int id, double dd)
      {                           
      
      }
more functionality to provide should be as follows:
   public Link delete(int key)    // delete link with given key
      {                           // 
      
      }
linkList2


(Copyright © 2009 by Hugh McGuire   — for thoughts about this, see   http://www.cis.gvsu.edu/~mcguire/teaching/copyright_thoughts.html .)