Need help on linked lists

This is a discussion on Need help on linked lists within the C Programming forums, part of the General Programming Boards category; I have a linked list representing a deck of 52 cards. I now need to deal the cards and have ...

  1. #1
    MRM
    MRM is offline
    Registered User
    Join Date
    Apr 2013
    Posts
    4

    Need help on linked lists

    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.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    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"

  3. #3
    MRM
    MRM is offline
    Registered User
    Join Date
    Apr 2013
    Posts
    4
    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

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    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.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    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"

  6. #6
    MRM
    MRM is offline
    Registered User
    Join Date
    Apr 2013
    Posts
    4
    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++;
        }
    
    }
    any idea why this doesn't work?

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    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.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    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.

    Quote Originally Posted by MRM View Post
    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++;
        }
    
    }
    any idea why this doesn't work?
    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"

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,255
    Quote Originally Posted by iMalc View Post
    Why don't you have a go at writing (and then using) the Push and Pop methods I suggested?
    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?

    Quote Originally Posted by MRM View Post
    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.
    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.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by rcgldr View Post
    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?
    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"

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,255
    Quote Originally Posted by iMalc View Post
    When there isn't any real need to maintain ordering, using the list as a stack is preferable.
    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.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by rcgldr View Post
    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.
    Oh, a circular list. Yeah I used that in production code on an embedded device once.
    It wasn't that bad really.
    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"

  13. #13
    MRM
    MRM is offline
    Registered User
    Join Date
    Apr 2013
    Posts
    4
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 12:07 PM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 01:49 AM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM
  5. Linked List of Linked lists Revisited.
    By Qui in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2004, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21