Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BballRoster.h"
- #include <iostream>
- using namespace std;
- BballRoster::BballRoster()
- {
- head = nullptr;
- tail = nullptr;
- }
- BballRoster::~BballRoster()
- {
- if(rosterEmpty()==true)
- {
- return;
- }
- else if (howManyPlayers() == 1)
- {
- delete head;
- return;
- }
- else {
- Node* p;
- p = head;
- while (p->next != nullptr)
- {
- p = p->next;
- delete p->prev;
- }
- delete tail;
- }
- }
- BballRoster::BballRoster(const BballRoster& copy)
- {
- }
- const BballRoster& BballRoster::operator=(const BballRoster& copy)
- {
- int size = howManyPlayers();
- for (int i = 0; i < size; i++)
- {
- string firstname;
- string lastname;
- string value;
- choosePlayer(0, firstname, lastname, value);
- renouncePlayer(firstname, lastname);
- }
- int size2 = copy.howManyPlayers();
- Node* p = copy.head;
- while (p != nullptr)
- {
- signPlayer(p->firstname, p->lastname, p->value);
- p = p->next;
- }
- return *this;
- }
- bool BballRoster::rosterEmpty() const
- {
- if (howManyPlayers() == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- int BballRoster::howManyPlayers() const
- {
- Node* p;
- p = head;
- int i = 0;
- while (p != nullptr)
- {
- p = p->next;
- i++;
- }
- return i;
- }
- bool BballRoster::signPlayer(const std::string& firstName, const std::string&lastName, const SomeType& value)
- {
- if (playerOnRoster(firstName, lastName) == true)
- {
- return false;
- }
- //put data into node
- Node* p;
- p = new Node;
- p->firstname = firstName;
- p->lastname = lastName;
- p->value = value;
- //create unsorted linked list
- if (rosterEmpty() == true)
- {
- head = p;
- tail = p;
- head->next = nullptr;
- p->prev = nullptr;
- }
- else {
- tail->next = p;
- p->prev = tail;
- tail = p;
- tail->next = nullptr;
- }
- //sort linked list
- Node* n;
- Node* k;
- string temp;
- string temp2;
- string temp3;
- for (n = head->next; n != nullptr; n = n->next)
- {
- for (k = head; k != nullptr; k = k->next)
- {
- if (n->lastname < k->lastname)
- {
- temp = n->lastname;
- temp2 = n->firstname;
- temp3 = n->value;
- n->lastname = k->lastname;
- n->firstname = k->firstname;
- n->value = k->value;
- k->lastname = temp;
- k->firstname = temp2;
- k->value = temp3;
- }
- else if (n->lastname == k->lastname)
- {
- if (n->firstname < k->firstname)
- {
- temp = n->lastname;
- temp2 = n->firstname;
- temp3 = n->value;
- n->lastname = k->lastname;
- n->firstname = k->firstname;
- n->value = k->value;
- k->lastname = temp;
- k->firstname = temp2;
- k->value = temp3;
- }
- }
- }
- }
- return true;
- }
- bool BballRoster::resignPlayer(const std::string& firstName, const std::string&lastName, const SomeType& value)
- {
- Node* p;
- p = head;
- if ((rosterEmpty() == true))
- {
- return false;
- }
- while (p != nullptr)
- {
- if (p->firstname == firstName && p->lastname == lastName)
- {
- p->value = value;
- return true;
- }
- p = p->next;
- }
- return false;
- }
- bool BballRoster::signOrResign(const std::string& firstName, const std::string&lastName, const SomeType& value)
- {
- Node* p;
- p = head;
- if ((rosterEmpty() == true))
- {
- signPlayer(firstName, lastName, value);
- return true;
- }
- while (p != nullptr)
- {
- if (p->firstname == firstName && p->lastname == lastName)
- {
- p->value = value;
- return true;
- }
- p = p->next;
- }
- signPlayer(firstName, lastName, value);
- return true;
- }
- bool BballRoster::renouncePlayer(const std::string& firstName, const std::string& lastName)
- {
- Node* p;
- p = head;
- if ((howManyPlayers() == 0))
- {
- return false;
- }
- int count = 0;
- while (p != nullptr)
- {
- count++;
- if (count > 1 && count < howManyPlayers())
- {
- if (p->firstname == firstName && p->lastname == lastName)
- {
- Node* temp = p->next;
- Node* temp2 = p->prev;
- temp->prev = temp2;
- temp2->next = temp;
- delete p;
- return true;
- }
- }
- else if (count == howManyPlayers() && howManyPlayers() > 1)
- {
- if (p->firstname == firstName && p->lastname == lastName)
- {
- Node* temp = p->prev;
- temp->next = nullptr;
- delete(p);
- return true;
- }
- }
- else if (count == 1 && howManyPlayers() > 1)
- {
- if (p->firstname == firstName && p->lastname == lastName)
- {
- head = head->next;
- head->prev = nullptr;
- delete p;
- return true;
- }
- }
- else if (howManyPlayers() == 1)
- {
- if (p->firstname == firstName && p->lastname == lastName)
- {
- head = nullptr;
- tail = nullptr;
- delete p;
- return true;
- }
- }
- p = p->next;
- }
- return false;
- }
- bool BballRoster::playerOnRoster(const std::string& firstName, const std::string& lastName) const
- {
- if (howManyPlayers() == 0)
- {
- return false;
- }
- Node* p = head;
- while (p != nullptr)
- {
- if (p->firstname == firstName && p->lastname == lastName)
- {
- return true;
- }
- p = p->next;
- }
- return false;
- }
- bool BballRoster::lookupPlayer(const std::string& firstName, const std::string&lastName, SomeType& value) const
- {
- if (howManyPlayers() == 0)
- {
- return false;
- }
- Node* p = head;
- while (p != nullptr)
- {
- if (p->firstname == firstName && p->lastname == lastName)
- {
- value = p->value;
- return true;
- }
- p = p->next;
- }
- return false;
- }
- bool BballRoster::choosePlayer(int i, std::string& firstName, std::string&lastName, SomeType& value) const
- {
- if (i < howManyPlayers() && i>=0)
- {
- Node* p = head;
- for (int j=0; j < i; j++)
- {
- p = p->next;
- }
- firstName = p->firstname;
- lastName = p->lastname;
- value = p->value;
- }
- else {
- return false;
- }
- return true;
- }
- void BballRoster::swapRoster(BballRoster& other)
- {
- if (howManyPlayers() == 0 && other.howManyPlayers() == 0) // check if both are empty do nothing
- {
- return;
- }
- Node* store = head; // store head
- head = other.head; //swap head with other head
- other.head = store;
- store = tail; // store tail
- tail = other.tail; // swap tail with other tail
- other.tail = store;
- }
- void BballRoster::printRoster()
- {
- Node* p = new Node;
- p = head;
- if (howManyPlayers() == 0)
- {
- return;
- }
- while (p != nullptr)
- {
- cout << p->lastname << " " << p->firstname << " " << p->value << endl;
- p = p->next;
- }
- }
- bool joinRosters(const BballRoster & bbOne, const BballRoster & bbTwo, BballRoster & bbJoined)
- {
- int sizebb1 = bbOne.howManyPlayers();
- int sizebb2 = bbTwo.howManyPlayers();
- string firstname;
- string lastname;
- string value;
- string firstname2;
- string lastname2;
- string value2;
- int j = 0;
- int count = 0;
- if (sizebb1 == 0)
- {
- for (int i = 0; i < sizebb2; i++)
- {
- bbTwo.choosePlayer(i, firstname, lastname, value);
- bbJoined.signPlayer(firstname, lastname, value);
- }
- return true;
- }
- if (sizebb2 == 0)
- {
- for (int i = 0; i < sizebb1; i++)
- {
- bbOne.choosePlayer(i, firstname, lastname, value);
- bbJoined.signPlayer(firstname, lastname, value);
- }
- return true;
- }
- if (sizebb1 == 0 && sizebb2 == 0)
- {
- return true;
- }
- for (int i = 0; i < sizebb1; i++)
- {
- bbOne.choosePlayer(i, firstname, lastname, value);
- j = 0;
- for (j = 0; j < sizebb2; j++)
- {
- bbTwo.choosePlayer(j, firstname2, lastname2, value2);
- if (firstname == firstname2 && lastname == lastname2 && value != value2)
- {
- count++;
- j = sizebb2;
- }
- else
- {
- bbJoined.signPlayer(firstname, lastname, value);
- }
- }
- }
- for (int i = 0; i < sizebb2; i++)
- {
- bbTwo.choosePlayer(i, firstname, lastname, value);
- j = 0;
- for (j = 0; j < sizebb1; j++)
- {
- bbOne.choosePlayer(j, firstname2, lastname2, value2);
- if (firstname == firstname2 && lastname == lastname2 && value != value2)
- {
- count++;
- j = sizebb1;
- }
- else
- {
- bbJoined.signPlayer(firstname, lastname, value);
- }
- }
- }
- if (count == 0)
- {
- return true;
- }
- else {
- return false;
- }
- }
- void checkRoster(const std::string& fsearch, const std::string& lsearch, const BballRoster& bbOne, BballRoster& bbResult) {
- string firstname;
- string lastname;
- string value;
- int sizebb = bbOne.howManyPlayers();
- string fs = fsearch;
- string ls = lsearch;
- bool option1 = false;
- bool option2 = false;
- bool option3 = false;
- if (sizebb == 0)
- {
- return;
- }
- if (fs == "*" && ls == "*")
- {
- option3 = true;
- }
- else if (fs == "*")
- {
- option1 = true;
- }
- else if (ls == "*")
- {
- option2 = true;
- }
- for (int i = 0; i < sizebb; i++)
- {
- bbOne.choosePlayer(i, firstname, lastname, value);
- if (option3 == true)
- {
- bbResult.signPlayer(firstname, lastname, value);
- }
- else if (option2 == true)
- {
- if (fsearch == firstname)
- {
- bbResult.signPlayer(firstname, lastname, value);
- }
- }
- else if (option1 == true)
- {
- if (lsearch == lastname)
- {
- bbResult.signPlayer(firstname, lastname, value);
- }
- }
- else
- {
- if (fsearch == firstname && lsearch == lastname)
- {
- bbResult.signPlayer(firstname, lastname, value);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement