質問

二重リンクリストの削除方法とは

役に立ちましたか?

解決

Bill the Lizard が言ったのと同じアルゴリズムですが、グラフィカルな方法で:-)

リンクリストから削除
(ソース: jaffasoft.co.uk

他のヒント

一般的なアルゴリズムは次のとおりです。

  • 削除するノードを見つけます。
  • node.previous.next = node.next
  • node.next.previous = node.previous
  • node.previous = null
  • node.next = null
  • GC以外の環境にいる場合はノードを破棄します

前と次のノードのnullをチェックして、頭または尾を削除するかどうかを確認する必要がありますが、これらは簡単なケースです。

public void remove ()
{
    if (getPreviousNode () != null)
        getPreviousNode ().setNextNode (getNextNode ());
    if (getNextNode () != null)
        getNextNode ().setPreviousNode (getPreviousNode ());    
}

二重リンクリストの実装Removeメソッド(2番目のプログラミング割り当てから):

public void remove(int index) {
    if(index<0 || index>size())
    throw new IndexOutOfBoundsException("Index out of bounds. Can't remove a node. No node exists at the specified index");
    if(size()==0) {
        throw new NullPointerException("Empty list");
    }
    if(!isEmpty()) {
        Node current;
        //starting next one to our head
        current = head.next;
        for(int i=0;i<index;i++) {
            current = current.next;
        }
        current.previous.next = current.next;
        current.next.previous = current.previous;
        numOfNodes--;
        sizeChangeCount++;
    }
}

public boolean remove(T o) {
    Node current = head;
    for(int i=0;i<size();i++) {
        current=current.next;
        if(current.data.equals(o)) {
            current.previous.next = current.next;
            current.next.previous = current.previous;
            numOfNodes--;
            sizeChangeCount++;
            return true;
        }           
    }
    return false;
}

APIのメソッドの名前を尋ねていますか?実際には二重リンクリストであるjava.util.LinkedListについて尋ねていると仮定すると、その答えは単に削除されます。

...または、そのタイプのデータ構造から要素を削除するためのアルゴリズムの名前は何と呼ばれていますか?まあ..その答えはまた、要素を削除することです。さて、実際のアルゴリズムでそれを行うには...前のノードの次のポインターと次のノードの最後のポインターを変更するだけです。ただし、複数のスレッドのデータ構造を使用している場合は、removeメソッドを同期するか、データ構造の使用パターンに合った順序で削除手順を実行する必要があります。

現在のポインターポインターはどうですか? crntを次のノードに移動する必要があります。 http://pastebin.ca/1249635

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top