简体   繁体   English

循环遍历链接列表

[英]Circular Traversal of a Linked List

So I'm having some trouble with one of my functions. 因此,我的一项功能遇到了麻烦。 The program (in C++) plays a game, and there are so many players sitting at the table. 该程序(使用C ++)玩游戏,并且有很多玩家坐在桌子旁。 Every time my play function is called, it should display to the console a player in the game. 每次调用我的游戏功能时,它都应该向控制台显示游戏中的玩家。 Every time it is called it should display the players in sequence. 每次调用它时,都应按顺序显示玩家。 After it hits the last player it will start back at the beginning of the list/table. 击中最后一个玩家后,它将在列表/表的开头重新开始。 void CircleList::play() 无效CircleList :: play()

LinkedListOfPlayersNode *p=(*pFront).pNext;
if (p->pData!=NULL)
{
    cout<<p->pData->getName()+" takes a turn\n";


    pLastPlayer = pLastPlayer->pNext;
}
else
{
    cout<<"There are no players. Please ADD a player.\n";
}

}

So lets say that that we add A, B, and C. When you use the PLAY command, C should take a turn, then B then A. As of right now with the above code, it will display that C Takes a Turn, however, it crashes right after that. 假设我们添加了A,B和C。当您使用PLAY命令时,C应该先转弯,然后B再转A。到现在为止,上面的代码将显示C进行转弯,但是,此后它立即崩溃。 So what is wrong with my code? 那么我的代码有什么问题呢? Is there a better way to write this? 有没有更好的方法来写这个?

I'm pretty sure you want that traversal to look something like this: 我很确定您希望遍历看起来像这样:

LinkedListOfPlayersNode *p = pNextPlayer ? pNextPlayer : pFront;

if (p && p->pData) // not sure why pData is also dynamic. look into that.
{
    cout<<p->pData->getName()+" takes a turn\n";
    pNextPlayer = p->pNext;
}
else
{
    cout<<"There are no players. Please ADD a player.\n";
}

Each time a player's turn comes up, they take it, and the next player is that player's next pointer. 每次出现一个玩家的回合时,他们都会接住,而下一个玩家就是该玩家的下一个指针。 When your last player at the table takes a turn, p->pNext will be null, and the next invoke will reset to the head of the list. 当您在桌上的最后一个玩家转弯时,p-> pNext将为null,并且下一次调用将重置为列表的开头。

At least thats where I think you're trying to get. 至少那是我认为您想要得到的。 Prime this with either pNextPlayer being set to pFront or NULL ; 通过将pNextPlayer设置为pFrontNULL进行pFront makes no difference. 没有区别。

You are in C++, you are better off with a std::list than writing your own implementation. 您使用的是C ++,使用std :: list比编写自己的实现更好。

Something's bugging me: Why are you starting your loop with "p = (*pFront).pNext" instead of starting it straight with "p = pFront"? 某事困扰着我:为什么要使用“ p =(* pFront).pNext”开始循环,而不是直接使用“ p = pFront”开始循环?

How are you adding your nodes to your list? 您如何将节点添加到列表中? You should be echo-ing "A takes a turn" before "C takes a turn". 您应该在“ C转向”之前回显“ A转向”。

If you are indeed adding to the front of your list (and your list is doubly-linked), you should call pPrevious instead of pNext. 如果确实要添加到列表的最前面(并且列表是双向链接的),则应调用pPrevious而不是pNext。

Lets say you initially define pnext = NULL in while creating nodes. 假设您在创建节点时最初定义pnext = NULL

So according to your code; 因此,根据您的代码;

if (p->pData!=NULL)
{
    cout<<p->pData->getName()+" takes a turn\n";
    pLastPlayer = pLastPlayer->pNext;
}
else
{
    cout<<"There are no players. Please ADD a player.\n";
}

There are 2 scenarios according to my assumptions: 1. p and pLastPlayer are the same ... so after the list is over. 根据我的假设,有2种情况:1. ppLastPlayer相同...因此列表结束后。 p will be equal to NULL , your code will crash as it tries to de-reference p->pData . p将等于NULL ,您的代码将在尝试取消引用p->pData崩溃。 2. p and pLastPlayer are different ... So after the list pLastPlayer ends, the code will crash at pLastPlayer = pLastPlayer -> pNext(); 2. ppLastPlayer是不同的...因此,列表pLastPlayer结束后,代码将在pLastPlayer = pLastPlayer -> pNext();处崩溃pLastPlayer = pLastPlayer -> pNext(); as you are probably de-referencing garbage or NULL value; 因为您可能正在取消引用垃圾或NULL值;

You need to check for NULL pointers at some point, either in p -> pData != NULL or pLastPlayer = pLastPlayer -> pNext(); 您需要在某些时候检查NULL指针,在p -> pData != NULLpLastPlayer = pLastPlayer -> pNext();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM