创建链表时删除了零()方法?

时间:2015-06-05 21:04:43

标签: java linked-list

我正在从头开始以火车的形式创建一个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 + "]");
     }

  }



 }

在过去的一天左右我真的被困在这一点上,似乎无法弄明白。任何帮助都会很棒。如果您需要客户端代码,请说明。谢谢!

1 个答案:

答案 0 :(得分:0)

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

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

其中toDelete是一个Domino对象,在这种情况下为零。由于没有多米诺骨牌现在有toDelete domino的引用,它基本上被删除了。

相关问题