简体   繁体   中英

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. 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()

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. 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.

At least thats where I think you're trying to get. Prime this with either pNextPlayer being set to pFront or NULL ; makes no difference.

You are in C++, you are better off with a std::list than writing your own implementation.

Something's bugging me: Why are you starting your loop with "p = (*pFront).pNext" instead of starting it straight with "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".

If you are indeed adding to the front of your list (and your list is doubly-linked), you should call pPrevious instead of pNext.

Lets say you initially define pnext = NULL in while creating nodes.

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. p will be equal to NULL , your code will crash as it tries to de-reference p->pData . 2. p and pLastPlayer are different ... So after the list pLastPlayer ends, the code will crash at pLastPlayer = pLastPlayer -> pNext(); as you are probably de-referencing garbage or NULL value;

You need to check for NULL pointers at some point, either in p -> pData != NULL or pLastPlayer = pLastPlayer -> pNext();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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