int CTECArray<Type>::nextIndexOf(int startingIndex, Type searchValue) { assert(this->size > 0); assert(startingIndex >= 0 && startingIndex < this->size); int indexNotFound = -1; ArrayNode<Type>* current = head; for(int index = 0; index < startingIndex; index++) { current = current->getNext(); } for(int index = startingIndex; index < this->size; index++) { if(current->getValue == searchValue) { return index; } else { current = current->getNext(); } } return indexNotFound; }
CTECList<Type>::~CTECList() { // Start at head // Loop over nodes // update position // Delete Final //Assign default ArrayNode<Type>* current = head; while (current->getNext() != nullptr) { ArrayNode<Type>* temp = current; current = current->getNext(); head = current; delete temp; } delete head; head = nullptr; end = nullptr; size = 0; }
Type CTECList<Type>::removeEnd() { assert(size > 0); ArrayNode<Type>* previous; ArrayNode<Type>* toDelete; ArrayNode<Type>* current = head; Type thing; if (current->getNext() == nullptr) { toDelete = current; thing = toDelete->getValue(); delete toDelete; end = nullptr; } else { while(current->getNext() != nullptr) { previous = current; current = current->getNext(); } toDelete = current; thing = toDelete->getValue(); delete toDelete; previous->setNext() = nullptr; end = previous; } this->calculateSize(); return thing; }
void CTECList<Type>::addAtIndex(int index, const Type& value) { ArrayNode<Type>* newNode = new ArrayNode<Type>(value); ArrayNode<Type>* current = head; ArrayNode<Type>* previous = current; assert(index <= size && index >= 0); if (index == 0) { addFront(value); } else if(index == size) { addEnd(value); } else { for(int i = 0; i < index; i++) { previous = current; current = current->getNext(); } previous->setNext(newNode); newNode->setNext(current); } calculateSize(); }
Type CTECList<Type>::getAtIndex(int index) { assert(index > size || index < 0); ArrayNode<Type>* current = head; for (int i = 0; i < index - 1; i++) { current = current->getNext(); } return current->getValue(); }
void CTECList<Type>::addToEnd(const Type& value) { ArrayNode<Type>* newNode = new ArrayNode<Type>(value); ArrayNode<Type>* current = head; if (current->getNext() == nullptr) { current->setNext(newNode); newNode->setNext(nullptr); end = newNode; } else { while(current->getNext() != nullptr) { current = current->getNext(); } current->setNext(newNode); newNode->setNext(nullptr); end = newNode; } calculateSize(); }
void CTECList<Type>::calculateSize() { assert(size >= 0); ArrayNode<Type>* counterPointer = head; int count = 0; if (counterPointer == nullptr) { this->size = 0; return; } else { count++; while(counterPointer->getNext() != nullptr) { counterPointer = counterPointer->getNext(); count++; } this->size = count; } }
void CTECArray<Type>::set(int position,const Type& value) { assert(position < size && position >= 0); ArrayNode<Type> *current = head; for (int spot = 0; spot <= position; spot++) { if (spot != position) { current = current->getNext(); } else { current->setValue(value); } } }
Type CTECList<Type>::removeAtIndex(int index) { ArrayNode<Type>* previous; ArrayNode<Type>* toDelete; ArrayNode<Type>* newNext; ArrayNode<Type>* current = head; Type thing; assert(this->size > 0); assert(index > 0 || index <= size); if (index == 0) { thing = removeFront(); } else if (index == size - 1) { thing = removeEnd(); } else { for (int i = 0; i < index; i++) { if (i == index - 1) { previous = current; } else if (i == index) { toDelete = current; thing = toDelete->getValue(); newNext = current->getNext(); delete toDelete; previous->setNext(newNext); } current = current->next(); } } this->calculateSize(); return thing; }
int CTECArray<Type>::indexOf(Type searchValue) { assert(this->size > 0); int indexNotFound = -1; ArrayNode<Type>* current = head; for(int index = 0; index < this->size; index++) { if(current->getValue() == searchValue) { return index; } else { current = current->getNext(); } } return indexNotFound; }