gpt4 book ai didi

java - 创建链表时的removeZero()方法?

转载 作者:太空宇宙 更新时间:2023-11-04 13:47:35 25 4
gpt4 key购买 nike

我正在从头开始创建一个火车形式的 LinkedList。所以我有一个名为 Domino 的类,它创建每个节点,然后我有一个 Train 类,其中包括添加、大小、删除等方法。我的问题是:

  • removeZeros() 方法:我不能有任何参数,但我必须删除所有包含零的节点。我的程序所做的是找到列表中的所有零,然后删除所有节点,直到不再有零为止。这些零已添加到客户端类中。

这是我的火车类(class):

 public class Train{

private Domino engine;
private Domino caboose;
private int insertS;


public Train(){
engine = null;
caboose = engine;
}
/** WHERE IM HAVING TROUBLE
* removeZero() - remove any Dominos from the train that have one or more zero spots
* while maintaining the linked list structure.
*/

// method is now just getting the spot1 0 and printing that
public void removeZero(){
Domino current = engine;
Domino hold = caboose.next;

while (current != hold) {

if(current.spot1 == 0 ||current.spot2 == 0){

current = current.next;
engine = current;
System.out.println("b " + engine);

}else{

current = current.next;


}
}
public String toString(){
String ts = "{ empty }";
if (engine == null) {
return ts;
} else {
Domino hold = engine;
ts = "{ ";
while (hold != caboose) {
ts += hold + ", ";
hold = hold.next;
}
ts += hold + " }";
}
return ts;

}
/**
*
* add(spot1, spot2) - add a Domino to the end of the Train with the given spots
*/
public void add(int spot1, int spot2){
if (engine == null) {
engine = new Domino(spot1,spot2);
caboose = engine;

} else {
caboose.next = new Domino(spot1, spot2, null,caboose);
//tail.next.back = tail;
caboose = caboose.next;
}

}



}

/**
* reversePrint() - like toString, but provides a String that lists
* all of the Dominos that are in the Train in reverse order
*/
public String reversePrint () {
Domino hold = caboose;
String reverse = "{ empty }";

if (engine == null) {
System.out.println(reverse);
} else {
reverse = "{ ";
while (hold != engine){
reverse += hold + ", ";
hold = hold.back;
}
reverse += hold + " }";
}
return reverse;
}
/**
* size() - return the number of Dominos in the Train
*/
public int size(){
int count = 0;
Domino hold = engine;
while(hold != null){
hold = hold.next;
count++;
}
return count;
}
/** insert(spot1, spot2, next, back) - insert a Domino in the middle of
* the Train where spot2 is the same as the spot1 of the next Domino and spot1
* is the same as spot2 of the previous Domino.
* (private)
*/
private void insert(int spot1,int spot2){
if (!(insertS == search)) {
Domino hold = engine;
while (hold != caboose) {
if (hold.spot1 == search) {
Domino newDom = new Domino(spot1, spot2, null,caboose);
hold.next = newDom;
newDom.next.back = newDom;
hold = hold.next;

} else {
hold = hold.next;
}
}
if (hold.spot2 == search) {
add(spot1, spot2);
}
} else {
System.out.println(" ** Error Inserting these values will cause an infinite loop:");
System.out.println(" * * * " + insertS + " and " + search + " * * *");
}

}

/**
* build() - scans through the Train creating links, using insert(spot1, spot2), between
* existing Dominos where the second spot of the first Domino does not match the
* first spot of the second domino, no param
*/

public void build(){
insert(search, insertS);
}

}

这是我的多米诺骨牌类(class):

    public class Domino{
public int spot1; // the leading half how many spots it has
public int spot2; // the trailing half how many spots it has
public Domino next; // a link to the next Domino (type)?
public Domino back; // a link to the previous Domino
private int zero;


/**
* Constructor
* Creates null Domino
*
*/
public Domino(){
this(0,0 , null, null);
}
/**
* Constructor
* a constructor for Domino with given spots
*/
public Domino( int spot1, int spot2){
this(spot1,spot2, null, null);
}
/**
* Constructor
* @param: all fields
* setting variables
*/
public Domino(int spot1, int spot2, Domino next, Domino back){
this.spot1 = spot1;
this.spot2 = spot2;
this.next = next;
this.back = back;
}
/**
* toString(): prints out single Domino
*
*/
public String toString(){
if(this == null){
return("[empty]");
}else{
return("[ " + spot1 + " | "+ spot2 + "]");
}

}



}

在过去的一天左右的时间里,我真的一直被这个问题困扰,似乎无法弄清楚。任何帮助都会很棒。如果您需要客户代码,请说明。谢谢!

最佳答案

在遇到零多米诺骨牌的情况下,您将引擎指定为当前多米诺骨牌。由于engine是列表的头部,这相当于删除包含零的前面的所有项目。链表中的删除通常是通过以下方式完成的:

toDelete.back.next = toDelete.next;
toDelete.next.back = toDelete.back

其中 toDelete 是一个 Domino 对象,在本例中为零。由于现在没有多米诺骨牌引用了 toDelete 多米诺骨牌,因此它实际上已被删除。

关于java - 创建链表时的removeZero()方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30675778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com