Λίστες
Λίστες (Lists) : Συλλογή δεδομένων σε δυναμικά δεσμευμένους κόμβους. Κάθε κόμβος περιέχει συνδέσεις προς άλλους κόμβους. Προσπέλαση -στού κόμβου διατρέχοντας όλους τους προηγούμενους. Πολλές παραλλαγές για διάφορες χρήσεις. : Singly Linked List - Μονά Συνδεδεμένη Λίστα Doubly Linked List - Διπλά Συνδεδεμένη Λίστα Circular Linked List - Κυκλικά Συνδεδεμένη Λίστα 1 / 31
Συνδεδεμένη Λίστα (Linked List) 2 / 31
Εισαγωγή Μπροστά (Insert Front) 3 / 31
Εισαγωγή Πίσω (Insert Back) 4 / 31
Αφαίρεση Μπροστά (Remove Front) 5 / 31
Αφαίρεση Πίσω (Remove Back) 6 / 31
Διάσχιση (Iteration) 7 / 31
ListNode class 1 class ListNode 2 { 3 // package access members; List can access these directly 4 protected Object element; 5 protected ListNode next; 6 7 ListNode( Object element ) { } 8 9 ListNode( Object element, ListNode node ) { } 10 11 Object getelement() { } 12 13 ListNode getnext() { } 14 } 8 / 31
List class 1 public class List 2 { 3 private ListNode first; 4 private ListNode last; 5 private String name; // string like "list" used in printing 6 7 public List() { } 8 9 public List( String listname) { } 10 11 public void insertatfront( Object element ) { } 12 13 public void insertatback( Object element ) { } 14 15 public Object removefromfront() throws EmptyListException { } 16 17 public Object removefromback() throws EmptyListException { } 18 19 public boolean isempty() { } 20 21 public void print() { } 22 } 9 / 31
EmptyListException 1 public class EmptyListException extends RuntimeException { 2 public EmptyListException() 3 { 4 this( "List" ); 5 } 6 7 public EmptyListException( String name ) 8 { 9 super( name + " is empty" ); 10 } 11 } 10 / 31
ListNode constructor 1 ListNode( Object element ) 2 { 3 4 } 5 6 ListNode( Object element, ListNode node ) 7 { 8 9 } 11 / 31
ListNode constructor 1 ListNode( Object element ) 2 { 3 this(element, null); 4 } 5 6 ListNode( Object element, ListNode node ) 7 { 8 data = element; 9 nextnode = node; 10 } 12 / 31
List constructor 1 public List() 2 { 3 4 } 5 6 public List( String listname ) 7 { 8 9 } 10 11 public boolean isempty() 12 { 13 14 } 13 / 31
List constructor 1 public List() 2 { 3 this("list"); 4 } 5 6 public List( String listname ) 7 { 8 name = listname; 9 firstnode = lastnode = null; 10 } 11 12 public boolean isempty() 13 { 14 return firstnode == null; 15 } 14 / 31
print List 1 /** 2 * Prints the list's contents to System.out 3 */ 4 public void print() 5 { 6 if ( isempty() ) 7 { 8 System.out.printf( "Empty %s\n", name ); 9 return; 10 } // end if 11 12 System.out.printf( "The %s is: ", name ); 13 ListNode current = first; 14 15 // while not at end of list, output current node's data 16 while ( current!= null ) 17 { 18 System.out.printf( "%s ", current.element ); 19 current = current.next; 20 } // end while 21 22 System.out.println( "\n" ); 23 } // end method print 15 / 31
insertatfront 1 public void insertatfront( Object insertitem ) { 2 3 4 5 6 7 8 } 16 / 31
insertatfront 1 public void insertatfront( Object insertitem ) { 2 ListNode node = new ListNode( insertitem ); 3 4 if ( isempty() ) { 5 firstnode = lastnode = node; 6 } else { // firstnode refers to new node 7 node.nextnode = firstnode; 8 firstnode = node; 9 10 //firstnode = new ListNode( insertitem, firstnode ); 11 } 12 } 17 / 31
insertatback 1 public void insertatback( Object element ) { 2 3 4 5 6 7 8 } 18 / 31
insertatback 1 public void insertatback( Object insertitem ) { 2 ListNode node = new ListNode( insertitem ); 3 4 if ( isempty() ) { 5 firstnode = lastnode = node; 6 } else { // lastnode's nextnode refers to new node 7 lastnode.nextnode = node; 8 lastnode = node; 9 10 //lastnode = lastnode.nextnode = new ListNode( insertitem ); 11 } 12 } 19 / 31
removefromfront 1 public Object removefromfront() throws EmptyListException { 2 3 4 5 6 7 8 } 20 / 31
removefromfront 1 public Object removefromfront() throws EmptyListException { 2 if ( isempty() ) 3 throw new EmptyListException( name ); 4 5 Object removeditem = firstnode.data; 6 7 if ( firstnode == lastnode ) { 8 firstnode = lastnode = null; 9 } else { 10 firstnode = firstnode.nextnode; 11 } 12 13 return removeditem; 14 } 21 / 31
removefromback 1 public Object removefromback() throws EmptyListException { 2 3 4 5 6 7 8 } 22 / 31
removefromback 1 public Object removefromback() throws EmptyListException { 2 if ( isempty() ) 3 throw new EmptyListException( name ); 4 5 Object removeditem = lastnode.data; 6 7 if ( firstnode == lastnode ) { 8 firstnode = lastnode = null; 9 } else { 10 ListNode current = firstnode; 11 12 while ( current.nextnode!= lastnode ) { 13 current = current.nextnode; 14 } 15 16 lastnode = current; 17 current.nextnode = null; 18 } 19 20 return removeditem; 21 } 23 / 31
Java Generics 1 public interface Iterator<T> { 2 T next(); 3 boolean hasnext(); 4 } 5 6 public interface List <E> { 7 void add(e x); 8 Iterator<E> iterator(); 9 } 10 11 public class ArrayList<E> implements List<E> { 12 void add(e x) { } 13 Iterator<E> iterator() { } 14 } 15 16 17 List<String> stringlist = new ArrayList<String>(); 18 List<Integer> intlist = new ArrayList<Integer>(); 19 List<Float> floatlist = new ArrayList<Float>(); 20 21 List<List<String>> listlist = new ArrayList<List<String>>(); 24 / 31
Generic ListNode 1 class ListNode<T> { 2 T data; 3 ListNode<T> nextnode; 4 5 ListNode(T data) { 6 this(data, null); 7 } 8 9 ListNode(T data, ListNode<T> node) { 10 this.data = data; 11 nextnode = node; 12 } 13 14 T getobject() { 15 return data; 16 } 17 18 ListNode<T> getnext() { 19 return nextnode; 20 } 21 } 25 / 31
Generic List 1 public class List<T> { 2 private ListNode<T> firstnode; 3 private ListNode<T> lastnode; 4 private String name; 5 6 public List() { 7 this("list"); 8 } 9 10 public List(String listname) { 11 name = listname; 12 firstnode = lastnode = null; 13 } 14 15 public void insertatfront(t insertitem) { } 16 public void insertatback(t insertitem) { } 17 public T removefromfront() throws EmptyListException { } 18 public T removefromback() throws EmptyListException { } 19 } 26 / 31
Generic List 1 public void insertatfront(t insertitem) { 2 ListNode<T> node = new ListNode<T>(insertItem); 3 4 if (isempty()) { 5 firstnode = lastnode = node; 6 } else { 7 node.nextnode = firstnode; 8 firstnode = node; 9 } 10 } 27 / 31
Generic removefromfront 1 public T removefromfront() throws EmptyListException { 2 if (isempty()) 3 throw new EmptyListException(name); 4 5 T removeditem = firstnode.data; 6 7 if (firstnode == lastnode) { 8 firstnode = lastnode = null; 9 } else { 10 firstnode = firstnode.nextnode; 11 } 12 13 return removeditem; 14 } 28 / 31
Javadoc is a documentation generator for the Java language for generating API documentation in HTML format from Java source code. It uses a format that is the de facto industry standard for documenting Java classes. It is included in your JDK software installation and is called using the following command: "full_path_to\javadoc.exe" -package -d location_of_doc_directory *.java 29 / 31
Javadoc - Documentation μέσω σχολίων 1 /** 2 * Returns an Image object that can then be painted on the screen. 3 * The url argument must specify an absolute {@link URL}. The name 4 * argument is a specifier that is relative to the url argument. 5 * <p> 6 * This method always returns immediately, whether or not the 7 * image exists. When this applet attempts to draw the image on 8 * the screen, the data will be loaded. The graphics primitives 9 * that draw the image will incrementally paint on the screen. 10 * 11 * @param url an absolute URL giving the base location of the image 12 * @param name the location of the image, relative to the url argument 13 * @return the image at the specified URL 14 * @see Image 15 */ 16 public Image getimage(url url, String name) { 17 try { 18 return getimage(new URL(url, name)); 19 } catch (MalformedURLException e) { 20 return null; 21 } 22 } 30 / 31
Javadoc - παραγόμενη HTML public Image getimage(url url, String name) Returns an Image object that can then be painted on the screen. The url argument must specify an absolute URL. The name argument is a specifier that is relative to the url argument This method always returns immediately, whether or not the image exists. When this applet attempts to draw the image on the screen, the data will be loaded. The graphics primitives that draw the image will incrementally paint on the screen. : url - an absolute URL giving the base location of the image. name - the location of the image, relative to the url argument. : the image at the specified URL. : Image 31 / 31