List Implementations That Link Data by mvh19129

VIEWS: 8 PAGES: 44

									List Implementations
       That Link Data
              Chapter 6
Chapter Contents
   Linked Data                           Method contains
       Forming a Chains                  Remaining methods
   The Class Node                        Using a Class Node with
                                           Set and Get Methods
   A Linked
                                      Tail References
    Implementation
                                          Revision of List
       Adding to End of List
       Adding at Given Position      Pros and Cons of Using
       Method remove                  Chain
       Method replace                Java Class Library:
       Method getEntry                LinkedList
                                                                     2
Linked Data
   Consider the analogy of desks in a classroom
       Placed in classroom as needed
       Each desk has a unique id, the “address”
       The desks are linked by keeping the address of
        another chair
       We have a chain of chairs




                                                         3
Linked Data




          A chain of 5 desks.
                                4
Forming a Chain
   First desk placed in room
       Blank desk top, no links
       Address of the first chair given to teacher




                   One desk in the room.              5
Forming a Chain
   Second student arrives, takes a desk
       Address of first desk placed on new desk
       Instructor “remembers” address of new desk




                                                     6
                     Two linked desks
Forming a Chain
   Third desk arrives
       New desk gets address of second desk
       Instructor remembers address of new desk




             Three linked desks, newest desk first.   7
Forming Another Chain
   This time the first student is always at the
    beginning of the chain
       Instructor only remembers first address
   The address of a new desk is placed on the
    previous desk (at end of chain)
       End of chain found by following links
       Newest desk does not have a pointer address to
        any other desk

                                                         8
Forming Another Chain




       Two linked desks, newest desk last.
                                             9
Forming Another Chain




       Three linked desks, newest desk last.
                                               10
Forming Another Chain




       Five linked desks, newest desk last.
                                              11
Adding New Desk

   Consider the requirement to organize the
    chain alphabetically
       New arrivals are placed somewhere in the chain,
        not necessarily at the end
   Possibilities for placement of a new desk
       Case 1: Before all current desks
       Case 2: Between two existing desks
       Case 3: After all current desks

                                                          12
Adding New Desk: Case 1




    Chain of desks prior to adding a new desk to
               beginning of the chain
                                                   13
Adding New Desk: Case 1




     Addition of a new desk to beginning of
                a chain of desks              14
Adding New Desk: Case 2




     Two consecutive desks within a chain prior
           to adding new desk between
                                                  15
Adding New Desk: Case 2




    Addition of a new desk between two other
                      desks.
                                               16
Removing Desk

   Removing an item from a chain
   Possible cases
       Case 1: Desk to be removed is first in the chain
       Case 2: Desk to be removed is between two
        current desks
       Case 3: Desk to be removed is last in the chain



                                                           17
Removing Desk: Case 1




    A chain of desks just prior to removing first desk.
                                                          18
Removing Desk: Case 1




    A chain of desks just after removing first desk.
                                                       19
Removing Desk: Case 2




    A chain of desks just prior to removing a desk
              between two other desks.
                                                     20
Removing Desk: Case 2




     A chain of desks just after removing a desk
              between two other desks.
                                                   21
Removing Desk: Case 3




    Before and after removing last desk from a chain.
                                                        22
The Class Node
   Nodes are objects that are linked together to
    form a data structure
   We will use nodes with two data fields
       A reference to an entry in the list
           (the person sitting at the desk)
       A reference to another node
           (the address on the paper on the desk)




                                                     23
The Class Node




 Two linked nodes with
 (a) primitive data; (b) class/object data
 private class Node
 {
         private T data; //data value in that node entry
          private Node next; //link to next node
 …
                                                           24
 }
Inner Class Node
private class Node
{
    private T data; //data value in that node entry
    private Node next; //link to next node

    private Node (T dataPortion)
    {
         data = dataPortion;
         next = null;
    } //end constructor

    private Node( T dataPortion, Node nextNode)
    {
          data = dataPortion;
          next = nextNode;
    } // end constructor

}                                                     25
    A Linked Implementation of the
    ADT List

   Use a chain of nodes
       Remember the address of the first node in the
        chain
   Record a reference to the first node
       The “head reference”
   The implementation contains the class Node
    as an inner class
                                                        26
A Linked Implementation of the
ADT List
public class LList<T> implements ListInterface<T>
{
    private Node firstNode; //head reference to fist node
    private Node lastNode; // tail reference to last node
    private int length; //number of entries in list

    public LList()
    {
          clear();
    } //end constructor

    public final void clear()
    {
         firstNode = null;
         lastNode = null;
         length = 0;
    } // end clear

    // implementation of the public methods add, remove, replace, getEntry, contains,
    getLength, isEmpty, isFull, and display…
}

                                                                                        27
Adding to the End of An empty
List




              (a) An empty list and a new node;
    (b) after adding a new node to a list that was empty
                                                           28
Adding to the End of An Non-empty
List




 A chain of nodes (a) just prior to adding a node at the end;
                                                          29

           (b) just after adding a node at the end.
Adding to the End of the List
public boolean add(T newEntry)
{
  Node newNode = new Node(newEntry);

    if (isEmpty())
         firstNode = newNode;
    else
         lastNode.next = newNode;

    lastNode = newNode;

    length++;
    return true;
}                                      30
Adding to the Beginning of the List




  A chain of nodes (a) prior to adding a node at the beginning;
            (b) after adding a node at the beginning.
                                                          31
Adding at a Given Position Within the
List




  A chain of nodes (a) prior to adding node between
   adjacent nodes; (b) after adding node between      32

                   adjacent nodes
Adding at a Given Position Within
the List
    The following Java Statements implement
     these steps:
        Node newNode = new Node(newEntry);
        Node nodeBefore = getNodeAt(newPosition -1);
        Node nodeAfter = nodeBefore.next;
        newNode.next = nodeAfter;
        nodeBefore.next = newNode;



                                                        33
Removing the first node




  A chain of nodes (a) prior to removing first node;
          (b) after removing the first node
 Need to see if there is only one node, if yes, update
 the lastnode.                                           34
Removing other than the
beginning of the list




 A chain of nodes (a) prior to removing interior node; (b)
              after removing interior node              35

 Need to see if nodeToRemove is at the end, if yes, update the lastnode.
    The Method remove: first node
   firstNode = firstNode.next;

    The Method remove: other than the
    beginning
    Node nodeBefore = getNodeAt(givenPosition -1);
    Node nodeToRemove = nodeBefore.next;
    Node nodeAfter = nodeToRemove.next;
    nodeBefore.next = nodeAfter;
    nodeToRemove = null;
                                                      36
Using Class Node that Has Set
and Get Methods
   Class Node is an inner class
     The class LList can access private data fields
        directly
       Stylistically better to use the Set and Get
        methods of the class Node
   Thus better to use statements such as:
    currentNode.getData(); or
    desiredNode.setData(newEntry);
                                                       37
 Using Class Node that Has Set
 and Get Methods

private T getData()
{
   return data;
}
private void setData(T newData)
{
   data = newData;
}

private Node getNextNode()
{
   return next;
}
                                  38
Tail References
   Consider a set of data where we repeatedly
    add data to the end of the list
   Each time the getNodeAt method must
    traverse the whole list
       This is inefficient
   Solution: maintain a pointer that always
    keeps track of the end of the chain
       The tail reference

                                                 39
Tail References




     A linked chain with a head and tail reference.

                                                      40
Tail References
   When adding to an empty list
       Both head and tail references must point to the
        new solitary node
   When adding to a non empty list
       No more need to traverse to the end of the list
       lastNode points to it
       Adjust lastNode.next to new node and
        lastNode to new node

                                                          41
Tail References




    Adding a node to the end of a nonempty chain
              that has a tail reference            42
Pros and Cons of a Chain for an ADT List
 The chain (list) can grow as large as
  necessary
 Can add and remove nodes without shifting
  existing entries
But …
 Must traverse a chain to determine where to
  make addition/deletion
 Retrieving an entry requires traversal
       As opposed to direct access in an array
                                                  43
Java Class Library: The Class LinkedList

     The standard java package java.util
      contains the class LinkedList
     This class implements the interface List
     Contains additional methods
           addFirst()
           addLast()
           removeFirst()
           removeLast()
           getFirst()
           getLast()                            44

								
To top