Java 从链接列表中删除元素
如何从链表中删除元素 这是正确的方法吗:Java 从链接列表中删除元素,java,data-structures,linked-list,Java,Data Structures,Linked List,如何从链表中删除元素 这是正确的方法吗: public E remove(E element) { Node search = currentNode; boolean nonStop = true; while(((search.previous) != null) && (nonStop)) { if(search.previous.toString().equals(element.toString()
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;
}
}
public E删除(E元素){
节点搜索=当前节点;
布尔不间断=真;
while(((search.previous)!=null)和&(nonStop)){
if(search.previous.toString().equals(element.toString())){
System.out.println(“找到项!!!”;
search.previous=search.previous.previous;
不间断=假;
}否则{
search=search.previous;
}
}
currentNode=搜索;
返回null;
}
公共类节点{
公共电子数据;
公共节点前向;
公共节点(E数据){
这个数据=数据;
}
public void printNode(){
System.out.println(“节点详细信息:+数据”);
}
@凌驾
公共字符串toString(){
//TODO自动生成的方法存根
返回(字符串)数据;
}
}
问题是当我打印所有元素时,GetAllegements()没有给出正确的答案,remove()方法或GetAllegements中是否有任何问题
public void getAllElements() {
Node<E> aNode = currentNode;
while(aNode != null) {
aNode.printNode();
aNode = aNode.previous;
}
}
public void getAllegements(){
节点阳极=电流节点;
while(阳极!=null){
printNode();
阳极=阳极;
}
}
您的元素没有某种标识符吗?
然后你可以做得更简单,比如这里:你的元素没有某种标识符吗? 然后你可以做得更简单,就像这里:行
if(search.previous.toString().equals(element.toString())
调用节点上的字符串而不是元素上的字符串。行
if(search.previous.toString().equals(element.toString())
调用节点上的字符串而不是元素上的字符串。似乎您的remove方法并没有真正删除任何内容,您应该更新方法中的指针,以便没有任何内容指向要删除的元素,然后垃圾收集器将删除任何内容指向的元素。一些伪代码来说明我的意思:
public remove(Element element){
for (Element e : myLinkedList){
if (e.equals(element)){
if (next != 0)
previousPtr = nextPtr;
else
previousPtr = null;
}
}
}
注意这不是正确的Java代码,只是伪代码给你一个想法,我为你保留一些乐趣!!:) 似乎您的remove方法并没有真正删除任何内容,您应该更新方法中的指针,以便没有任何内容指向要删除的元素,然后垃圾收集器将删除nothing指向的元素。一些伪代码来说明我的意思:
public remove(Element element){
for (Element e : myLinkedList){
if (e.equals(element)){
if (next != 0)
previousPtr = nextPtr;
else
previousPtr = null;
}
}
}
注意这不是正确的Java代码,只是伪代码给你一个想法,我为你保留一些乐趣!!:) 试试这个:
public void remove(E element)
{
Node n = head; // This is the head of the linked list-- It is the starting node of your linked list: For your case "currentNode"
Node tmp;
while(n!=null && !n.data.equals(element))
{
tmp = n;
n = n.previous;
}
if(n==null)
{
// Do your stuff
System.out.println("Element "+element+" not found.");
}
else
{
// Do your stuff
tmp.prev = n.prev;
n.prev = null;
System.out.println("Element "+element+" removed.");
}
}
// Suggestion: This method name should be "printList()"
public void getAllElements()
{
Node n = head; // In your case: "currentNode"
while(n!=null)
{
n.printNode();
n = n.previous;
}
}
试试这个:
public void remove(E element)
{
Node n = head; // This is the head of the linked list-- It is the starting node of your linked list: For your case "currentNode"
Node tmp;
while(n!=null && !n.data.equals(element))
{
tmp = n;
n = n.previous;
}
if(n==null)
{
// Do your stuff
System.out.println("Element "+element+" not found.");
}
else
{
// Do your stuff
tmp.prev = n.prev;
n.prev = null;
System.out.println("Element "+element+" removed.");
}
}
// Suggestion: This method name should be "printList()"
public void getAllElements()
{
Node n = head; // In your case: "currentNode"
while(n!=null)
{
n.printNode();
n = n.previous;
}
}
我已经找到了解决这个问题的办法,谢谢大家的支持
我已经找到了这个问题的解决方案,感谢大家的支持。您的链接列表的详细信息是什么?是单链接还是双链接?它是线性的还是循环的?如果标准在运行时列出,您为什么不使用它?如果您想查看代码或想改进代码,请提出为什么要比较它们的toString()值而不是将它们与equals()进行比较的问题?既然已经存在一个标准的LinkedList,为什么还要重新实现它呢?一件肯定不好的事情是
search.previous.toString().equals(element.toString())
。您应该先使用equals
而不使用toString
。链接列表的详细信息是什么?是单链接还是双链接?它是线性的还是循环的?如果标准在运行时列出,您为什么不使用它?如果您想查看代码或想改进代码,请提出为什么要比较它们的toString()值而不是将它们与equals()进行比较的问题?既然已经存在一个标准的LinkedList,为什么还要重新实现它呢?一件肯定不好的事情是search.previous.toString().equals(element.toString())
。您应该先使用equals
,而不使用toString
。