一直在寻找解决我的问题的方法。已经阅读了很多关于覆盖等于以正确比较链表的节点但已经卡住的内容。基本上我正在尝试搜索我的列表并找到一个等于它的节点并将其删除。我找到了查看链接列表的方法,但是当我返回节点时,它只是胡言乱语。我正在尝试找到一种将其放入字符串并将其与另一个字符串进行比较的方法。无论如何,这里是当前不起作用的代码和我的 equals 方法。
public class MagazineList {
private MagazineNode list;
private Object obj;
public MagazineList(){
list = null;
}
public void add(Magazine mag){
MagazineNode node = new MagazineNode(mag);
MagazineNode current;
if(list==null)
list = node;
else{
current = list;
while(current.next != null)
current = current.next;
current.next = node;
}
}
public void insert(Magazine mag)
{
MagazineNode node = new MagazineNode (mag);
// make the new first node point to the current root
node.next=list;
// update the root to the new first node
list=node;
}
public void deleteAll(){
if(list == null){
}
else{
list = null;
}
}
public void delete (Magazine mag) {
MagazineNode current = this.list;
MagazineNode before;
//if is the first element
if (current.equals(mag)) {
this.list = current.next;
return; //ending the method
}
before = current;
//while there are elements in the list
while ((current = current.next) != null) {
//if is the current element
if (current.equals(mag)) {
before.next = current.next;
return; //endind the method
}
before = current;
}
//it isnt in the list
}
public boolean equals(Object other) {
System.out.println("Here in equals" + other + this);
// Not strictly necessary, but often a good optimization
if (this == other)
return true;
else{
return false;
}
}
@ Override
public String toString(){
String result = " ";
MagazineNode current = list;
while (current != null){
result += current.magazine + "\n";
current = current.next;
}
return result;
}
private class MagazineNode {
public Magazine magazine;
public MagazineNode next;
public MagazineNode(Magazine mag){
magazine = mag;
next = null;
}
}
}