私はLinkedListでシャッフルメソッドを作成しようとしています。現在、シャフリングの方法は、1から10までの乱数nを生成し、n番目のカードを取り出して前面に移動することです。その後、ランダムな時間内にループします。しかし、私の現在のコードは、前面に持っていくのではなく、取り出したカードが取り除かれるので、うまくいかないようです。リンクリストでシャッフルメソッドを作る方法
public void shuffle() {
Node current = head;
int randomX = (int) (Math.random() * 10 + 1);
for (int x = 0; x < randomX; x++) {
int randomY = (int) (Math.random() * 10 + 1);
for (int y = 0; y < randomY; y++) {
if (current.getNext() != null) {
current = current.getNext();
System.out.println("Yup");
System.out.println(current);
System.out.println(y);
}
else {
current = head;
System.out.println("nope");
current = current.getNext();
}
if (current.getPrevious() != null){
current.getPrevious().setNext(current.getNext());
head.setPrevious(current);
current.setPrevious(head);
}
head = current;
}
}
}
にノードの次の以前のに設定されていることを確認してくださいあなたはリンクされたリストを使用する必要がありますか? –
はい、私はこのためのリンクリストを使用する必要があります –
https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle –