I need to make a poker game for school in c++. I have made a class Card and Deck. I need to make a doubly linked list of all the cards, and every card has a suit and a rank (value). How can I attach 2 attributes (suit and rank) to a Card in a doubly linked list.
Asked
Active
Viewed 490 times
0
-
3Is re-creating your own doubly-linked-list part of the assignment, or can you use `std::list` which does all of this for you already? – Cody Gray - on strike Dec 19 '11 at 12:19
-
What exactly is your problem? What have you tried, and where are you stuck? – Björn Pollex Dec 19 '11 at 12:19
-
_"I need to make a poker game for school"_. Haha I thought you were asking for the best secret place at school, to throw a poker party. – ali_bahoo Dec 19 '11 at 13:03
2 Answers
1
The suit and the rank are properties of the card, and have nothing to do with the linked list. As such, these properties are best encapsulated into the Card
class.
If you already do this, and something remains unclear, please expand your question.

NPE
- 486,780
- 108
- 951
- 1,012
1
A double linked list is a structure (a struct or a class) with pointers to the previous and the next link. In addition to these pointers you can add arbitrary data, which can be considered the payload. There you can put any data you want. Here is an example:
class Card {
public:
// Constructor
Card(int rank, int suit, Card* prev=NULL)
{
if (prev)
{
m_prev = prev;
prev->m_next = this;
}
m_prev = prev;
m_rank = rank;
m_suit = suit;
}
// Accessors
int Rank() { return m_rank; }
int Suit() { return m_suit; }
Card* Prev() { return m_prev; }
Card* Next() { return m_next; }
private:
int m_rank, m_suit;
Card *m_prev, *m_next;
}

Dov Grobgeld
- 4,783
- 1
- 25
- 36
-
Hmmmm ... Personally I would not include the next and previous pointers in the card class. Couple of reasons, it mixes up the listing responsibility with the card model, and it has no obvious single use... it could link to the next highest value card, it could link to the next card in a hand, it could link to the next numerical value etc... If you have `List
hand, deck, suit;` there is no room for misinterpretation. – Dennis Dec 19 '11 at 13:35 -
I absolutely agree. But I didn't want to mix in the concepts of templates because of the newbie flavor of the question. Once he understands the underlying concept of a double linked list and its payload, I would indeed use the abstraction that you suggest. – Dov Grobgeld Dec 19 '11 at 13:59