Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Shuffling elements in a linked list.

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Shuffling elements in a linked list.

    I'm trying to shuffle elements in a linked list without using another linked list or array. I'm supposed to manipulate the linked list directly. I'm completely stumped as to how I'd go about doing this. In my node class I only have 4 methods ( setItem(), setNext(), getNext(), and getItem() )

    Some guidance would be appreciated =)


  2. #2
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Shuffling elements in a linked list.

    Well, if this is the Deck program again, unless you changed it I don't believe you are using the LinkedList API, I am no pro but as I understand it, you made a "linked list" of your node classes, but without utilizing the API...if that makes sense. I did a quick search of linkedlist and it would appear you should initialize the list myDeck = new LinkedList(); and then add the nodes and then use get() and set() perhaps...but then what is the point of the Nodes, if you could explain whats going on a little better it would help I think.

    You can move an element in your Node list to the front(Like the way a kid shuffles) with:
    int RANDOM_NUMBER = (int) ((Math.random()*52));
    Node traveller = front;
    for(int i = 0;i<RANDOM_NUMBER;i++ ) {
    	traveller = traveller.getNext();
    }
    Node second = front;
    if(traveller.getNext() != null) front = traveller.getNext();
    traveller.setNext(front.getNext());
    front.setNext(second);
    Do that a random or specified amount of times and it should shuffle your deck.

Similar Threads

  1. Generic Doubly Linked List
    By javapenguin in forum What's Wrong With My Code?
    Replies: 23
    Last Post: October 23rd, 2010, 03:13 PM
  2. [SOLVED] Update on Doubly Linked List
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2010, 07:19 PM
  3. Simple linked list
    By Koren3 in forum Collections and Generics
    Replies: 10
    Last Post: November 2nd, 2009, 03:33 AM
  4. circular linked list
    By student123xyz in forum Collections and Generics
    Replies: 4
    Last Post: August 19th, 2009, 10:40 AM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM