ListLinked.java

Iz E-študij, proste zakladnice študentskega znanja

Skoči na: navigacija, iskanje
//Implementacija seznama s kazalci
 
public class ListLinked extends List {
  protected ListLinkedNode first, last ;
 
  public ListLinked() {
      makenull() ;
  }
  public ListLinked(List init) {
    copy(init) ;
  }
 
  public void makenull() {
     first = new ListLinkedNode(null,null) ;
     last = null ;
  }
 
  public Object first() {
    return first ;
  }
  public void insert(Object x) {
     if (last==null)
        insert(x, first) ;
     else
       insert(x,last.next) ;
  }
  public void insert(Object x, Object pos) {
     ListLinkedNode newElement = new ListLinkedNode(x, ((ListLinkedNode) pos).next) ;
     ((ListLinkedNode)pos).next = newElement ;
     if (last==null) // we inserted 1st element
       last=first ;
     else if (last.next==pos) // inserting at end
       last = (ListLinkedNode)pos ;
  }
  public Object last() {
     return last ;
  }
  public Object retrieve(Object pos) {
     return ((ListLinkedNode) pos).next.element ;
  }
 
  public Object retrieve(int pos) {
    for (Object iter = first() ; ! overEnd(iter); iter = next(iter), pos--)
      if (pos==0)
        return retrieve(iter) ;
    return null ;
  }
 
  public boolean overEnd(Object pos) {
     if ( ((ListLinkedNode) pos).next == null)
        return true ;
     else return false ;
  }
  public Object end() {
     if (first.next == null)
       return first ;
     else return last.next ;
  }
  public Object previous(Object pos) {
     ListLinkedNode temp = first ;
     while (temp.next != pos && temp.next != null)
       temp = temp.next ;
     if (temp.next == null)
       return null ;
     else
       return temp ;
  }
  public Object next(Object pos) {
     return ((ListLinkedNode) pos).next ;
  }
  public void delete(Object pos) {
    ListLinkedNode temp = ((ListLinkedNode) pos).next ;
    ((ListLinkedNode) pos).next = ((ListLinkedNode) pos).next.next ;
    if (temp == last)
       last = (ListLinkedNode) pos ;
    else if (pos==last) { // find new last
      if (pos == first) // empty list
          last = null ;
      else {
        last = first ;
        while (last.next.next != null)
          last = last.next ;
      }
    }
  }
  public boolean empty() {
    if (last==null)
      return true ;
    else return false ;
  }
  public void concatenate(ListLinked s1, ListLinked s2) {
    if (s1.empty()) {
      first = s2.first ;
      last = s2.last ;
      s2.makenull() ;
    }
    else if (s2.empty()) {
      first = s1.first ;
      last = s1.last ;
      s1.makenull() ;
    } else {
      first = s1.first ;
      last = s2.last ;
      s1.last.next.next = s2.first.next ;
      s1.makenull() ;
      s2.makenull() ;
    }
  }
 
  public static void main(String[] args) {
    List list = new ListLinked();
    System.out.println("Testing "+list.getClass() );
    int i ;
    for (i=10 ; i > 2 ; i--)
      list.insert(new Integer(i));
    System.out.print("List: ");
    list.printList();
 
    Object locator ;
    for (i=0 ; i < 5 ; i++) {
      locator = list.locate(new Integer(i)) ;
      if (locator !=null)
         System.out.println("Found "+list.retrieve(locator));
      else System.out.println("Not found "+i);
    }
    for (i=10 ; i > 2 ; i=i-2) {
        locator = list.locate(new Integer(i)) ;
        if (locator != null) {
          System.out.println("Deleting element " + i);
          list.delete(locator) ;
        }
    }
    System.out.print("List:");
    list.printList();
 
    locator = list.first() ;
    locator = list.next(locator);
    System.out.println("On place " + 2 + " is element " + list.retrieve(locator));
 
    ListLinked c1 = new ListLinked(), c2= new ListLinked(),c3= new ListLinked()  ;
    for (i=1 ; i <= 5 ; i++) {
      c1.insert(new Integer(i));
      c2.insert(new Integer(5+i));
    }
    System.out.print("List 1: ");
    c1.printList();
    System.out.print("List 2: ");
    c2.printList();
    c3.concatenate(c1,c2);
    System.out.print("Concatenated 1 and 2: ");
    c3.printList();
  }
}
Osebna orodja
Imenski prostori
Različice
Dejanja
navigacija

Tiskanje/izvoz
orodja