//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); }
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++; } } }
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; }
/** * 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::pushBack(int value) { if (head == NULL) { pushFront(value); } else { DLNode* newTail = new DLNode(value); newTail->setPrevious(tail); tail->setNext(newTail); tail = newTail; size++; } }
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++; } }
//create new DLNode with newContents and attach at tail void DLList::pushBack (int newContents) { DLNode* temp = new DLNode(newContents); temp->setPrevious(tail); tail = temp; size++; if(tail->getPrevious() == NULL) { head = tail; } else { tail->getPrevious()->setNext(tail); } }
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; } }
//remove the first instance of a DLNode containing target; do nothing if target is not found bool DLList::removeFirst (int target) { if(head == NULL) { return false; } else if(head->getContents()==target) { popFront(); return true; } else { DLNode* temp = head; while(temp->getNext() != NULL) { DLNode* nextTemp = temp->getNext(); if(temp->getContents() == target) { temp->getPrevious()->setNext(nextTemp); nextTemp->setPrevious(temp->getPrevious()); delete temp; size--; if(size == 1) { head = tail; } return true; } temp = temp->getNext(); } if(temp->getContents() == target) { popBack(); return true; } return false; } }
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;} }