2012-04-25 10 views
0

javaのLinkedListから項目を削除しようとしています。このリストは私によって実装されており、私はJava APIを使用していません。私が直面している主な問題は、再帰コーディングで常に失われているので、リカバリであることです。APIを使用せずにjavaのLinkedListから特定の項目を削除する

class List{ 

    int N; 
    List next; 
    List current; 
    List(int N){ 
     this.N =N; 
     this.next = null; 
    } 

    @Override 
    public String toString() { 
     String o = ""; 
     List curr = this; 
     while(curr != null){ 
      o += curr.N+"-->"; 
      curr = curr.next; 
     } 

     return o+"TAIL"; 
    } 
} 

実装方法:

private static List Remove(List L,int N){ 
    if(L == null || L.next == null) 
     return L; 

    List current = L; 
    List previous = null; 

    while(current != null){ 
     if(current.N == N){ 
      current = current.next; 
      if(previous == null)previous = current; 
      else{ 
       previous.next = current; 
      } 
      break; 
     }else{ 
      previous = current; 
      current = current.next;    
     } 
    } 
    return previous; 
} 

が入力 - 私は取得しています

List list1 = new List(1); 
     list1.next = new List(2); 
     list1.next.next = new List(3); 
     list1.next.next.next = new List(4); 
     list1.next.next.next.next = new List(5); 
     list1.next.next.next.next.next = new List(6); 
     list1.next.next.next.next.next.next = new List(7); 
System.out.println("Before Removal "+list1.toString()); 
     System.out.println("After Removal "+Remove(list1,3)); 

出力がある - 除去前

  • 1 - > 2 - > 3 - →4→5→6→7→TAIL
  • 取り外し2後 - > 4 - > 5 - > 6 - > 7 - 私はcurrent = current.nextまたは参照次に設定されている設定していたように> TAILここ

Iが値1を失ってい値。だから間違いなく私は、異なる参照に格納されたデータの表示にいくつか問題があります。

答えて

2

間違いはここにある:

return previous; 

それが除去されていない場合は、リストの元の頭を返す必要があります。グラフィカルに表示するには:この時点で

N == 3 
List Before Removal: 1-->2-->3-->4-->5-->6-->7-->TAIL 
At start of iteration 1: 
L     ^
previous  (null) 
current   ^
No match -> iteration 2: 
L     ^
previous   ^
current    ^
No match -> iteration 3: 
L     ^
previous    ^
current     ^
Match -> remove current: 
List After Removal: 1-->2-->4-->5-->6-->7-->TAIL 
L     ^
previous    ^
current     ^

previousを返すことによって、あなたは元ヘッド要素Lを失います。

head要素を削除する場合は、ループの前に別のチェックを追加する必要があります。

BtwあなたのRemoveメソッドは再帰的ではありません - それ自体を呼び出すことはありません。

+1

@bunta私の更新を見てください、それが助けてくれることを願っています: –

+0

説明のおかげで –

1

あなたが頭を返すされていないので、それは単にです - あなたは単に「削除」ノードにではなく、以前のポインタを: - 明確にする - これは再帰的な解決策ではありません。また

static List Remove(final List L, final int N) { 
    // Base case for null head pointer 
    final List head = L; 
    if (head == null) 
     return head; 

    // Base case for removing the head 
    if (head.N == N) 
     return head.next; 

    List current = head.next; 
    List previous = head; 

    while (current != null) { 
     if (current.N == N) { 
      current = current.next; 
      if (previous == null) { 
       previous = current; 
      } 
      else { 
       previous.next = current; 
      } 

      break; 
     } else { 
      previous = current; 
      current = current.next; 
     } 
    } 

    return head; 
} 

+0

ヘッド要素を削除する場合、これは正しく動作しません。 –

+0

ですので、(L.N == N)L = L.nextの場合は先頭行に追加しました –

+0

@PéterTörökfixed – BeRecursive

関連する問題