ADT List

Summary
MENU
  1. ADT List
  2. Sequential Storage Implementation
  3. An Alternative Implementation - Linked List -
  4. Variations of Linked Lists
  5. Generalized Lists

1. ADT List

   Specifications of ADT List

   public class List
   { // ADT whose instances are lists (i.e., sequences of elements)

     public List( )
       {Create an instance of ADT List and initialize it to the empty list.}

     public int length( )
       {Return the number of elements in the list.}

     public ElementType get(int i)
       {Return the i-th element of the list, assuming that 1<=i<=length( ).}

     public int find(ElementType e)
       {Return the position of a given element e in the list if it exists.
        Otherwise, return -1.}

     public void insert(ElementType e, int i)
       {Insert a given element e between the (i-1)-th and the i-th positions
        of the list, assuming 1<=i<=length( )+1.}

     public void delete(int i)
       {Delete the i-th element from the list, assuming 1<=i<=length( ).}

     public List concatenate(List x)
       {Return the concatenation of the list and a given list x.}
   }

   The ADT defined above is sometimes called a linear list in which
   every element is atomic.
   It can be generalized into a list (called a generalized list)
   whose elements may be either atoms or lists.

2. Sequential Storage Implementation

   In order to limit the maximum length of a list,
   the specification of the constructor List is modified as follows.

     public List(int size)
       {Create an instance of ADT List with maximum length size and
        initialize it to the empty list.}

   Storage Structures
   ------------------
     public class List
     {  int no_elements;
        ElementType seq[ ];
        private int MaxLength;

        // methods for the operations on List (discussed below)
     }

   Algorithms
   ----------
     public List(int size)
     {  ElementType seq[ ] = new ElementType[size];
        no_elements=0;
        MaxLength=size;
     }

     public int length( )
     {  return no_elements;
     }

     public ElementType get(int i)
     {  return seq[i];
     }

     public void insert(ElementType e, int i)
     {  int j;
        int n;
        ElementType t;
        n=length( );
        if (n < MaxLength) {
           no_element++;
           if (i==n+1) {
              seq[i]=e;
           }
           else {
              for (j=n; j > i; j--) seq[j]=seq[j-1];
              seq[j]=e;
           }
        else
           // Exception Handling for Overflow;
        }
     }

     etc.

3. An Alternative Implementation - Linked List -

   Singly Linked Lists

   Storage Structures
   ------------------
     public class List
     {  int no_items;  // the number of items, i.e., the length
        private Item head; // the first item

        // methods for the operations on List (discussed below)
     }

     public class Item
     {  private ElementType value;
        private Item next;

        public Item(ElementType x)
        {  value=x;
           next=null;
        }
        public ElementType getValue( ) {return value;}
        public Item getNext( ) {return next;}
        public void setValue(ElementType x) {value=x;}
        public void setNext(Item r) {next=r}
     }

   Algorithms
   ----------
     public List( )
     {  no_items=0;
        head=null;
     }

     public int length( ) {return no_items;}

     public ElementType get(int i)
     {  int j=1;
        Item t=head;
        while (j < i) {t=t.getNext( ); j++;}
        return t.getValue( );
     }

     public void insert(ElementType e, int i)
     {  int j;
        Item p,q,previous;
        p = new Item(e);
        if (head==null || i==1) {
           p.setNext(head);
           head=p;
        }
        else {
           previous=head;
           q=previous.getNext( );
           j=2;
           while (j < i) {
              previous=q;
              q=q.getNext( );
           }
           if (q==null) previous.setNext(p)
           else {
              p.setNext(q);
              previous.setNext(p);
           }
        }
     }

     etc.

   Give algorithms for the following operations.
     public int find(ElementType e)
     public void delete(int i)
     public List concatenate(List x)

   If the storage structures do not include no_items,
   how should the algorithm for length be modified?
   Compare the time complexities of these two alternatives.

4. Variations of Linked Lists

(1) Linked Lists with Dummy Headers
	There are special cases to which we have to pay attention.
	(1) Empty list
	(2) Operation at the beginning of a list
	(3) Operation at the end of a list
	---> They make algorithms complicated.
	Introduce dummy elements (called dummy headers) at the
        beginning (and/or the end) of a linked list

(2) Circular Linked Lists
	Improvement:	Make it possible to reach any element
			from any other element by following pointers

(3) Doubly Linked Lists
	Improvement:	Make it efficient to search elements
			back and forth and update a list

(4) Orthogonal Linked Lists
	Elements are threaded by pointers of different types.
        An application to sparse matrices

5. Generalized Lists

   A generalized list A is a sequence of n (>=0) elements A1,A2,...,An,
   where each element Ai is either an atom or a list.
   The elements that are not atoms are called sublists of A.

   The generalized list is denoted by A=(A1,A2,...,An).
   A is the name of the list (A1,A2,...,An) and n is the length of A.

   If n>=1, A1 is called the head of A and (A2,...,An) is called
   the tail of A.

   The special case for n=0 is denoted by ( ) and called the null
   list (or empty list).

   Examples
   --------
     A = ( a , ( B , B ) , a )
           -   ---------   -
           A1      A2      A3

       Head of A:  a
       Tail of A:  ( ( B , B ) , a )

     B = ( A , b , ( ) , ( a , ( ) , ( a , b ) ) )
           -   -   ---   -----------------------
           B1  B2  B3              B4

       Head of B4:  a
       Tail of B4:  ( ( ) , ( a , b ) )

   Generalized lists can also be implemented by linked lists.
   They are useful for representation of grammars and
   pattern matching which will be discussed in the next lecture.