我刚刚更新了我的代码,如果有人可以帮助我...在我的 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;
}
}