Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否在创建linkedlist时移除零()方法?_Java_Linked List - Fatal编程技术网

Java 是否在创建linkedlist时移除零()方法?

Java 是否在创建linkedlist时移除零()方法?,java,linked-list,Java,Linked List,我正在以火车的形式从头创建一个LinkedList。我有一个名为Domino的类,它创建每个节点,然后我有一个类Train,它包括add、size、remove等方法。我的问题是: removeZeros()方法:我不能有任何参数,但必须删除其中包含零的所有节点。我的程序所做的是查找列表中的所有零,并删除所有节点,直到没有更多的零为止。零被添加到客户端类中 这是我的火车班: public class Train{ private Domino engine; private Dom

我正在以火车的形式从头创建一个LinkedList。我有一个名为Domino的类,它创建每个节点,然后我有一个类Train,它包括add、size、remove等方法。我的问题是:

  • removeZeros()方法:我不能有任何参数,但必须删除其中包含零的所有节点。我的程序所做的是查找列表中的所有零,并删除所有节点,直到没有更多的零为止。零被添加到客户端类中
这是我的火车班:

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

下面是我的Domino类:

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

  }



 }

在过去的一天左右,我真的被困在这个问题上,似乎无法理解。任何帮助都会很好。如果您需要客户端代码,请这样说。谢谢

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

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

其中toDelete是一个Domino对象,在本例中为零。由于现在没有任何domino引用toDelete domino,因此它基本上被删除了

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

toDelete.back.next = toDelete.next;
toDelete.next.back = toDelete.back
其中toDelete是一个Domino对象,在本例中为零。由于现在没有任何domino引用toDelete domino,因此它基本上被删除了