bool LinkedList<T>::insertAtPos(T data, int pos) { ListElement<T> *nodeToInsert = new ListElement<T>(data); if (nodeToInsert == nullptr) return false; // If list is empty and position is not the // begining of the list return false. if (isEmpty() && pos != 0) { return false; } ListElement<T> *curr = _head; // iterate until we are at pos - 1 while (curr != nullptr && pos != 1) { curr = curr->getNext(); pos--; } // insert the ListElement<T> if (pos == 1) { nodeToInsert->setNext(curr->getNext()); curr->setNext(nodeToInsert); return true; } return false; }
void List::addAfterN(int nPos, int inValue) { if (mLength >= nPos) { ListElement *additional = new ListElement(inValue); ListElement *current = mHead; for (int i = 1; i < nPos; i++) { current = current->getNext(); } additional->setNext(current->getNext()); current->setNext(additional); mLength++; } }
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T> &ListToBeAssigned) { std::cout << "Copy assignement operator called for @ " << this << std::endl; if (&ListToBeAssigned != this) { // Free The memory held by this deleteList(); ListElement<T> *copy = ListToBeAssigned._head; if (copy != nullptr) { _head = new ListElement<T>(copy->getValue()); ListElement<T> *curr = _head; copy = copy->getNext(); // copy List Elements Until we reach the end of the // List to be copied while (copy != nullptr) { curr->setNext(new ListElement<T>(copy->getValue())); copy = copy->getNext(); curr = curr->getNext(); } } } return *this; }
bool LinkedList<T>::deleteElement(ListElement<T> *deleteMe) { if (deleteMe == nullptr) return false; ListElement<T> *temp = _head; // check if the element is the head if (deleteMe == _head) { _head = temp->getNext(); delete temp; temp = nullptr; return true; } while (temp != nullptr) { if (temp->getNext() != nullptr && temp->getNext()->getValue() == deleteMe->getValue()) { temp->setNext(temp->getNext()->getNext()); delete deleteMe; deleteMe = nullptr; return true; } temp = temp->getNext(); } // deleteMe not found return false; }
bool LinkedList<T>::insertAtHead(T data) { ListElement<T> *newElement = new ListElement<T>(data); if (newElement == nullptr) return false; newElement->setNext(_head); _head = newElement; return true; }
void LinkedList<T>::reverseList() { if (isEmpty()) return; ListElement<T> *curr = _head; ListElement<T> *prev = curr->getNext(); ListElement<T> *next = nullptr; // Iterate until we reach to the end of the list while (prev != nullptr) { curr->setNext(next); next = curr; curr = prev; prev = prev->getNext(); } curr->setNext(next); // Set head to the current element _head = curr; }
void List::append(int inValue) { ListElement *updateHead = new ListElement(inValue); if (mLength == 0) { mHead = updateHead; mTail = updateHead; } else { updateHead->setNext(mHead); mHead = updateHead; } mLength++; }
void List::remove(string str) throw (string) { if (size >= 2) { if (head->getStr() == str) { ListElement *toDel = head; head = head->getNext(); head->setPrev(NULL); toDel->setNext(NULL); delete toDel; size--; return; } ListElement *temp = head->getNext(); while(temp->getNext() != NULL) { if (temp->getStr() == str) { temp->getNext()->setPrev(temp->getPrev()); temp->getPrev()->setNext(temp->getNext()); delete temp; size--; return; } temp = temp->getNext(); } if (tail->getStr() == str) { ListElement *toDel = tail; tail = tail->getPrev(); tail->setNext(NULL); size--; delete toDel; return; } throw string("No such word!"); } else if (size == 1) { if (head->getStr() == str) { delete head; size--; return; } throw string("No such word!"); } else throw string("List is Empty!"); }
LinkedList<T>::LinkedList(const LinkedList<T> & ListToBeCopied) { std::cout << "Copy constructor called for @ " << this << std::endl; ListElement<T> *copy = ListToBeCopied._head; _head = new ListElement<T>(copy->getValue()); copy = copy->getNext(); ListElement<T> *curr = _head; // copy List Elements Until we reach the end of the // List to be copied while (copy != nullptr) { curr->setNext(new ListElement<T>(copy->getValue())); copy = copy->getNext(); curr = curr->getNext(); } }
// Before revert List : x, y, z, w // After revert List : w, z, y, x void List::revert() { // "Write" List in the end ListElement *lastModified = mTail; ListElement *step; // for all elements befor tail for (int i = 1; i < mLength; i++) { step = mHead; // Gets pre lastModified value while (step->getNext() != lastModified) { step = step->getNext(); } lastModified->setNext(step); lastModified = step; } mHead = mTail; mTail = lastModified; mTail->setNext(nullptr); }
void List::revertNew() { ListElement *previous = new ListElement(mHead->getValue()); // Sets new Tail mTail = previous; ListElement *step = mHead->getNext(); while (step) { ListElement *clone = new ListElement(step->getValue()); clone->setNext(previous); previous = clone; step = step->getNext(); } // Head of reverted List now in previous while (mHead) { step = mHead; mHead = mHead->getNext(); delete step; } mHead = previous; }
bool LinkedList<T>::insertAtEnd(T data) { ListElement<T> *nodeToInsert = new ListElement<T>(data); if (nodeToInsert == nullptr) return false; // head is null if (isEmpty()) { _head = nodeToInsert; return true; } ListElement<T> *curr = _head; // iterate to the end while (curr != nullptr && curr->getNext() != nullptr) { curr = curr->getNext(); } curr->setNext(nodeToInsert); return true; }