Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help trying to implement a Linked-List program, unfortunately have been uns

ID: 3591117 • Letter: N

Question

Need help trying to implement a Linked-List program, unfortunately have been unsuccessful in creating a new C++ source file named lfgqueue.cpp that implements the LFGQueue class declared in lfgqueue.h such that the files provided compile into a program that runs with no failed tests in main.cpp. Only need help with implementing lfgqueue.cpp based off lfgqueue.h hint: use three linked-list-based queues, one for each player role. The files provided are not to be changed they are given just to implement the rest of the program, just lfgqueue.cpp file is to be created using Single Linked-List based data structures . Keep it simple and use very basic syntax, and statements. Thank You.

____________________________________________________________________

lfgqueue.h

__________________________________________________________________________________

player.h

______________________________________________________________________________________________________________________________________________

player.cpp

#include "player.h"

______________________________________________________________________________________

main.cpp

Explanation / Answer

I have answered this question earlier today and giving you the code for same. Hope it helps. If it helped, please do rate the answer. Thank you.

#include "lfgqueue.h"

LFGQueue::LFGQueue()
{
for(int i = 0; i < 3; i ++)
{
heads[i] = NULL;
tails[i] = NULL;
counts[i] = 0;
}
}

int LFGQueue::size()
{
  
return counts[0] + counts[1] + counts[2];
}

void LFGQueue::push_player(Player* p)
{
  
int index;
if(p->role() == Player::Defender)
{
index = 0;
}
else if(p->role() == Player::Hunter)
{
index = 1;
}
else
{
index = 2;
}
  
Node* n = new Node();
n->p = p;
n->next = NULL;
  
if(counts[index] == 0)
{
heads[index] = tails[index] = n;
}
else
{
tails[index]->next = n;
tails[index] = n;
}
counts[index]++;
}
Player* LFGQueue::front_player(Player::Role r)
{
int index;
if(r == Player::Defender)
{
index = 0;
}
else if(r == Player::Hunter)
{
index = 1;
}
else
{
index = 2;
}

if(heads[index] == NULL)
return NULL;
else
return heads[index]->p;
}

void LFGQueue::pop_player(Player::Role r)
{
int index;
if(r == Player::Defender)
{
index = 0;
}
else if(r == Player::Hunter)
{
index = 1;
}
else
{
index = 2;
}
  
if(counts[index] > 0)
{
heads[index] = heads[index]->next;
if(heads[index] == NULL)
tails[index] = NULL;
counts[index]--;
}
}
bool LFGQueue::front_group(Player** group)
{
group[0] = front_player(Player::Defender);
group[1] = front_player(Player::Hunter);
group[2] = front_player(Player::Bard);
  
if(group[0] == NULL || group[1] == NULL || group[2] == NULL) //if did not find one or more of the players
{
group[0] = group[1] = group[2] = NULL; //set all to NULL
return false;
}
else
return true;
}
void LFGQueue::pop_group()
{
pop_player(Player::Defender);
pop_player(Player::Hunter);
pop_player(Player::Bard);
}

output

Added 1000000 players to a queue in 0.317714 seconds.
Removed 167024 groups of players from this queue in 0.02336 seconds.
Assignment complete.