1

誰かが私を助けてくれるなら、コードを更新しました...私のfindFirstメソッドで、要素を含む最初のノードを探したいと思いました。そのようなノードが見つかった場合は、それ以外の場合はnullを返します...これまでのところ方法私はノードを見つけましたが、最初のノードを見つける方法がわかりません...どうすればそれを行うことができますか?これは私がこれまでに持っているものです..ありがとう...

public SLNode<E> findFirst(E element){
    SLNode<E> cursor = head.getSuccessor();
    while(cursor != null){
        if(cursor.getElement() !=null && cursor.getElement().equals(element))
            return cursor;
        cursor = cursor.getSuccessor();
    }
    return null;
}

以下は私の実際のクラスです

public class SinglyLinkedList<E> {
private final SLNode<E> head;
private final SLNode<E> tail;
int length;

// creates an empty list
public SinglyLinkedList() {
    head = new SLNode<E>();
    tail = new SLNode<E>();
    head.setSuccessor(tail);
    length = 0;
}


// adds new node on beginning of the list
public void add(E element) {
    SLNode<E> node = new SLNode<E>(element, null);
    node.setSuccessor(head.getSuccessor());
    head.setSuccessor(node);
}

//removes a first node on beginning of the list
public boolean remove(E element) {
    SLNode<E> previous = head;
    SLNode<E> current = head.getSuccessor();
    while (current != tail) {
        if (current.getElement().equals(element)) {
            previous.setSuccessor(current.getSuccessor());
            //successor is next
            return true;
        }
        previous = current;
        current = current.getSuccessor();
    }
    return false;
}

public SLNode<E> findFirst(E element){
    /*Shown At the Top as part of the Question*/
}


// adds new node on beginning of the list
public void add(SLNode<E> node) {
    node.setSuccessor(head.getSuccessor());
    head.setSuccessor(node);
}

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    SLNode<E> cursor = head.getSuccessor();
    while (cursor != tail) {
    sb.append(cursor.getElement()).append(" ");
    cursor = cursor.getSuccessor();
    }
    sb.append("\n");
    return sb.toString();
}


}

SLnodeクラス

public class SLNode<E> {
    private E element;
    private SLNode<E> successor;

    public SLNode() {
        element = null;
        successor = null;
    }

    public SLNode(E theElement, SLNode<E> theSuccessor) {
        element = theElement;
        successor = theSuccessor;
    }

    public E getElement() {
        return element;
    }

    public void setElement(E newElement) {
        element = newElement;
    }

    public SLNode<E> getSuccessor() {
        return successor;
    }

    public void setSuccessor(SLNode<E> newSuccessor) {
        successor = newSuccessor;
    }

    public Object getPrevious() {
        // TODO Auto-generated method stub
        return null;
    }

    public void setPrevious(Object object) {
        // TODO Auto-generated method stub      
    }

    public void setNext(Object object) {
        // TODO Auto-generated method stub      
    }

    public Object getNext111() {
        // TODO Auto-generated method stub
        return null;
    }

    public Object getNext() {
        // TODO Auto-generated method stub
        return null;
    }

    public Object getPrevious1() {
        // TODO Auto-generated method stub
        return null;
    }

    public void setPrevious1(Object object) {
        // TODO Auto-generated method stub  
    }

    public Object getNext1() {
        // TODO Auto-generated method stub
        return null;
    }

    public void setNext11(Object object) {
        // TODO Auto-generated method stub
    }

    public void setNext1(Object object) {
        // TODO Auto-generated method stub  
    }

    public Object getNext11() {
        // TODO Auto-generated method stub
        return null;
    }
}
4

0 に答える 0