Implementation of a BballRoster

In the following sample solution involving the implementation of BballRoster, code has been provided by the expert in C++ Programming language. Here, our tutor  demonstrates to write an implementation of BballRoster using a doubly linked list. The names should be sorted alphabetically according to last name, then the first name.This example also showcases the implemention of some of the algorithms which operate on BballRoster.

SOLUTION: –

#include “BballRoster.h”

BballRoster::BballRoster(){

root= nullptr;

size = 0;

}

BballRoster::~BballRoster(){

deleteList();

}

BballRoster::BballRoster(const BballRoster& cp)

{

deleteList();

size = cp.size;

root = nullptr;

Node * tmp = cp.root;

while (tmp != NULL)

{

insert(tmp->first, tmp->last, tmp->val);

tmp = tmp->next;

}

}

BballRoster& BballRoster::operator=(const BballRoster& cp)

{

deleteList();

size = cp.size;

root = nullptr;

Node * tmp = cp.root;

while (tmp != NULL)

{

insert(tmp->first, tmp->last, tmp->val);

tmp = tmp->next;

}

return  *this;

}

bool BballRoster::rosterEmpty() const{

return size == 0;

}

int BballRoster::howManyPlayers() const{

return  size;

}

bool BballRoster::signPlayer(

const std::string& firstName,

const std::string& lastName,

const SomeType& value){

if (findPlayer(firstName, lastName) != NULL)

return  false;

insert(firstName, lastName, value);

return  true;

}

bool BballRoster::resignPlayer(const std::string& firstName,

const std::string& lastName, const SomeType& value)

{

Node * tmp = findPlayer(firstName , lastName);

if (tmp == nullptr)

return  false;

tmp->val = value;

return true;

}

bool BballRoster::signOrResign(const std::string& firstName, const std::string& lastName, const SomeType& value)

{

Node * tmp = findPlayer(firstName, lastName);

if (tmp == nullptr)

{

insert(firstName, lastName, value);

return  true;

}

tmp->val = value;

return  true;

}

bool BballRoster::renouncePlayer(const std::string& firstName, const std::string& lastName)

{

Node * tmp = findPlayer(firstName, lastName);

if (tmp == nullptr)

return  false;

deletePLayer(firstName, lastName);

size–;

return  true;

}

bool BballRoster::playerOnRoster(const std::string& firstName, const std::string& lastName) const

{

return findPlayer(firstName, lastName) != NULL;

}

bool BballRoster::lookupPlayer(const std::string& firstName,

const std::string& lastName, SomeType& value) const

{

Node * tmp = findPlayer(firstName, lastName);

if (tmp == nullptr)

return  false;

tmp->val = value;

return true;

}

bool BballRoster::choosePlayer(int i, std::string& firstName,

std::string& lastName, SomeType& value) const

{

// If linked list is empty

if (root == NULL)

return false;

// Store head node

Node* temp = root;

// If head needs to be selected

if (i == 0)

{

firstName = root->first;

lastName = root->last;

value = root->val;

return true;

}

// Find previous node of the node to be selected

for (int i1 = 0; temp != NULL && i1<i – 1; i1++)

temp = temp->next;

// If position is more than number of nodes

if (temp == NULL || temp->next == NULL)

return false;

firstName = temp->next->first;

lastName = temp->next->last;

value = temp->next->val;

return  true;

}

void BballRoster::swapRoster(BballRoster& other)

{

deleteList();

size = other.size;

root = nullptr;

Node * tmp = other.root;

while (tmp != NULL)

{

insert(tmp->first, tmp->last, tmp->val);

tmp = tmp->next;

}

}

 

void BballRoster::dump() const

{

cerr << “Size:” << size << endl;

Node * tmp = root;

while(tmp != NULL)

{

cerr << tmp->first << “\t” << tmp->last << “\t” << tmp->val << endl;

tmp = tmp->next;

}

}

bool BballRoster::insert(const std::string& firstName,

const std::string& lastName, const SomeType& value)

{

size++;

Node * temp;

if (root == NULL)

{

/*List is empty*/

root = new Node;  /*malloc the root */

(root)->next = NULL; /*mark the next as null*/

root->first = firstName;

root->last = lastName;

root->val = value;

}

else {

/*list is not empty */

if(cmpNames(firstName , lastName , root->first , root->last) == -1)

{

/* the given token is equal to or smaller than the root token*/

/*push the given token to the beging*/

temp = new Node;

temp->first = firstName;

temp->last = lastName;

temp->val = value;

temp->next = root;

root = temp;

}

else {

temp = root;

while (true)

{

if (temp->next == NULL)

{/* this means that the given token is smaller than every thing in the list

add it to the end

*/

temp->next =new Node;

temp->next->first = firstName;

temp->next->last = lastName;

temp->next->val = value;

temp->next->next = NULL;

return true;

}

if (cmpNames(firstName, lastName ,

temp->next-> first , temp->next->last) == -1)

{

/*

the given token is smaller than some string in the middle

1-break the connection

2-add a node in the middle

3-connect it to the next node

*/

Node* tmp =new Node;

tmp->first = firstName;

tmp->last = lastName;

tmp->val = value;

tmp->next = temp->next;

temp->next = tmp;

return  true;

}

temp = temp->next;

}

}

}

return true;

}

void BballRoster::deletePLayer(const std::string& firstName, const std::string& lastName)

{

// Store head node

Node* temp = root, *prev =NULL;

 

// If head node itself holds the key to be deleted

if (temp != NULL && cmpNames(firstName, lastName,

temp->first, temp->last) == 0)

{

root = temp->next;   // Changed head

delete(temp);               // free old head

return;

}

// Search for the key to be deleted, keep track of the

// previous node as we need to change ‘prev->next’

while (temp != NULL &&  cmpNames(firstName, lastName,

temp->first, temp->last) != 0)

{

prev = temp;

temp = temp->next;

}

// If key was not present in linked list

if (temp == NULL) return;

// Unlink the node from linked list

prev->next = temp->next;

delete(temp);  // Free memory

}

BballRoster::Node * BballRoster::findPlayer(const std::string& firstName, const std::string& lastName) const

{

Node *tmp = root;

while(tmp!= nullptr)

{

if (firstName == tmp->first && lastName == tmp->last)

return  tmp;

tmp = tmp->next;

}

return  NULL;

}

void BballRoster::deleteList()

{

Node* current = root;

Node* next;

 

while (current != NULL)

{

next = current->next;

delete(current);

current = next;

}

}

int BballRoster::cmpNames(const string &first1, const string &last1, const string &first2, const string & last2)

{

if(last1 == last2)

{

if (first1 == first2)

return 0;

return  first1 > first2 ? 1 : -1;

}

return  last1 > last2 ? 1 : -1;

}

#include <iostream>

#include <string>

using namespace std;

typedef std::string SomeType;

class BballRoster

{

public:

BballRoster(); // Create an empty BballRoster list

~BballRoster();

BballRoster(const  BballRoster & cp);

BballRoster &operator =(const BballRoster & cp);

bool rosterEmpty() const; // Return true if the BballRoster list

// is empty, otherwise false.

int howManyPlayers() const; // Return the number of players in

// the BballRoster list.

bool signPlayer(const std::string& firstName, const std::string&

lastName, const SomeType& value);

// If the full name (both the first and last name) is not equal

// to any full name currently in the list then add it and return

// true. Elements should be added according to their last name.

// Elements with the same last name should be added according to

// their first names. Otherwise, make no change to the list and

// return false (indicating that the name is already in the

// list).

bool resignPlayer(const std::string& firstName, const std::string&

lastName, const SomeType& value);

// If the full name is equal to a full name currently in the

// list, then make that full name no longer map to the value it

// currently maps to, but instead map to the value of the third

// parameter; return true in this case. Otherwise, make no

// change to the list and return false.

bool signOrResign(const std::string& firstName, const std::string&

lastName, const SomeType& value);

// If full name is equal to a name currently in the list, then

// make that full name no longer map to the value it currently

// maps to, but instead map to the value of the third parameter;

// return true in this case. If the full name is not equal to

// any full name currently in the list then add it and return

// true. In fact, this function always returns true.

bool renouncePlayer(const std::string& firstName, const

std::string& lastName);

// If the full name is equal to a full name currently in the

// list, remove the full name and value from the list and return

// true. Otherwise, make no change to the list and return

// false.

bool playerOnRoster(const std::string& firstName, const

std::string& lastName) const;

// Return true if the full name is equal to a full name

// currently in the list, otherwise false.

bool lookupPlayer(const std::string& firstName, const std::string&

lastName, SomeType& value) const;

// If the full name is equal to a full name currently in the

// list, set value to the value in the list that that full name

// maps to, and return true. Otherwise, make no change to the

// value parameter of this function and return false.

bool choosePlayer(int i, std::string& firstName, std::string&

lastName, SomeType& value) const;

// If 0 <= i < size(), copy into firstName, lastName and value

// parameters the corresponding information of the element at

// position i in the list and return true. Otherwise, leave the

// parameters unchanged and return false. (See below for details

// about this function.)

void swapRoster(BballRoster& other);

// Exchange the contents of this list with the other one.

void dump()const;

private:

// A linked list node

struct Node

{

string first, last;

SomeType val;

struct Node *next;

};

bool  insert(const std::string& firstName, const std::string&

lastName, const SomeType& value);

void deletePLayer(const std::string& firstName, const

std::string& lastName);

Node* findPlayer(const std::string& firstName, const

std::string& lastName) const;

void deleteList();

int cmpNames(const string &first1, const string &last1, const string &first2, const string &last2);

Node * root;

int size;

};