java Java链表搜索和删除方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13638289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Linked List search and delete method
提问by blankwall
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.
我有一个计算机科学课的项目,除了一种方法外,所有的东西都完成了。删除方法。基本上,我正在根据用户输入制作一个链表,我需要能够删除所有节点(已完成)并删除单个指定节点。所以我需要在节点列表中搜索找到要删除的节点并删除它。任何可以提供帮助的东西都值得赞赏。如果您有解决方案,请提供解释,因为我正在尝试学习并解决问题。
I'm not going to give you the GUI because I don't think it is necessary but here is the node class.
我不打算给你 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;
}
}
}
UPDATE
更新
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.
这是我放在一起的方法,它通过第一部分进入 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");
}
}
}
回答by Bohemian
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.
不用勺子喂你答案。删除有点像删除链中的一个链接 - 您剪掉链接并将两个(新)末端连接起来。
So, deleting "B" would mean changing
所以,删除“B”意味着改变
A --> B --> C --> D
to this
对此
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
- 从第一个节点开始算法
- 检查它是否是您要删除的那个
- 如果不是,则转到下一个节点并再次检查(返回上一步)
- 如果是,则使上一个节点的下一个节点成为该节点的下一个节点
- 删除从此节点到下一个节点的引用
回答by chamini2
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
}
the comments should explains whats happening
评论应该解释发生了什么
回答by awolfe91
All you need to do is search through the list, keeping track of where you are. When the node you want to delete is in front of you, set the current node's "next" to the one after the one you want to delete:
您需要做的就是在列表中搜索,跟踪您所在的位置。当您要删除的节点在您面前时,将当前节点的“next”设置为您要删除的节点之后的节点:
for(Node current = list; current.next() != null; current = current.next()){
if(current.next().magazine().equals(toDelete)) current.setNext(current.next().next());
}
Something like that. Hope that helps!
类似的东西。希望有帮助!