JAVA从循环链表中删除多个连续元素

时间:2018-10-04 03:34:11

标签: java linked-list

我在处理单个循环链表的项目上进展非常快,但在一点上仍然遇到麻烦。我的麻烦源于无法/不知道如何在包含四个连续颜色中的第一个的节点之前调用该节点,并将其指向起点。

  

提示大理石游戏:   将随机的弹珠放在一个圆圈中,如果出现四个相同颜色的弹珠,请将其删除,然后将颜色分数加到总分数的四倍。

如果有人能帮助我解决该问题,我将不胜感激。

这是我正在处理的代码:

public void deleteQuadruples()
{
    //if the list has 4 nodes, delete all and make a new head
    if (size == 4 && (start.getaMarble().getNumber() == start.getNextLink().getaMarble().getNumber()
            && start.getNextLink().getaMarble().getNumber() == start.getNextLink().getNextLink().getaMarble().getNumber()
            && start.getNextLink().getNextLink().getaMarble().getNumber() == start.getNextLink().getNextLink().getNextLink().getaMarble().getNumber()))
    {
        sum = 4*start.getaMarble().getNumber();

        start = new Link(null, null);

        addFirstMarble();
    }

    //else go thru the list and find whether or not there are four consecutive marbles of the same color/number and if so take them out
    else 
    {
        Link tmp = start;

        if (tmp == null)
        {
            return;
        }

        do
        {
            /*int colorNumber = tmp.getaMarble().getNumber();
              int counter = 1;
              tmp = tmp.getNextLink();
              if(colorNumber == tmp.getaMarble().getNumber())
              {
                  counter++;
              }

              if(counter == 4)
              {
                  score += 4*tmp.getaMarble().getNumber();
                  counter = 1;
                  //delete the 4 consecutive elements
              }

              if(tmp.getNextLink() == start && counter == 3 && start.getaMarble().getColor() == tmp.getaMarble().getColor())
              {
                  score += 4*tmp.getaMarble().getNumber();
                  start = start.getNextLink();
                  //delete the 4 consecutive elements
              }*/

            for(Link cursor = start; cursor != end; cursor = cursor.getNextLink)
            {
                Link temp;

                counter = 1;
                if(cursor.getaMarble().getNumber() == cursor.getNextLink().getaMarble().getNumber())
                {
                    counter++;
                }

                if(cursor.getaMarble().getNextLink().getNumber() != cursor.getNextLink().getNextLink().getaMarble().getNumber())
                {
                    counter = 1;
                }

                if (counter == 4)
                {
                    //deletes the four consecutive nodes with same color 
                    //something.getNextLink() = start;  
                    score += 4* cursor.getaMarble.getNumber();
                }
            }
        } 

        while (tmp != start);
    }
}

1 个答案:

答案 0 :(得分:0)

我对您的代码进行了一些更改(它可能无法编译,但是会给出想法)

public void deleteQuadruples()
{
  if (start == null || size == 3)
          return;

  //if the list has 4 nodes, delete all and make a new head
  if (size == 4 && (start.getaMarble().getNumber() == start.getNextLink().getaMarble().getNumber()
      && start.getNextLink().getaMarble().getNumber() == start.getNextLink().getNextLink().getaMarble().getNumber()
      && start.getNextLink().getNextLink().getaMarble().getNumber() == start.getNextLink().getNextLink().getNextLink().getaMarble().getNumber()))
  {
      sum = 4*start.getaMarble().getNumber();
      start = new Link(null, null);
      addFirstMarble();
  }

  //else go thru the list and find whether or not there are four consecutive marbles of the same color/number and if so take them out
  else 
  {
      Node end = start;

      while (end.getNextLink().getaMarble().getNumber() != start.getNextLink().getaMarble().getNumber())
      { end = end.getNextLink(); }

      // no data
      if(end.getNextLink().getaMarble().getNumber() == start.getaMarble().getNumber())
        return;

      // we have data
      else {
        sum = 4*start.getaMarble().getNumber();
     // loop four times
     // set the end.getNextLink() with start.getNextLink()
     }  
  }
}