【问题标题】:deleting first node from singly linked list Java [closed]从单链表Java中删除第一个节点[关闭]
【发布时间】:2013-05-20 15:20:40
【问题描述】:

我有一个链表数据结构,我正在测试 deleteInfo() 函数。但是,当我尝试删除链表中的最后一项时,出现错误。链表通过从顶部插入而增长,因此在这种情况下,最后一项实际上是插入的第一项。

代码如下:

public lList deleteInfo(String outInfo) {
        if ( info == outInfo ) {
            lList link = nextList().deleteInfo( outInfo );
            info = link.info;
            nextList = link.nextList();
        }
        else if ( nextList() != null )
            nextList().deleteInfo( outInfo );
        return this;
    }

public void insert(String in_Info) {
        if ( isEmpty() == false ) {
            lList entry = new lList(); // New entry is created to store new list
            entry.info = info; //Store the current list's information into this list
            entry.nextList = nextList;
            nextList = entry; //Next list now points to the entry created
        }

        info = in_Info;
    }

public lList nextList() {
        if ( isEmpty() == false )
            return nextList;
        return null;
    }

有人可以告诉我一种允许删除最后一个列表的方法吗?我知道问题出在第一个 if 语句中,因为它可能试图访问一个空列表,因为最后一个列表没有 nextList。但我不知道有其他方法可以做到这一点;所以任何帮助表示赞赏

【问题讨论】:

  • 这个片段本身毫无意义。看起来您正试图从节点中删除一个节点。
  • 是的,Brian 这正是我想要做的,但不是删除节点。我只是将更改它包含的数据链接到它之后不包含与它相同的数据的下一个节点的数据
  • 您将遇到的问题是您无法删除自己。如果你只有一个节点呢?
  • 这就是我的问题所在。这很容易在不使用递归和使用简单的for循环的情况下完成,但是教授说大多数方法都可以使用递归来编写,这就是我想尝试的原因
  • 对 - 所以,你可以递归地这样做,但你必须传入对前一个节点的引用,因为这是一个单链表.要删除一个节点,您需要对前一个节点的引用和对下一个节点的引用。或者……向前看两个节点。

标签: java data-structures linked-list nodes


【解决方案1】:
public lList deleteInfo(String outInfo) {
        if ( nextList() != null && nextList().info == outInfo ) {
            lList link = nextList();

            nextList = link.nextList();
        }
        else if (nextList() != null){
            nextList().deleteInfo( outInfo );
        }
        return this;
    }

【讨论】:

  • 如果链接列表中的最后一个条目包含正在查找的信息,您的方法将忽略它
  • @Smac89 no...它将适用于最后一个节点..基本上在我的代码中我们正在删除下一个节点..不是当前节点..所以如果nextList()==null那么你在列表的末尾没有匹配的元素。
  • 所以如果最后一个元素有数据...倒数第二个元素if ( nextList() != null && nextList().info == outInfo ) { 将是真的...和second last nextelement will be set to null
  • 不,我只是试了一下,它甚至没有“删除”它应该删除的节点,而是“删除”了错误的节点
  • @Smac89 检查更新的答案removed info=nextList().info
【解决方案2】:

如果检查是否要删除最后一个,您可能需要在第一个 if 中添加一个内部 if:

 public lList deleteInfo(String outInfo) {
        if ( info == outInfo ) {
         if( nextList() == null ) {
               //delete current list as u want
               return this;
         }
            lList link = nextList().deleteInfo( outInfo );
            info = link.info;
            nextList = link.nextList();
        }
        else if ( nextList() != null )
            nextList().deleteInfo( outInfo );
        return this;
    }

【讨论】:

  • 如果它包含信息,它将仍然是链接列表中的最后一项
【解决方案3】:

这是最终对我有用的方法:

//lList : deleteInfo()
//Pre: information to delete
//Post : List not containing information is returned
public lList deleteInfo(String outInfo) {
    if ( nextList() != null ) {
        lList link = nextList().deleteInfo( outInfo );
        if ( nextList().info == outInfo ) { //Handles information that is located at end of list
            nextList = link.nextList();
        }
        else if ( info == outInfo ){//Handles information that is located at beginning of list
            info = link.info;
            nextList = link.nextList();
        }
        else nextList().deleteInfo( outInfo );
    }
    return this;
}

请注意,当只有一个列表并且您尝试删除该列表中的信息时,它不起作用。我认为这是一件好事,因为你不能取消对 self 的引用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多