【发布时间】:2017-11-09 00:29:07
【问题描述】:
我在创建两个结构时遇到问题,一个称为 Bag,另一个是 Card。这是我目前所拥有的:
struct Card
{
char suit;
int value;
};
struct BagNode
{
Card* Card;
BagNode* next;
};
void fillWithCards(BagNode *&head, BagNode *&tail, BagNode *&temp)
{
char suits [] = {'s', 'h', 'd', 'c'};
int val [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
for (int x = 0; x < 4 ; x++)
{
for (int y =1 ; y <= 13 ; y++)
{
BagNode *now = new BagNode;
now -> Card -> suit = suits [x];
now -> Card -> value = val [y];
if (*&head == NULL && *&tail == NULL && *&temp == NULL)
{
head = now;
tail = now;
temp = now;
}
else if (*&tail != NULL && *&temp != NULL)
{
tail -> next = now;
temp -> next = now;
tail = now;
temp = now;
}
else
{
tail -> next = NULL;
}
}
}
}
问题是当我编译和运行程序时它会崩溃。我感觉这个问题与Card* Card;有关
是的,我知道我可以在 Card 中拥有所有内容,即*next,我不需要BagNode。然而,在询问我的教授时,他说我们需要有两个结构,一个用于Bag,另一个用于Card。
【问题讨论】:
-
这是learn how to debug your programs的最佳时机。
-
为什么会有多个 Bags 的链表,每个 Bag 只有一张卡片?那个包应该代表什么?一个包含多张卡片的包对我来说更有意义。
标签: c++ struct linked-list