void DLList::insert(int newValue) { if (head == NULL) pushFront(newValue); else if (newValue <= head->getContents()) pushFront(newValue); else if (newValue >= tail->getContents()) pushBack(newValue); else { DLNode* spot = head; while (spot->getNext() != NULL && newValue > spot->getContents()) spot = spot->getNext(); if (spot->getNext() == NULL && newValue >= spot->getContents()) pushBack(newValue); else { DLNode* newNode = new DLNode(newValue); DLNode* trailer = spot->getPrevious(); trailer->setNext(newNode); newNode->setNext(spot); newNode->setPrevious(trailer); spot->setPrevious(newNode); size++; } } }
//create new DLNode with newContents and insert in ascending (based on newContents) order void DLList::insert (int newContents) { if(head == NULL || newContents < head->getContents()) { pushFront(newContents); return; } DLNode* temp = head; while(temp->getNext() != NULL) { DLNode* nextTemp = temp->getNext(); if(nextTemp->getContents() > newContents) { DLNode* inserted = new DLNode(newContents); inserted->setPrevious(temp); inserted->setNext(nextTemp); temp->setNext(inserted); nextTemp->setPrevious(inserted); size++; return; } temp = temp->getNext(); } pushBack(newContents); }
/** * Insert function that creates a new node and * inserts it in an appropriate location in the list. *@param string contents the contents of the new node. */ void DLList::insert(string contents) { //Checks if head is null or contents is < head_->getContents(); if (head_ == NULL) { pushFront(contents); } else if ( tail_->getContents() < contents) { // cout << " got it: " << contents << endl; // cout << ToString(); pushBack(contents); } else { //creates iterator and newnode DLNode* iterator; iterator = head_; while (iterator->getNext() != tail_ && contents > iterator->getNext()->getContents()) { iterator = iterator->getNext(); } if (iterator != tail_) { DLNode *newNode = new DLNode(contents); newNode->setNext(iterator->getNext()); iterator->setNext(newNode); size_ +=1; } else { pushBack(contents); } } }
void DLList::insert(int newContents) { if (head == NULL) { pushFront(newContents); } else { count++; DLNode* newNode = new DLNode(newContents); if (head->getContents() > newContents){ DLNode* oldHead = head; head = newNode; head->setNext(oldHead); oldHead->setPrevious(head); oldHead = NULL; } else { if (head->getNext() == NULL) { head->setNext(newNode); newNode->setPrevious(head); tail = newNode; } else { DLNode* nodeMark = head->getNext(); DLNode* lagMark = NULL; for (unsigned int i = 1; i < count; i++) { if (nodeMark->getContents() < newContents && nodeMark->getNext() != NULL) { lagMark = nodeMark; nodeMark = nodeMark->getNext(); } } if ((lagMark == NULL) && (nodeMark->getNext() != NULL || nodeMark->getContents() > newContents)) { head->setNext(newNode); newNode->setNext(nodeMark); nodeMark->setPrevious(newNode); newNode->setPrevious(head); } else if (nodeMark->getNext() == NULL && newContents > nodeMark->getContents()) { nodeMark->setNext(newNode); newNode->setPrevious(nodeMark); tail = newNode; } else { lagMark->setNext(newNode); newNode->setNext(nodeMark); nodeMark->setPrevious(newNode); newNode->setPrevious(lagMark); } nodeMark = NULL; lagMark = NULL; } } newNode = NULL; } }
bool DLList::removeFirst(int target) { DLNode* toRemove = NULL; DLNode* nodeMark = head; DLNode* lagMark = NULL; for (unsigned int i = 0; i < count; i++){ if (nodeMark->getContents() != target && nodeMark->getNext() != NULL){ lagMark = nodeMark; nodeMark = nodeMark->getNext(); } else if (nodeMark->getContents() == target){ toRemove = nodeMark; } } if (toRemove == NULL) { return false; } if (lagMark == NULL){ popFront(); } else if (toRemove->getNext() == NULL) { popBack(); } else { count--; nodeMark = nodeMark->getNext(); lagMark->setNext(nodeMark); nodeMark->setPrevious(lagMark); delete toRemove; } toRemove = NULL; nodeMark = NULL; lagMark = NULL; return true; }
/** * create new DLNode with newContents and insert in ascending (based on * newContents) order */ void DLList::insert(int newContents) { /** * Checks to see if head_ is null or if the contents of head_ are greater * then the parameter being inserted. If it is then calls the * pushFront() function, otherwise continues to next check */ if(head_ == NULL || head_->getContents() > newContents) { pushFront(newContents); /** * Checks if the contents of tail_ are less then the parameter being * inserted. If so, calls pushBack() function, otherwise continues * to next check */ } else if (tail_->getContents() < newContents) { pushBack(newContents); /** * Inserts the parameter into the correct location in the list */ } else { DLNode* iterator; iterator = head_; /** * Checks whether the contents of the next node is less then the parameter * and that the next node is not the tail. If they are not, then it * advances the iterator to the next node in the list */ while(newContents > iterator->getNext()->getContents() && iterator->getNext() != tail_) { iterator = iterator->getNext(); } /** * Checks if the iterator is located at the tail */ if(iterator != tail_) { /** * If the iterator is not located at the tail, it creates a new node * and inserts it at the iterators current location, then increases * the size variable by 1 */ DLNode* new_node = new DLNode(newContents); new_node->setNext(iterator->getNext()); //new_node->setPrevious(iterator); iterator->setNext(new_node); count_ = count_ + 1; } else { /** * If the iterator is located at the tail, then calls the InsertTail() * function */ pushBack(newContents); } } }
/** * creates a new dynamic DLNode with the contents of * the parameter and attaches as the new head of the list * @param string contents */ void DLList::pushFront(string contents) { DLNode* temp = new DLNode(contents); temp->setNext(head_); temp->setPrevious(tail_); head_ = temp; size_ += 1; if (tail_ == NULL) tail_ = head_; }
void DLList::pushFront(int value) { DLNode* newHead = new DLNode(value); if (head == NULL) { head = newHead; tail = head; size++; } else { newHead->setNext(head); head->setPrevious(newHead); head = newHead; size++; } }
void DLList::popBack() { // Remove current head node; do nothing if list is empty if (tail_node != NULL) { DLNode* temp = head_node; DLNode* previous = temp; while(temp->getNext() != tail_node) { previous = temp; temp = temp->getNext(); } previous->setNext(head_node); head_node->setPrevious(previous); delete tail_node; tail_node = previous; temp = NULL; node_count--; } }
/** * Removes all occurences of the parameter * does nothing if not in list */ bool DLList::removeAll(string contents) { //returns false for empty list if (head_ == NULL) { return false; // uses popBack() if tail == contents } else if (tail_->getContents() == contents) { popBack(); return true; } else { //creating pointers DLNode* iterator = head_, *temp = head_->getNext(); //traverses to find match for contents while (contents != temp->getContents() && temp->getNext() != NULL) { temp = temp->getNext(); iterator = iterator->getNext(); } //returns false if contents are not in list if (temp == tail_ && temp->getContents() != contents) { return false; } else //checks if temp is the node to delete then executes while (temp != NULL) { if(temp->getContents() == contents) { iterator->setNext(temp->getNext()); delete temp; size_ = size_ - 1; temp = NULL; } } } return true; }
/** * removes the tail node from the list, * or does nothing if the list is empty */ void DLList::popBack() { if (head_ != NULL) { if (head_ == tail_) { popFront(); } else { DLNode* temp = head_; while (temp->getNext() != tail_) temp = temp->getNext(); temp->setNext(NULL); delete tail_; tail_ = temp; size_ -= 1; } } }
void DLList::popBack() { if (head != NULL){ if (head->getNext() == NULL) { popFront(); } else { DLNode* i = head; DLNode* j = NULL; while (i->getNext() != NULL) { j = i; i = i->getNext(); } if (i != NULL) { delete i; j->setNext(NULL); tail = j; } count--; } } }
bool DLList::removeFirst(string target) { if(head_node == NULL) { return false; } DLNode* temp = head_node; DLNode* previous = temp; if(head_node->getContents() == target) { cout << "Eliminated " << target << endl; popFront(); return true; } if(tail_node->getContents() == target) { cout << "Eliminated " << target << endl; popBack(); return true; } // There is no reason why this loop would be endless, so there won't be a catch. // The incoming TARGET string is only called when getStop finishes. while(temp->getContents() != target) { // Get next node and continue, and keep previous 1 behind. previous = temp; temp = temp->getNext(); } if(temp->getContents() == target) { cout << "Eliminated " << target << endl; // Delete Node // Setting previous' node to the next node of temp previous->setNext(temp->getNext()); // Setting temp's next node's PREVIOUS to temp's previous NODE temp->setPrevious(temp->getPrevious()); delete temp; temp = NULL; node_count--; return true; } else { cout << "RemoveFirst returned false entirely (bad?)"<<endl;return false;} }
void DLList::insert(string newContents) { // Inserts New Node with newContents and attach in order string placeholder = newContents; char check_place = newContents[0]; char check_head; char check_tail; if(head_node != NULL) { string head_contents = head_node->getContents(); check_head = head_contents[0]; string tail_contents = tail_node->getContents(); check_tail = tail_contents[0]; } if(head_node == NULL || ((check_place <= check_head) && (head_node != NULL))) { cout << " Creating new head node:" << newContents << endl; pushFront(newContents); } else if(tail_node == NULL || ((check_place >= check_tail) && (tail_node != NULL))) { cout << " Creating new tail node:" << newContents << endl; pushBack(newContents); } else { DLNode* temp = head_node; DLNode* previous = temp; while(temp->getContents() < newContents) { previous = temp; temp = temp->getNext(); } DLNode* new_node = new DLNode(newContents); new_node->setNext(temp); previous->setNext(new_node); node_count = node_count + 1; } }
void DLList::pushBack(int newContents) { if (head == NULL) { pushFront(newContents); } else { DLNode* newTail = new DLNode(newContents); if (head->getNext() == NULL) { head->setNext(newTail); newTail->setPrevious(head); } else { DLNode* oldTail = head->getNext(); for (unsigned int i = 1; i < count; i++){ if (oldTail-> getNext() != NULL){ oldTail = oldTail->getNext(); } } oldTail->setNext(newTail); newTail->setPrevious(oldTail); oldTail = NULL; } tail = newTail; newTail = NULL; count++; } }