【问题标题】:Linked List - Moving to the next node链表 - 移动到下一个节点
【发布时间】:2012-09-23 19:30:30
【问题描述】:

所以我在关于玩家围坐在桌子旁的编程任务中遇到了一些问题。该程序应该能够在刚刚转弯的玩家之后添加玩家。该作业应该向我们展示如何将数据添加到任何地方的链表中。所以当我使用 PLAY 命令时会出现我的问题。这应该允许单个玩家轮流。

例如,如果有玩家 A、B 和 C,并且执行 PLAY 命令,控制台会显示“玩家 A 轮到”。如果再次执行PLAY,会显示“玩家B轮到”。

我的代码允许列表中的第一个播放器播放,但不会移动到下一个节点/播放器。

void CircleList::play()
{
    LinkedListOfPlayersNode *p=(*pFront).pNext;
    if (p->pData!=NULL)
    {
        cout<<p->pData->getName()+" takes a turn\n";
        p-> pNext; //My attempt to move to the next node.
    }
    else
    {
        cout<<"There are no players. Please ADD a player.\n";
    }
}

所以这显然行不通。谁能向我解释一下我将如何转移到下一个玩家?

PS - 代码是 C++

【问题讨论】:

    标签: c++ data-structures linked-list


    【解决方案1】:

    您需要班级中的一名成员来保留最后一个轮到的玩家。

    class CircleList
    {
       //...
       PlayerNode* pLastPlayer;
    };
    

    最初,它被设置为pFront,每次调用play 时,您都会将其移动到下一个玩家。

    void CircleList::play()
    {
       //logic with pLastPlayer here
    
       //at the end move it
       pLastPlayer = pLastPlayer->next;
    }
    

    【讨论】:

      【解决方案2】:

      线

      p-> pNext; //My attempt to move to the next node.
      

      不会将任何东西移动到下一个节点。 您可能应该将p-&gt;pNext 存储/分配给某些东西。

      【讨论】:

        猜你喜欢
        • 2013-05-19
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 2015-08-16
        • 2014-08-06
        • 2011-08-26
        • 1970-01-01
        相关资源
        最近更新 更多