ECS110 Lecture Notes for Wednesday, October 25, 1995

Professor Rogaway conducting

Lecture 12

Scribe of the day: Dattesh Patel



Last Time: Survey, Review, Queues

Today:  Lists as an ADT
	List Exotica
	Lists in Lisp
	Connected Components

Reminder:  Midterm Tomorrow
           1322 Storer
	   7:30-9:00 pm

LISTS AS AN ADT (SPECIFICATIONS)

      Objects (Data) -- for n>=0, elements a(1), ...... a(n) of Type T
                      connected together. 
        
      Operations -- 

         Time    Function
         O(n)    Insert(T a, int m)
                 // Modify data to a(1)....a(m), a, a(m+1).....a(n),
                 // if element m exists or else signal error.
        
         O(n)    Delete(int m)
                 // Modify data to a(1)....a(m-1), a(m+1)......a(n),
	         // if element m exists or else signal error.

         O(n)    int Find(T a) 
                 // Find element a in list, and return it's position.
   	         // Return -1, if not found.
         
   O(n)/ O(1)   T FindKth(int k)
	         // return element a(k) if it exists, else signal error
        
         O(1)    T Next(int m)
	         // return element a(m+1) if it exists, else signal error

         O(1)    T Previous(int m)
	         // return element a(m-1) if it exists, else signal error

         O(1)    int Empty()
		 // return n==0

	 O(n)    Readlist()
		 // read in new list

	 O(n)    PrintList()
	         // print entire list

      Note:  These times are for an array implementation of the list.
             If the list were implemented using links (as in a linked
             list) --- Insert, Delete, Next, and Previous would be O(1)
             to O(n) depending on the locality of the inserts and deletes.
             FindKth would be O(n).

 
REPRESENTATIONS: List Exotica Each of the following list have some sort of pointer pointing to the front of it. It some situations a pointer to the end of the list might also be used. 1. Singly linked a(1) ---> a(2) ---> a(3) NULL, where an element is of form DATA | next 2. Doubly linked NULL a(1) <---> a(2) <---> a(3) NULL, where an element is of form prev | DATA | next 3. Circularly Single linked a(1) ---> a(2) ---> a(3) ---> back to a(1), where an element is of the form DATA | next useful: When you've taken over youw own storage allocation you might have a list of garbage elements which you want to add to this list. You could maintain tail pointers to both your free list and your real list, but then you would have to constantly update those pointers. This circularly linked list allows you to make an easier connection: FROM: real list a(1) --> a(2) --> a(3) --> back to a(1) free list f(1) --> f(2) --> f(3) --> back to f(1) TO: f(1) --> a(2) --> a(3) --> a(1) --> f(2) --> f(3) --> back to f(1) 4. Singly Linked List with Header Header ---> a(1) ---> a(2) ---> a(3) NULL Reason: Because of Header you won't have to side effect the caller. You don't need to treat the first element of the list any different than any other element. 5. Circularly Single Linked List with Header Header | | a(1) ---> a(2) ---> a(3) ---> back to a(1) 6. Doubly Linked List with Header NULL Header <---> a(1) <---> a(2) <---> a(3) NULL where the first NULL is the previous pointer for that element and the second NULL is the next pointer for that element. 7. Ciruclarly Double Linked List a(1) prev= a(3) ---> a(2) <---> a(3) next=a(1) 8. Circularly Doulble Linked List with Header Header | | a(1) prev= a(3) ---> a(2) <---> a(3) next=a(1)
LISTS IN LISP l(1) = (PLUS 3 4) l(2) = (PLUS 3 (TIMES 2 6)) Lisp Style Lists --- c(1), c(2), ...., c(n) is a list if each c(i) is an Atom or another List. l(1) = (PLUS 3 4) head of l(1) hd(l(1))= PLUS tail of l(1) tl(l(1))= (3, 4) REPRESENTATION: l(1)front --> PLUS ----> 3 -----> 4 l(2)front --> PLUS ----> 3 -----> bit 0 | TIMES ---> 2 ----> 6 ATOMS are of form bit 0/1 | DATA | next 0 bit indicates list, 1 bit indicates atom