英文:
Given an element , how to erase all the nodes with the given element value?
问题
好的,以下是您提供的代码的中文翻译:
好的,我有一个名为 Node 的类,其中一些字段定义为整数值(val),另一个字段是 Node 类型的引用。此外,我还有一个名为 listaArmada 的第二个类,其中有一个名为 head 的 Node 字段,表示列表的开头。我创建了一个名为 borrarEle 的方法类,该方法接收一个创建好的带有一些随机值的列表和一个整数作为参数。该方法必须在列表中搜索具有给定元素值的节点,并消除包含该值的所有节点。它能够工作,但只能删除找到的第一个元素,而不是所有具有该元素值的节点。应该能够删除具有该元素值的任何节点,而不考虑其位置。
> 列表:9,1,2,12,14,2,7,7,1,10
>
> 要消除的值:7
>
> 期望输出:9,1,2,12,14,2,1,10
>
> 实际输出:9,1,2,12,14,2,**7**,1,10
-------NODE 类
```java
public class Nodo {
int val;
Nodo siguiente;
public Nodo(int ob, Nodo nxt){
this.val = ob;
this.siguiente = nxt;
}
}
-------SECOND 类
public class listaArmada {
static public Nodo head;
static public int size;
static void borrarEle(int n, listaArmada ls){
Nodo anterior = null;
if(ls.head.val == n){//元素在头部
ls.head = ls.head.siguiente;
}else{
Nodo temp = ls.head;
while(temp.siguiente.val != n){
temp = temp.siguiente;
}
Nodo sg = temp.siguiente.siguiente;
temp.siguiente = sg;
}
}
}
英文:
Ok, I have my Node class with some fields defined as an integer for the value (val) and a Node one for a reference. Also I have a second class called listaArmada with a Node head field that indicates the beginning of the list. I created a method class called borrarEle that receives a created list with some random values and an integer. The method must search within the list for nodes with the value of the given element and eliminate all nodes that contain it. It works but only removes the first element found and not the others as well. Should be able to remove any node that has that element regardless of position.
> List: 9,1,2,12,14,2,7,7,1,10
>
> value to eliminate: 7
>
> Expected output: 9,1,2,12,14,2,1,10
>
> Real output: 9,1,2,12,14,2,7,1,10
-------NODE CLASS
public class Nodo {
int val;
Nodo siguiente;
public Nodo(int ob, Nodo nxt){
this.val = ob;
this.siguiente = nxt;
}
}
-------------------------------SECOND CLASS
public class listaArmada {
static public Nodo head;
static public int size;
static void borrarEle(int n, listaArmada ls){
Nodo anterior = null;
if(ls.head.val == n){//EL ELEMENTO ESTÁ EN LA CABEZA
ls.head = ls.head.siguiente;
}else{
Nodo temp = ls.head;
while(temp.siguiente.val != n){
temp = temp.siguiente;
}
Nodo sg = temp.siguiente.siguiente;
temp.siguiente = sg;
}
}
答案1
得分: 0
以下是翻译好的部分:
你提供的问题中的代码不是一个最小可复现示例(MRE),因此在下面的代码中,我根据你的代码添加了一些内容,以创建一个MRE。基本上,ListaArmada
类是一个单向链表,其中列表中的每个节点都是 Nodo
类的实例。
首先,我在 ListaArmada
类中添加了 append()
方法,以便能够创建一个列表。其次,我在 ListaArmada
和 Nodo
类中都重写了 toString()
方法,以便于测试和调试。
我使用了你在问题中提供的示例数据。另外,我遵循了面向对象编程原则和Java命名约定。
public class ListaArmada {
// ...(省略部分代码)
public void append(int val) {
// ...(省略部分代码)
}
public void borrarEle(int n) {
// ...(省略部分代码)
}
public String toString() {
// ...(省略部分代码)
}
public static void main(String[] args) {
// ...(省略部分代码)
}
}
class Nodo {
// ...(省略部分代码)
}
append()
方法简单地在链表末尾添加一个新节点。
你的 borrarEle()
方法中的 while
循环是错误的。你希望遍历列表中的所有节点。当节点的 siguiente
为 null 时,你知道已经到达列表的最后一个节点。你还需要处理删除列表中第一个节点的情况。在这种情况下,你还必须更新 ListaArmada
类中的 head
。
运行上述代码会产生以下输出。
9 -> 1 -> 2 -> 12 -> 14 -> 2 -> 7 -> 7 -> 1 -> 10 -> END
9 -> 1 -> 2 -> 12 -> 14 -> 2 -> 1 -> 10 -> END
英文:
The code in your question is not a minimal, reproducible example (MRE) so in the below code I added to your code in order to create a MRE. Basically, class ListaArmada
is a singly linked list where each node in the list is an instance of class Nodo
.
First I added method append()
to class ListaArmada
so as to be able to create a list. Second, I overrode method toString()
in both ListaArmada
and Nodo
classes so as to facilitate testing and debugging.
I used the sample data you provided in your question. Also, I used OOP principles and java naming conventions.
public class ListaArmada {
public Nodo head;
public void append(int val) {
Nodo n = new Nodo(val, null);
if (head == null) {
head = n;
}
else {
Nodo next = head.getSiguiente();
Nodo prev = head;
while (next != null) {
prev = next;
next = next.getSiguiente();
}
prev.setSiguiente(n);
}
}
public void borrarEle(int n) {
if (head != null) {
Nodo curr = head;
Nodo prev = head;
while (curr != null) {
if (curr.getVal() == n) {
prev.setSiguiente(curr.getSiguiente());
if (curr == head) {
head = curr.getSiguiente();
}
}
else {
prev = curr;
}
curr = curr.getSiguiente();
}
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
Nodo curr = head;
while (curr != null) {
sb.append(curr);
curr = curr.getSiguiente();
}
sb.append("END");
return sb.toString();
}
public static void main(String[] args) {
ListaArmada la = new ListaArmada();
la.append(9);
la.append(1);
la.append(2);
la.append(12);
la.append(14);
la.append(2);
la.append(7);
la.append(7);
la.append(1);
la.append(10);
System.out.println(la);
la.borrarEle(7);
System.out.println(la);
}
}
class Nodo {
private int val;
private Nodo siguiente;
public Nodo(int ob, Nodo nxt) {
this.val = ob;
this.siguiente = nxt;
}
public int getVal() {
return val;
}
public Nodo getSiguiente() {
return siguiente;
}
public void setSiguiente(Nodo n) {
siguiente = n;
}
@Override
public String toString() {
return String.format("%d -> ", val);
}
}
The append()
method simply appends a new node to the end of the list.
The while
loop in your borrarEle()
method is wrong. You want to iterate through all the nodes in the list. You know that you have reached the last node in the list when its siguiente
is null. You also need to handle the situation where you delete the first node in the list. In that case you also have to update head
in class ListaArmada
.
Running the above code produces the following output.
9 -> 1 -> 2 -> 12 -> 14 -> 2 -> 7 -> 7 -> 1 -> 10 -> END
9 -> 1 -> 2 -> 12 -> 14 -> 2 -> 1 -> 10 -> END
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论