2016-05-09 23 views
0

我必須反轉鏈接列表。只要看看方法reverse,也許你可以幫助我,因爲我絕望;)。也許這是可行的以下方法...Java:如何通過可重用來反轉

謝謝大家。

繼承人的代碼:

import java.util.Iterator; 

public class LL<E> implements Iterable<E> { 
    E head; 
    LL<E> tail; 
    String a="["; 

    public LL(E head, LL<E> tail) { 
     super(); 
     this.head = head; 
     this.tail = tail; 
    } 

    public LL() { 
     this(null, null); 
    } 

    boolean isEmpty() { 
     return head == null && tail == null; 
    } 

    @Override 
    public Iterator<E> iterator() { 
     return new MyIterator(this); 
    } 

    private class MyIterator implements Iterator<E> { 
     LL<E> current; 

     public MyIterator(LL<E> current) { 
      super(); 
      this.current = current; 
     } 

     @Override 
     public boolean hasNext() { 
      return !current.isEmpty(); 
     } 

     @Override 
     public E next() { 
      E result = current.head; 
      current = current.tail; 
      return result; 
     } 
     E get(int i) throws IndexOutOfBoundsException{//maybe... 
      E result = current.head; 
      for(int q=0; q<i;q++){ 
       current=current.tail; 
      } 
      return result; 
     } 

     boolean contains(E e){//das gleiche nur mit tail 
      boolean yo=false; 
      while(hasNext()){ 
       E result= current.head;// oder tail 
       if(result.equals(e))return yo=true;//unnötig ;) 
      } 
      return yo; 
     } 

     E last(){//oder nochmal 
      E result=get(1); 
      if(hasNext()) {result= next();}; 
      return result; 
     } 




    } 

    public LL<E> limit(int i){ 
     if(i>0&& !isEmpty()){ 
      return new LL<E>(head,tail.limit(i-1)); 
     } 
     return new LL<E>(); 
    } 

    public LL<E> drop(int i){ 
     //löschen wie bei limit 
     if(i>0){ 
      return tail.drop(i-1); 
     } 
     return new LL<E>(head,tail); 
    } 

    public LL<E> sublist(int from, int to){ 
     //was rauslöschen und limit 
     LL<E> ersteElemente= this.limit(to); 
     //erste delete 
     return ersteElemente.drop(from); 
    } 

    public LL<E> reverse(){ 
     //2 schleifen 
     for(int q=6;q>0;q--){ 
      return this.sublist(q-1, q); 
     } 

    } 


    @Override 
    public String toString(){ 
     String result="["; 
     LL<E> n;//for Schleife 

     //while (tail!=null){ 
      this.forEach((x)-> a+=x+";"); 
      a+="]"; 
     //} 
     return a; 

    } 















    public static void main(String[] args) { 
     LL<String> xs = new LL<>("Freunde", new LL<>("Römer", new LL<>("Landsleute", 
       new LL<>("leiht", new LL<>("mir", new LL<>("euer", new LL<>("Ohr", new LL<>()))))))); 
     xs.forEach((x) -> System.out.println(x.toUpperCase())); 
     for (String x:xs){ 
      System.out.println(x.toUpperCase()); 
     } 
     System.out.println((xs.toString())); 
     xs.toString(); 
     System.out.println("END"); 

    } 
} 
+0

在使用你的迭代雙向鏈表返回一個值。 [1] https://en.wikipedia.org/wiki/Doubly_linked_list –

+0

只有['ListIterator'](https://docs.oracle.com/javase/8/docs/api/java/util/ListIterator。 html)直接支持反向迭代。 – zapl

回答

0

您需要在反向方法

public LL<E> reverse() { 
    LL<E> result = null; 
    // 2 schleifen 
    for (int q = 6; q > 0; q--) { 
     result = this.sublist(q - 1, q); 
    } 
    return result; 
} 
+0

最幸福的是,但我怎麼知道長度(你定義爲6),所以我得到的一切,但沒有null對象 – Felix

+0

作爲輸出我只是:[Freunde;]因爲我重寫並不添加元素在反轉訂購 – Felix