void StringLinkedList::remove(int index) { if (empty()){ throw EmptyException("Empty list"); } if (index < 0 || index > n - 1){ throw OutOfBoundException("Index is out of bounds"); } else{ StringNode* remove = head, *prev = head; int pos = 0; while (pos != index && remove->next != NULL){ prev = remove; remove = remove->next; pos++; } if (index == 0){ removeFront(); } else if(index == n) { removeBack(); } else{ prev->next = remove->next; delete remove; n--; } } }
const std::string& StringLinkedList::front() const{ if (empty()){ //Empty list check throw EmptyException("Empty list"); } else{ //Prints the front element return head->elem; } }
Node<T>* BSTree<T>::search(const T &k) const { if (root_ == nullptr) { throw EmptyException(); } else { Node<T>* node = root_; return search(node, k); } }
const std::string& StringLinkedList::back() const { if (empty()){ //Empty list check throw EmptyException("Empty list"); } else{ StringNode* back = head; while (back->next != NULL){ //Transverse through the list back = back->next; } return back->elem; //Returns the last element } }
void BSTree<T>::print(std::ostream &os/* = std::cout*/) const { if (root_ == nullptr) { throw EmptyException(); } else { Node<T>* node = root_; { print(node, os); } } }
const std::string& StringLinkedList::get(int index) const { if (empty()){ //empty list throw EmptyException("Empty list"); } if (index > n - 1 || index < 0){ //If the index is bigger than the size of the linked list or if the index is smaller than 0 then it is out of range throw OutOfBoundException("Index is Out of Bounds"); } StringNode* v = head; int pos = 0; //Look for the element while (pos != index){ v = v->next; pos++; } return v->elem; }
void StringLinkedList::removeBack() { if (empty()){ //Empty list condition throw EmptyException("Empty List"); } if (head->next == NULL){ //Only one element in the list delete head; head = NULL; n--; } else{ //Multiple elemetn in the string StringNode* node = head->next, *prev = head; //sets a node equal to the next element, and a prev equal to the first elemetn while (node->next != NULL){ //tranverse through the list prev = node; node = node->next; } prev->next = NULL; //Set the element before node's pointer to the next elemetn equal to null delete node; //delete the pointer } }
void BSTree<T>::deletewithchild(const T &k) { if (root == nullptr) { throw EmptyException(); } else if (search(k) == nullptr) { //throw "Empty"; } else { Node<T>* node = search(k); if (node->left != nullptr) { remove(node->left->key); } if (node->right != nullptr) { remove(node->right->key); } if (node->parent != nullptr) { if (node->parent->left == node) { node->parent->left = nullptr; } else { node->parent->right = nullptr; } } this->_size -= 1; delete node; } }
void BSTree<T>::remove(const T &k) { if (root_ == nullptr) { throw EmptyException(); } else if (search(k) == nullptr) { //throw "Empty"; } else { Node<T>* node = search(k); if (node->left == nullptr && node->right == nullptr) { if (node->parent != nullptr) { if (node->parent->left == node) { node->parent->left = nullptr; } else { node->parent->right = nullptr; } this->size_ -= 1; delete node; } else { delete this->root_; this->root_ = nullptr; this->size_ = 0; } } else if (node->left == nullptr || node->right == nullptr) { if (node->parent != nullptr) { if (node->left == nullptr) { if (node->parent->left == node) { node->parent->left = node->right; } else { node->parent->right = node->right; } node->right->parent = node->parent; } else { if (node->parent->left == node) { node->parent->left = node->left; } else { node->parent->right = node->left; } node->left->parent = node->parent; } this->size_ -= 1; delete node; } else { if (node->left == nullptr) { root_ = node->right; node->right->parent = nullptr; } else { root_ = node->left; node->left->parent = nullptr; } this->size_ -= 1; delete node; } } else { Node<T>* pos = next(node); node->key = pos->key; if (pos->parent->left == pos) { pos->parent->left = pos->right; if (pos->right != nullptr) { pos->right->parent = pos->parent; } } else { pos->parent->right = pos->right; if (pos->right != nullptr) { pos->right->parent = pos->parent; } } this->size_ -= 1; delete pos; } } }