I have a linked list representing a deck of 52 cards. I now need to deal the cards and have a linked list for each hand. Any suggestions on how to do that would be appreciated.
I have a linked list representing a deck of 52 cards. I now need to deal the cards and have a linked list for each hand. Any suggestions on how to do that would be appreciated.
Why a linked-list? That's not the most suitable data structure for that.
Presumably you want to shuffle the deck first?
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
The deck is already shuffled and in a linked list. From that i need to deal it out to two hands which need to also be represented as linked lists
All you should need to do is unlink one card from the deck, and then add it into the hand, then the same thing for the second card... and so on until you deal the whole hand. A specific answer would depend on what you actually wrote, but conceptually you are only taking from one list and inserting into another. There's lots of code to show you how that is supposed to work on the Web.
If you implement methods for push and pop, then you simply need to call Pop 5 times, followed each time by a Push into the list for the player's hand.
How about showing some code?
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
any idea why this doesn't work?Code:void deal(c *p, c *p1, c *p2) { int i=1; while (i<=52) { if (i%2==0) { p1=p; p->next=NULL; } else { p2=p; p->next=NULL; } i++; } }
Well say p1 points to a node. You assign p to it. Then you assign p->next to NULL; Then say it's p2's turn... same thing, you assign p to it. Then set p->next to NULL. You will just keep doing that 52 times. There is no progressive movement in your list.
Think about this:
You've already dealt me 4 cards in a game of 5 card stud. (Go ahead and make a contrived list. My list is K-9-9-9-? You need to insert another node at ? ... Assuming you've done this before at all, you can easily adapt to that. Once you figure that small puzzle out, you can start from nothing just to make sure it works. Add my first card, then the second, and so on... Once you finish that, you've built up to the required knowledge to better solution that deals many cards to many people. Since you've already mastered dealing my hand, all you need to do is construct a proper loop.
1. For all cards in the hand (5 in 5 card stud):
2. For each of the players at the table:
3. Add a card to the current player's hand list.
Take a look at references that already exist.
Sorry, just realised I've been assuming you wanted to deal 5 cards to each hand, but from the looks of this you want to deal ALL of the cards to two hands.
If that is really what you want to do, then the easy way is to just cut the list in half.
I think the better question is, why do you think it would work?
It looks like you don't understand pointers. For starters, p never changes, so every time but the first that it sets p->next to NULL, it's not going to have any effect. That's just a small part of what is wrong though.
Do you really need to alternate between dealing cards to each hand? I mean if the order is randomised already, then you wont be able to tell if you just take N cards for the first hand, and then N for the second.
Why don't you have a go at writing (and then using) the Push and Pop methods I suggested? That would turn this into a very easy problem.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Assuming that a deck or hand of cards is implemented as some sort of list or linked list, wouldn't mrm want to be using pop_front() and push_back() type (fifo) functions?
Could you show us the code for your linked list structure(s) and any code you have for appending / retrieving nodes to / from a linked list?
Last edited by rcgldr; 04-23-2013 at 06:06 PM.
Assuming a singly-linked-list, one could push on the back, but I certainly wouldn't in this case...
Pushing at the front can be done easily with only a head pointer, and doesn't even need any flow control logic at all.
Pushing at the back requires a tail pointer as well, which is more complicated as you potentially have to update the head and tail pointers, and not every linked-list implementation has a tail pointer.
When there isn't any real need to maintain ordering, using the list as a stack is preferable.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
True. I wasn't sure if the original poster would understand that push and pop would be using a list as a stack.
On a side note, it's possible to create a list with only a tail pointer, with the tail element pointing to the head element, but it's also needlessly complicated.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Yep, push and pop is much easier to do and makes much more sense. Thanks for the help.
... and that code is pretty embarrassing after I figured out how a single linked list works