Java Linked List搜索和删除方法 [英] Java Linked List search and delete method

查看:104
本文介绍了Java Linked List搜索和删除方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a project for computer science class and have everything done except for one method. The delete method. Basically I am making a linked list from user input and I need to be able to delete all nodes (which is done) and delete a single specified node. So I need to search through the list of nodes find the one to delete and delete it. Anything that can help is appreciated. If you have a solution please offer an explanation as I am trying to learn and just solve the problem.

我不打算给你GUI,因为我不喜欢t认为这是必要的,但这里是节点类。

I'm not going to give you the GUI because I don't think it is necessary but here is the node class.

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循环,从不识别列表中的相同项目。我对输入和删除方法使用完全相同的东西,但它不会识别它。任何帮助表示赞赏。

Here is the method I put together and it goes through the first part into the while loop and never recognizes the same item in the list. I used the exact same thing for the input and delete methods yet it will not recognize it. Any help is appreciated.

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


推荐答案

没有勺子给你答案。删除有点像删除一个链的一个链接 - 你切断链接并加入两个(新的)结束。

Without spoon feeding you the answer. deletion is a bit like removing one link of a chain - you cut out the link and join up the two (new) ends.

因此,删除B意味着更改

So, deleting "B" would mean changing

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

到此

A --> C --> D



在伪代码中,算法为:


In pseudo code, the algorithm would be:


  • 使用第一个节点启动算法

  • 检查是否是您要删除的算法

  • 如果没有,转到下一个节点并再次检查(返回上一步)

  • 如果是,请将上一个节点的下一个节点设为此节点的下一个节点

  • 删除此节点到下一个节点的引用

  • start the algorithm with the first node
  • check if it is the one you want to delete
  • if not, go to the next node and check again (go back to the previous step)
  • if so, make the next node of the previous node the next node of this node
  • remove the reference from this node to the next node

这篇关于Java Linked List搜索和删除方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆