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; }
/** * return true if target is in list, else return false */ bool DLList::get(int target) const { /** * Checks if the head is null and returns false if it is */ if(head_ == NULL) { return false; /** * Checks if the contents of head_ are equal to the parameter and returns true if it is */ } else if(head_->getContents() == target) { return true; /** * Checks if the contents of tail_ are equal to the parameter and returns true if it is */ } else if(tail_->getContents() == target) { return true; /** * checks if the list contains only two nodes, if it does not then * it checks for the specified parameter, otherwise it returns false */ } else if(head_->getNext() != tail_) { DLNode* iterator = head_->getNext(); /** * Loops through the node list checking the contents of the current * node to the parameter and if the next node is the tail, if neither * statement is true then it iterates to the next node */ while(iterator->getContents() != target && iterator->getNext() != tail_) { iterator = iterator->getNext(); } /** * Checks if the contents of the current node are equal to the parameter * and returns true, if not then returns false */ if(iterator->getContents() == target) { return true; } else { return false; } } else { return false; } }
/** * returns true if parameter is in the list * else returns false */ bool DLList::get(string contents) { DLNode* temp = head_; while (temp != tail_) { if (temp->getContents() == contents) return true; if (temp->getContents() != contents) { temp = temp->getNext(); } } return false; }
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; } }
string DLList::getStop(int iterator) { // getStop will loop through the linked list until ITERATIONS is met. // It will stop and return where it stopped and do nothing. // Create TEMP pointer at HEAD DLNode* temp = head_node; cout << "ITERATOR STARTING; Count = " << iterator << endl; for(int i=1;i<iterator;i++) { sleep(1); temp = temp->getNext(); } cout << "Stopped on: " << temp->getContents() << endl; sleep(2); return temp->getContents(); }
void DLList::debugger() { DLNode* temp = head_node; for(int i=0;i<node_count;i++) { cout << " Node #" << i << " returns as " << temp->getContents() << endl; temp = temp->getNext(); } }
//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); }
bool DLList::get(int target) const{ DLNode* node = head; bool isPresent = false; while(node->getNextNode() != NULL){ if(node->getContents() == target){ isPresent = true; } node->setNextNode(node->getNextNode()); } return isPresent; }
//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;} }
//display the contents of each node in the list, formatted per the program specification ("NUM1,NUM2,NUM3,...,NUMX"), to the output stream out ostream& operator<< (ostream& out, const DLList src) { DLNode* temp = src.head; stringstream returned; while(temp != NULL) { returned << temp->getContents(); if(temp->getNext() != NULL) returned << ", "; temp=temp->getNext(); } out << returned.str(); return out; }
bool DLList::get(int target) const { DLNode* nodeMark = head; bool targetFound = false; for (unsigned int i = 0; i < count; i++) { if (nodeMark->getContents() == target){ targetFound = true; } if (nodeMark->getNext() != NULL) { nodeMark = nodeMark->getNext(); } } nodeMark = NULL; return targetFound; }
string DLList::toString() const { if (head == NULL) return string(""); else { stringstream ss; for (DLNode* i = head; i != NULL; i = i->getNext()) { ss << i->getContents(); if ((i->getNext()) != NULL) ss << ','; } return ss.str(); } }
/* DLNode* temp = contents.getFront(); while (temp != NULL) { out << temp->getContents(); temp = temp->getNext(); }*/ string DLList::ToString() const { if (head_ == NULL) return ""; stringstream ss; DLNode* temp = head_; while (temp != NULL) { ss << temp->getContents(); if (temp->getNext() != NULL) ss << ", "; temp = temp->getNext(); } return ss.str(); }
bool DLList::removeFirst(int target){ if(get(target) == true){ DLNode* b = head; DLNode* previous = b; while(b->getContents() != target){ previous = b; b = b->getNextNode(); } if(b->getNextNode() != NULL){ previous->setNextNode(b->getNextNode()); delete b; } else{ delete b; previous->setNextNode(NULL); } size--; } }
string DLList::toString() const { string listContents; stringstream ss; if (head != NULL){ ss << head->getContents(); if (head->getNext() != NULL){ DLNode* currentNode = head->getNext(); for (unsigned int i = 1; i < count; i++){ ss << ","; ss << currentNode->getContents(); if (currentNode->getNext() != NULL){ currentNode = currentNode->getNext(); } } } } ss >> listContents; return listContents; }
//return true if target is in list, else return false bool DLList::get (int target) const { if(head == NULL) { return false; } else { DLNode* temp = head; while(temp->getNext() != NULL) { if(temp->getContents() == target || temp->getNext()->getContents() == target) { return true; } temp = temp->getNext(); } 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; } }
/** * remove all instances of DLNode containing target; do nothing if target * is not found */ bool DLList::removeAll(int target) { /** * Checks if the head is null and returns false if it is */ if(head_ == NULL) { return false; /** * Checks if the contents of head_ are equal to the parameter, then * calls the popBack() function and returns true if it is */ } else if(head_->getContents() == target) { popBack(); /** * Checks if the contents of tail_ are equal to the parameter, then * calls the popBack() function and returns true if it is */ } else if(tail_->getContents() == target) { popBack(); /** * checks if the list contains only two nodes, if it does not then * it checks for the specified parameter, otherwise it returns false */ } else if(head_->getNext() != tail_) { DLNode* iterator = head_->getNext(); DLNode* temp_node = head_; /** * Loops through the node list checking the contents of the current * node to the parameter and if the next node is the tail, if neither * statement is true then it iterates to the next node */ while(iterator->getContents() != target && iterator->getNext() != tail_) { temp_node = iterator; iterator = iterator->getNext(); } /** * Checks if the contents of the current node are equal to the parameter, * if not then returns false */ if(iterator->getContents() == target) { /** * Sets the placeholder node to the node that the iterator is pointing * to then deletes the iterator node, reduces the size variable * then returns true */ temp_node->setNext(iterator->getNext()); delete iterator; iterator = NULL; count_ = count_ - 1; } else { return false; } } else { return false; } return true; }