9までの値があると言う循環リンクリストの数字を削除する方法を作成する必要があります
1 2 3 4 5 6 7 8 9
そして、通過する4番目の整数ごとに継続的に削除したい場合、次のようになります
5 6 7 8 9 1 2 3; // 4 is removed
9 1 2 3 5 6 7; // 8 is removed
5 6 7 9 1 2; // 3 is removed
1 2 5 6 7; // 9 is removed
7 1 2 5; // 6 is removed
7 1 2; // 5 is removed
1 2; // 7 is removed
1; // 2 is removed
要素を通過する移動と、要素を削除する削除を作成する必要がありますが、それは自分で行うことができます。toString(); に問題があります。メソッド、私は現在値を返していません。
class Digit{
class DigitNode
{
public int num=0; // Digit's position in line
public DigitNode next=null; // Reference to next digit
/**
* Digit constructor, initializes number
*/
public DigitNode(int number)
{
//
num = number;
next = null;
}
}
private int number;
private DightNode current = null; // Linked list of digits
private DigitNode tail = null; // Tracks end of list as it is constructed
/**
* constructs a circular linked list of
* @param n DigitNodes, current is the first DigitNode and tail is the last DigitNode(next of tail is current)
*/
public Digit(int n)
{
//
number = n;
current = null;
tail = null;
}
/*
* prints all Digits starting from current until tail
*/
@Override
public String toString()
{
//
String strVal = "";
DigitNode position = current;
while (position != null) {
strVal = strVal + position + " ";
position = current.next;
}
return strVal;
}
私には、現在の値として position を割り当てていることを理解し1
ています。次に、 position を次の値であると呼び、 after になるまで続行します。したがって、する必要があります。しかし、残念ながら何も返されません。デバッグを試み、マーカーをいくつか配置して、何かを返すかどうかを確認しましたが、そうではありませんでした。null
strVal
[1]
" "
[2]
null
9
strVal
1 2 3 4 5 6 7 8 9
System.out.prinln();