TurboSchach/MoveList.cpp

97 lines
2.3 KiB
C++
Raw Permalink Normal View History

#include "MoveList.h"
MoveList::MoveList() {
this->head->next = nullptr;
}
MoveList::~MoveList() {
Node *current = head;
std::cout << "Destructor: " << std::endl;
while (current != nullptr) {
Node *nextNode = current->next;
std::cout << " delete on " << current->data.first << " " << current->data.second << std::endl;
delete current;
current = nextNode;
}
}
MoveList &display() {
Node *currentNode = this->head;
while (currentNode->next != nullptr) {
currentNode = currentNode->next;
std::cout << "[" << currentNode->data.first << " " << currentNode->data.second << " | " << currentNode->next << ((currentNode->next != nullptr) ? "] ---> " : "] ");
}
std::cout << std::endl;
return *this;
}
MoveList &append(const std::pair<int, int> &data) {
if(this->head->next == nullptr) {
Node *node = new Node;
node->data = data;
this->head->next = node;
} else {
Node *newNode = new Node;
newNode->data = data;
newNode->next = nullptr;
Node *currentNode = this->head;
while (currentNode->next != nullptr) {
currentNode = currentNode->next;
}
currentNode->next = newNode;
}
return *this;
}
MoveList &insert(const std::pair<int, int> &data) {
Node *newNode = new Node;
newNode->data = data;
newNode->next = this->head->next;
this->head->next = newNode;
return *this;
}
Node* MoveList::search(const std::pair<int, int> &data) const {
Node *currentNode = this->head;
do {
if (currentNode->data == data) {
return currentNode;
}
} while(currentNode = currentNode->next);
return nullptr;
}
MoveList &remove(const std::pair<int, int> &data) {
Node *currentNode = this->head;
do {
if (currentNode->next->data == data) {
Node *temp = currentNode->next;
currentNode->next = currentNode->next->next;
delete temp;
break;
}
} while((currentNode = currentNode->next) && (currentNode->next != nullptr));
return *this;
}
void MoveList::removeTail(const std::pair<int, int> &data) {
Node *found = search(data);
if(found != nullptr) {
found->next = nullptr;
}
}