2

我是 C++ 新手,我正在处理链接列表。但是,当我正在编写链接列表类时,我对此感到困惑。我正在尝试为足球联赛编写代码。我试图将每支球队保存在一个链接列表中,并且由于每支球队都有球员,我想将他们也存储在一个链接列表中。所以链表类应该有一个成员指向一个与我正在编写的类具有相同属性的链表。有可能吗?

4

3 回答 3

3

是的。球队列表的一个节点将包含一个球员列表。如果你不能使用std::list

struct Player
{
    Player* nextPlayer;
};

struct Team
{
    Team* nextTeam;
    Player* firstPlayer;
};

我很确定虽然列表不是对此建模的最佳结构。

于 2012-05-05T10:20:38.867 回答
1

我更喜欢以下结构,因为您可能不想总是在列表中使用球员和球队:

struct PlayerNode
{
    Player* item;
    PlayerNode* nextNode;
};

struct TeamNode
{
    Team* item;
    TeamNode* nextNode;
};

struct Team {
    // ...
    PlayerNode* firstPlayer;
};

This allows you to use a team or a player as a standalone object (not connected to other teams/players). But within a team you can also access its players, which I guess is desirable.

于 2012-05-05T11:14:34.510 回答
0

If you are using C++ then use STL instead. I am explaining one example with vector

Suppose this is the heirarchy You have 2 League League1, League2 each have 2 teams Team1, Team2 each team has 10 players P1, P2

All the players of one team can be stored in a vector named temp_team All temp_team can be pushed to vector named temp_league All temp_league vector can be pushed to vector named footBall, League, watever.

于 2015-07-29T06:19:21.037 回答