Computer Science Canada Randomnizing a LinkedList |
Author: | a22asin [ 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:
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). |
Author: | Tony [ 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. |
Author: | Krocker [ 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? |
Author: | Tony [ 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. |