【问题标题】:Java Linked List search and delete methodJava链表查找和删除方法
【发布时间】:2012-11-30 02:46:13
【问题描述】:

我有一个计算机科学课程项目,除了一种方法外,我已经完成了所有工作。删除方法。基本上我正在从用户输入创建一个链接列表,我需要能够删除所有节点(已完成)并删除单个指定节点。所以我需要在节点列表中搜索找到要删除的节点并删除它。任何可以提供帮助的东西都会受到赞赏。如果您有解决方案,请提供解释,因为我正在努力学习并解决问题。

我不会给你 GUI,因为我认为没有必要,但这里是节点类。

public class MagazineList {
private MagazineNode list;

    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) {
    //Delete Method Goes Here
}

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;
    }
}
}

更新

这是我放在一起的方法,它通过第一部分进入 while 循环,并且永远不会识别列表中的相同项目。我对输入和删除方法使用了完全相同的东西,但它不会识别它。任何帮助表示赞赏。

public void delete (Magazine mag) {
MagazineNode current = list;
MagazineNode before;

before = current;

if(current.equals(mag)){
    before.next = current;
    System.out.println("Hello");
}

while ((current = current.next)!= null){
    before = current.next;
    System.out.println("Hello Red");

    if(current.equals(mag)){
        current = before;
        System.out.println("Hello Blue");
    }
}
 }

【问题讨论】:

    标签: java linked-list


    【解决方案1】:

    不用勺子喂你答案。删除有点像删除链中的一个链接 - 您切断链接并将两个(新)末端连接起来。

    所以,删除“B”意味着改变

    A --> B --> C --> D
    

    到这里

    A --> C --> D
    


    在伪代码中,算法是:

    • 从第一个节点开始算法
    • 检查是否是您要删除的那个
    • 如果不是,则转到下一个节点,再次检查(返回上一步)
    • 如果是,则使前一个节点的下一个节点成为本节点的下一个节点
    • 删除从这个节点到下一个节点的引用

    【讨论】:

    • 我已经阅读了该内容,但对此感到非常困惑。您能否就该功能的搜索部分提供一些帮助。我有一个想法,在搜索节点之前有一个指针节点指向一个,当搜索​​节点找到我将它设置为空的项目时。这听起来像是需要做的事情吗?
    • 是的。应使用.equals() 方法进行测试以查看节点是否“匹配”搜索节点。我建议在纸上使用节点框和指向下一个节点的指针的箭头来绘制它。了解它是件好事,而不仅仅是“编写答案”
    • 每当我尝试计算等号时,当前节点只是一堆字母和数字,无论如何都要将其恢复为字符串?
    • @blankwall 不要使用toString()。而是为您的节点类覆盖(即实现)equals() 方法,并比较其中有意义的内容;也许是一些String 字段,例如name
    【解决方案2】:
    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
    }
    

    cmets 应该解释发生了什么

    【讨论】:

      【解决方案3】:

      您需要做的就是在列表中搜索,跟踪您所在的位置。当你要删除的节点在你面前时,将当前节点的“next”设置为你要删除的节点之后的节点:

      for(Node current = list; current.next() != null; current = current.next()){
         if(current.next().magazine().equals(toDelete)) current.setNext(current.next().next());
      }
      

      类似的东西。 希望对您有所帮助!

      【讨论】:

      • 看起来不错,只是需要检查要删除的项目是否在列表的最前面。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-04
      • 2012-03-23
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      相关资源
      最近更新 更多