Processing math: 100%
Close
Close Window

Show Source |    | About   «  30.9. Clean Code   ::   Contents   ::   30.11. SkipLists  »

30.10. Lists

30.10.1. Lists

30.10.1.1. Lists

A list is a finite, ordered sequence of data items.

Important concept: List elements have a position.

Notation: <a0,a1,,an1>

What operations should we implement?

30.10.1.2. List Implementation Concepts

Our list implementation will support the concept of a current position.

Operations will act relative to the current position.

<20,23 | 12,15>

30.10.1.3. List ADT (1)

30.10.1.4. List ADT (2)

30.10.1.5. List ADT (3)

30.10.1.6. List ADT Examples

List: <12 | 32,15>

L.insert(99);

Result: <12 | 99,32,15>

Iterate through the whole list:

for (L.moveToStart(); !L.isAtEnd(); L.next()) {
  it = L.getValue();
  doSomething(it);
}

30.10.1.7. List Find Function

// Return true if k is in list L, false otherwise
static boolean find(List L, Object k) {
  for (L.moveToStart(); !L.isAtEnd(); L.next())
    if (k == L.getValue()) return true; // Found k
  return false;                         // k not found
}

30.10.1.8. Array-Based List Class (1)

class AList implements List {
  private Object listArray[];             // Array holding list elements
  private static final int DEFAULT_SIZE = 10; // Default size
  private int maxSize;                    // Maximum size of list
  private int listSize;                   // Current # of list items
  private int curr;                       // Position of current element

30.10.1.9. Array-Based List Insert

1 / 6 Settings
<<<>>>

Inserting an element at the head of an array-based list requires shifting all existing elements in the array by one position toward the tail.

Created with Raphaël 2.1.2
    Proficient Saving... Error Saving
    Server Error
    Resubmit

    30.10.1.11. Linked List Position (1)

    1 / 3 Settings
    <<<>>>

    Here is a graphical depiction for a linked list storing five integers. The value stored in a pointer variable is indicated by an arrow "pointing" to something. A NULL pointer is indicated graphically by a diagonal slash through a pointer variable's box. The vertical line between the nodes labeled 23 and 10 indicates the current position (immediately to the right of this line).

    Created with Raphaël 2.1.2
    20
    23
    10
    Created with Raphaël 2.1.2
    12
    15
    Proficient Saving... Error Saving
    Server Error
    Resubmit

    30.10.1.12. Linked List Position (2)

    1 / 7 Settings
    <<<>>>

    Another problem is that we have no link to get us to the preceding node (shown in yellow). So we have no easy way to update the yellow node's next pointer.

    Created with Raphaël 2.1.2
    20
    23
    10
    Created with Raphaël 2.1.2
    12
    15
    head
    curr
    tail
    Proficient Saving... Error Saving
    Server Error
    Resubmit

    30.10.1.13. Linked List Position (3)

    Created with Raphaël 2.1.2
    Created with Raphaël 2.1.2
    null
    null
    head
    curr
    tail

    Created with Raphaël 2.1.2
    null
    20
    23
    10
    12
    Created with Raphaël 2.1.2
    15
    null
    head
    curr
    tail

    30.10.1.14. Linked List Class (1)

    1 / 7 Settings
    <<<>>>

    Let's look at the data members for class LList.

      Proficient Saving... Error Saving
      Server Error
      Resubmit

      30.10.1.15. Linked List Class (2)

      1 / 5 Settings
      <<<>>>

      Now we look at the constructors for class LList.

        Proficient Saving... Error Saving
        Server Error
        Resubmit

        30.10.1.16. Insertion

        1 / 9 Settings
        <<<>>>

        The linked list before insertion. 15 is the value to be inserted.

        Created with Raphaël 2.1.2
          null
          35
          23
          Created with Raphaël 2.1.2
          12
          null
          head
          curr
          tail
          it
          1. 15
          Proficient Saving... Error Saving
          Server Error
          Resubmit

          30.10.1.17. Removal

          1 / 7 Settings
          <<<>>>

          Now we look at the remove method.

          Created with Raphaël 2.1.2
            null
            23
            8
            35
            Created with Raphaël 2.1.2
            10
            null
            head
            curr
            tail
            Proficient Saving... Error Saving
            Server Error
            Resubmit

            30.10.1.19. Overhead

            • Container classes store elements. Those take space.

            • Container classes also store additional space to organize the elements.

              • This is called overhead
            • The overhead fraction is: overhead/total space

            30.10.1.20. Comparison of Implementations

            • Array-Based Lists:
              • Insertion and deletion are Θ(n).
              • Prev and direct access are Θ(1).
              • Array must be allocated in advance.
              • No overhead if all array positions are full.
            • Linked Lists:
              • Insertion and deletion are Θ(1).
              • Prev and direct access are Θ(n).
              • Space grows with number of elements.
              • Every element requires overhead.

            30.10.1.21. Space Comparison

            "Break-even" point:

            DE=n(P+E)

            n=DEP+E

            E: Space for data value.

            P: Space for pointer.

            D: Number of elements in array.

            30.10.1.22. Space Example

            • Array-based list: Overhead is one pointer (8 bytes) per position in array – whether used or not.
            • Linked list: Overhead is two pointers per link node one to the element, one to the next link
            • Data is the same for both.
            • When is the space the same?
              • When the array is half full

            30.10.1.23. Freelist

            System new and garbage collection are slow.

            • Add freelist support to the Link class.
            1 / 7 Settings
            <<<>>>

            We will illustrate using a freelist by performing a series of list operations. Let's start from an empty singly linked list and a freelist variable pointing to null.

            Created with Raphaël 2.1.2
            Created with Raphaël 2.1.2
            null
            null
            Created with Raphaël 2.1.2
            null
            head
            curr
            tail
            freelist
            Proficient Saving... Error Saving
            Server Error
            Resubmit

            30.10.1.24. Doubly Linked Lists

            Created with Raphaël 2.1.2
            null
            20
            23
            12
            Created with Raphaël 2.1.2
            15
            null
            head
            curr
            tail

            30.10.1.25. Doubly Linked Node (1)

            30.10.1.26. Doubly Linked Insert

            1 / 10 Settings
            <<<>>>

            The linked list before insertion. 15 is the value to be inserted.

            Created with Raphaël 2.1.2
              it
              1. 15
              null
              23
              8
              35
              Created with Raphaël 2.1.2
              10
              null
              head
              curr
              tail
              Proficient Saving... Error Saving
              Server Error
              Resubmit

              30.10.1.27. Doubly Linked Remove

              1 / 9 Settings
              <<<>>>

              Now we will look at the remove method. Here is the linked list before we remove the node with value 8.

              Created with Raphaël 2.1.2
                null
                23
                8
                Created with Raphaël 2.1.2
                35
                null
                head
                curr
                tail
                Proficient Saving... Error Saving
                Server Error
                Resubmit

                   «  30.9. Clean Code   ::   Contents   ::   30.11. SkipLists  »

                nsf
                Close Window