List Objects

  1. Each program (e.g., linked lists) class has:

  2. The linked lists in the examples so far are "fully exposed":

    I have done so for academic reasons, i.e., this is the most direct way to show you how to connect linked list elements

    A "fully" exposed linked list class is easy to program, but it is vunerable to abuse by programmers that only uses the linked list (they may not have the programming experience to maintain the linked list structure....)

  3. Linked lists are complex information storage structures and users must use great care in manipulating list structures.

    It is therefore a good idea not to disallow access of the list structure by users of a linked list class

    Yet, we must provide enough functionalities for the users of the linked list to enable them to satify their need.



  4. Every information storage structure (including a linked list) has a number of operations, such as:

  5. The key to

  6. How to prevent users of a linked list from messing with the list itself:

    List class that maintains a list of ListElem objects
      public class List
      {
         private ListElem head;  // Users CANNOT access the list
    
         public List()
         {
            head = null;   // Initializes an empty list
         }
    
         // *************************************************
         // Define PUBLIC methods that provides needed 
         // operations to the user
         // *************************************************
         public void insert(int v)
         {
    	.....
         }
    
         public void delete()
         {
    	.....
         }
    
         public void print()
         {
    	.....
         }
      }
    
    
    

  7. Example:

    Protected list class that provides insert and "toString" methods
      public class List
      {
         private ListElem head;  // Denies user access to the list
    
         public List()
         {
            head = null;   // empty list
         }
    
         // *************************************************
         // insert(v): make an object with value "v"
         // 	   and insert into list at this.head
         // *************************************************
         public void insert(int v)
         {
            ListElem elem;
    
            elem = new ListElem();       // Make ListElem object   
            elem.value = v;              // Fill info. part
    
            elem.next = this.head;       // Link at beginning
            this.head = elem;
         }
    
         // Method returns a String with all number in the list
         public String toString()
         {
            String out = "";
            ListElem p;
    
            p = head;
            while (p != null)
            {
               out = out + "[" + p.value + "] ";
               p = p.next;    // move down the chain !!!
            }
    
            return(out);
         }
    
      }
    
    
    

  8. DEMO programs