Java 如何在链表中创建洗牌方法

Java 如何在链表中创建洗牌方法,java,Java,我正在尝试在我的LinkedList中创建一个shuffle方法。目前,我的洗牌方法是生成一个介于1到10之间的随机数n,然后取第n张牌并将其移动到前面。然后它将在随机的时间内循环。然而,我当前的代码似乎不起作用,因为它所带的卡只是被移除,而不是放在前面 public void shuffle() { Node current = head; int randomX = (int) (Math.random() * 10 + 1); for (in

我正在尝试在我的LinkedList中创建一个shuffle方法。目前,我的洗牌方法是生成一个介于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;
            }
        }


    }
public void shuffle(){
节点电流=头;
int randomX=(int)(Math.random()*10+1);
对于(int x=0;x
看起来您的
将随机选择的节点移动到头部
是错误的。它应该位于选择要移动的循环的外部

一些评论会让这一点变得显而易见

public void shuffle() {
    Node current = head;
    // How many times to shuffle.
    int randomX = (int) (Math.random() * 10 + 1);
    // Move random node to head random number of times.
    for (int x = 0; x < randomX; x++) {
        // Pick the one to move.
        int randomY = (int) (Math.random() * 10 + 1);
        // Go find it.
        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 {
                // Hit end of list - go back to start.
                current = head;
                System.out.println("nope");
                current = current.getNext();

            }
        }
        // Bring the chosen one to `head` - **** I moved this OUTSIDE the loop above.
        if (current.getPrevious() != null) {
            current.getPrevious().setNext(current.getNext());
            head.setPrevious(current);
            current.setPrevious(head);

        }
        head = current;
    }
}
public void shuffle(){
节点电流=头;
//洗牌多少次。
int randomX=(int)(Math.random()*10+1);
//将随机节点随机移动到头部的次数。
对于(int x=0;x
看起来您的
将随机选择的节点移动到头部
是错误的。它应该位于选择要移动的循环的外部

一些评论会让这一点变得显而易见

public void shuffle() {
    Node current = head;
    // How many times to shuffle.
    int randomX = (int) (Math.random() * 10 + 1);
    // Move random node to head random number of times.
    for (int x = 0; x < randomX; x++) {
        // Pick the one to move.
        int randomY = (int) (Math.random() * 10 + 1);
        // Go find it.
        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 {
                // Hit end of list - go back to start.
                current = head;
                System.out.println("nope");
                current = current.getNext();

            }
        }
        // Bring the chosen one to `head` - **** I moved this OUTSIDE the loop above.
        if (current.getPrevious() != null) {
            current.getPrevious().setNext(current.getNext());
            head.setPrevious(current);
            current.setPrevious(head);

        }
        head = current;
    }
}
public void shuffle(){
节点电流=头;
//洗牌多少次。
int randomX=(int)(Math.random()*10+1);
//将随机节点随机移动到头部的次数。
对于(int x=0;x
确保在找到要查找的节点时,将其上一个节点的“下一个”设置为“下一个”,并将该节点的“下一个上一个”设置为“上一个”

Node temp = head;
int randomX = (int) (Math.random() * 10 + 1);

//simply go until the randomX
while(randomX-- > 0 && temp.getNext() != null)
    temp = temp.getNext();

//remove the Nth node from the list
temp.getPrevious().setNext(temp.getNext());

if(temp.getNext() != null)
    temp.getNext().setPrevious(temp.getPrevious());

//set it to point to the head
temp.setNext(head);
temp.setPrevious(null);

//now set the Head to the Nth node we found
head = temp;

确保在找到要查找的节点时,将其上一个节点的“下一个”设置为“下一个”,并将该节点的“下一个上一个”设置为“上一个”

Node temp = head;
int randomX = (int) (Math.random() * 10 + 1);

//simply go until the randomX
while(randomX-- > 0 && temp.getNext() != null)
    temp = temp.getNext();

//remove the Nth node from the list
temp.getPrevious().setNext(temp.getNext());

if(temp.getNext() != null)
    temp.getNext().setPrevious(temp.getPrevious());

//set it to point to the head
temp.setNext(head);
temp.setPrevious(null);

//now set the Head to the Nth node we found
head = temp;

你需要使用链表吗?是的,我必须使用链表。我建议看一看java.util.Collections.shuffle实现我不能使用Collections shuffle,因为它实际上是一个自我实现的双链表。你需要使用链表吗?是的,为此,我必须使用链表。我建议看一看java.util.Collections.shuffle实现。我不能使用Collections shuffle,因为它实际上是一个自我实现的双链表。感谢您的代码,这对我正在尝试的工作很有用,但是,因为我使用的是双链接列表,所以我不得不对其进行一些更改。感谢您的代码,这对于我尝试执行的操作是有效的,但是因为我使用的是双链接列表,所以我不得不对其进行一些更改。