如何在链表中洗牌? [英] How do I shuffle nodes in a linked list?

查看:139
本文介绍了如何在链表中洗牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚为我的Java2课程开始了一个项目,我完成了一个工作。我只是不能得到
我的头围绕这个方法。特别是当这个作业没有让我们使用任何其他的DATA STRUCTURE或者随机播放方法从java。



所以我有一个Deck.class,其中我已经创建了一个包含52个节点的链接列表,其中包含52个卡。

  public class Deck {

private Node theDeck;
private int numCards;

public Deck()
{
while(numCards< 52)
{
theDeck = new Node(new Card(numCards),theDeck) ;
numCards ++;
}
}

public void shuffleDeck()
{
int rNum;
int count = 0;
Node current = theDeck;
卡卡;
int range = 0;

while(count!= 51)
{
//在temp变量中存储当前节点内的任何东西
tCard = current.getItem();

//生成0-51之间的随机数
rNum =(int)(Math.random()* 51);

//在循环上发送一个随机次数
(int i = 0; i current = current.getNext ); ******< - (Btw这是我得到我的错误,我知道为什么但idk如何阻止它。)

//所以无论当前登陆将该项目存储在该节点中,并将其存储在
theDeck.setItem(current.getItem())上。

//现在使用开始时的temp变量,并将其存储在当前着陆的地方
current.setItem(tCard);

//发送当前回到甲板的开头
current = theDeck;

//我为另一个循环创建了一个计数器,我想做
count ++;

//发送一个循环的计数次数,这样它不会刷新已经被洗牌的卡片。 (int i = 0; i< count; i ++)
current = current.getNext();
****< - 不能确定这最后一个循环,因为如果我不洗牌已经洗牌的卡不会被视为合法洗牌?我认为? ****这也是我有时得到一个nullpointerexception ****

}

}

}

现在我收到不同种类的错误
当我调用这个方法:




  • 它有时会洗牌只有2张牌,但有时会洗牌3 - 5卡,然后给我一个NullPointerException。
    我已经指出,在我上面的代码


  • 中,我给出了这个错误的星号,我得到它洗牌13卡,那么每当它做到这一点,它并没有使他们正确的方式洗牌。一张卡总是重复。


  • 在另一点我有52张卡片通过while循环,但是再次重复一张卡片。




所以我真的需要一些输入,我做错了什么。在我的代码结束之前,我认为我的逻辑是完全错误的,但我似乎无法想像出来。

解决方案

sup guys所以我终于弄清楚了,这是我的洗牌方法的更新

  public void shuffleDeck()
{

节点当前;
节点随机;
卡cTemp;
int rand;

rand =(int)(Math.random()* numCards); (int i = 0; i< numCards; i ++)




$ b current = theDeck;
random = theDeck;

rand =(int)(Math.random()* numCards); (int k = 0; k< rand; k ++)


$ b random = random.getNext();

cTemp = current.getItem();
current.setItem(random.getItem());

random.setItem(cTemp);
}
}
}



它的工作^ _ ^
感谢输入tho。 =)


I just started a project for my Java2 class and I've come to a complete stop. I just can't get my head around this method. Especially when the assignment does NOT let us use any other DATA STRUCTURE or shuffle methods from java at all.

So I have a Deck.class in which I've already created a linked list containing 52 nodes that hold 52 cards.

public class Deck {

    private Node theDeck;
    private int numCards;

    public Deck () 
    {
        while(numCards < 52)
        {
            theDeck = new Node (new Card(numCards), theDeck);
            numCards++;
        }
    }

    public void shuffleDeck()
    {           
        int rNum;
        int count = 0;
        Node current = theDeck;
        Card tCard;
        int range = 0;  

        while(count != 51)
        {   
            // Store whatever is inside the current node in a temp variable
               tCard = current.getItem();

            // Generate a random number between 0 -51      
                rNum = (int)(Math.random()* 51);

            // Send current on a loop a random amount of times
               for (int i=0; i < rNum; i ++)
                current = current.getNext();   ******<-- (Btw this is the line I'm getting my error, i sort of know why but idk how to stop it.)

            // So wherever current landed get that item stored in that node and store it in the first on
            theDeck.setItem(current.getItem());

            // Now make use of the temp variable at the beginning and store it where current landed
            current.setItem(tCard);

            // Send current back to the beginning of the deck
            current = theDeck;

            // I've created a counter for another loop i want to do     
            count++;

            // Send current a "count" amount of times for a loop so that it doesn't shuffle the cards that have been already shuffled.   
            for(int i=0; i<count; i++)
             current = current.getNext();  ****<-- Not to sure about this last loop because if i don't shuffle the cards that i've already shuffled it will not count as a legitimate shuffle? i think? ****Also this is where i sometimes get a nullpointerexception****

        }

    }

}

Now I get different kinds of errors When I call on this method:

  • it will sometimes shuffle just 2 cards but at times it will shuffle 3 - 5 cards then give me a NullPointerException. I've pointed out where it gives me this error with asterisks in my code above

  • at one point I got it to shuffle 13 cards but then everytime it did that it didn't quite shuffle them the right way. one card kept always repeating.

  • at another point I got all 52 cards to go through the while loop but again it repeated one card various times.

So I really need some input in what I'm doing wrong. Towards the end of my code I think my logic is completely wrong but I can't seem to figure out a way around it.

解决方案

sup guys so i finally figured it out and this is my update on my shuffle method

public void shuffleDeck()
{           

    Node current;
    Node random;
    Card cTemp;
    int rand;

    rand = (int)(Math.random() * numCards);

    for (int j=0; j<rand; j++)
    {
        for (int i=0; i<numCards; i++)
        {
            current = theDeck;
            random = theDeck;

            rand = (int)(Math.random() * numCards);

            for(int k = 0; k < rand; k++)
                random = random.getNext();

            cTemp = current.getItem();
            current.setItem(random.getItem());

            random.setItem(cTemp);
        }
    }
}



AND IT WORKS ^_^ Thanks for the input tho. =)

这篇关于如何在链表中洗牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆