Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Randomnizing a LinkedList
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
a22asin




PostPosted: Mon Mar 31, 2014 11:12 am   Post subject: Randomnizing a LinkedList

Hi, so im working on an assignment where i have 3 classes, card, cardDeck and tester. What i need to do is convert the cardDeck classes to use LinkedList instead of an array. The problem im having is trying to figure out how to shuffle the deck using the linkedList.

This is what im given:
code:

   public void shuffle()
   {
      // randomly choose position among 0 to 51  to swap with position 0
      // randomly choose position among 1 to 51  to swap with position 1
      // ...
      // randomly choose position among k to 51  to swap with position k
      // ...
      // randomly choose position among 50 to 51 to swap with position 50
     
      Random rand = new Random();
      initialize();
      for (int k = 0; k <= DECK_SIZE - 2; k++)
      {
         int index = rand.nextInt(DECK_SIZE - k) + k;
         Card temp = deck[k];
         deck[k] = deck[index];
         deck[index] = temp;
      }
   }



and somehow i need to convert this method to use LinkedList, but i have no idea on how to do this (this is my 1st time working with LinkedList).
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Mar 31, 2014 2:18 pm   Post subject: RE:Randomnizing a LinkedList

Conceptually they are the same thing -- you pick the two elements to swap, and then swap them.

With arrays, you have to figure out how to not overwrite a value (you've done that with temp variable).

With linked lists, you have to figure out how to end up with a valid structure after the swap operation. Sketching out a few diagrams would be very helpful, to get an idea of exactly what you need to change around.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Krocker




PostPosted: Mon Mar 31, 2014 3:34 pm   Post subject: Re: RE:Randomnizing a LinkedList

Tony @ Mon Mar 31, 2014 2:18 pm wrote:
Conceptually they are the same thing -- you pick the two elements to swap, and then swap them.

With arrays, you have to figure out how to not overwrite a value (you've done that with temp variable).

With linked lists, you have to figure out how to end up with a valid structure after the swap operation. Sketching out a few diagrams would be very helpful, to get an idea of exactly what you need to change around.


ok... how do i select a random index in the linkedlist to be moved?
Tony




PostPosted: Mon Mar 31, 2014 4:27 pm   Post subject: RE:Randomnizing a LinkedList

Same way you would pick random numbers for an array implementation -- something you've already done above.

A linked list is just a storage structure. Same way it could be an array, or a line in a text file, or some other structure. The core algorithm doesn't change, only the implementation details of working with a specific structure.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: