如何从链接列表中删除元素?从链接列表中删除元素
是不是正确的方法:
public E remove(E element) {
Node search = currentNode;
boolean nonStop = true;
while(((search.previous) != null) && (nonStop)) {
if(search.previous.toString().equals(element.toString())) {
System.out.println("Item found !!!");
search.previous = search.previous.previous;
nonStop = false;
} else {
search = search.previous;
}
}
currentNode = search;
return null;
}
public class Node<E> {
public E data;
public Node<E> previous;
public Node(E data) {
this.data = data;
}
public void printNode() {
System.out.println("Node details: "+data);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return (String)data;
}
}
当我打印的所有元素的问题是,getAllElements()不给正确的答案,有没有remove()方法或任何问题getAllElements
public void getAllElements() {
Node<E> aNode = currentNode;
while(aNode != null) {
aNode.printNode();
aNode = aNode.previous;
}
}
链接列表的细节是什么?它是单链还是双链?它是线性的还是圆形的? – johnchen902
如果标准列表在运行时出现任何不使用的原因? –
如果您想查看您的代码或希望改善您的代码,请在http://codereview.stackexchange.com/上发布您的问题。 –