/** * 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 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; }
//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); }
/** * 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); } } }
/** * 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; } }
//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(); } }
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; } }
//removes node from head bool DLList::removeAll(int target) { if (head == NULL) return false; else { DLNode* trailer = NULL; DLNode* spot = head; while(spot != NULL &&spot -> getContents() != target) { trailer = spot; spot = spot -> getNext(); } if(spot == NULL) return false; else if(spot == head) { popFront(); return true; } else { trailer -> setNext(spot->getNext()); delete spot; count--; return true; } } }
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(); } }
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--; } }
/* 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(); }
DLNode* DLList::searchIndex(int targetValue) const { DLNode* nodeInQuestion = head; for (unsigned int i = 0; i < size; i++) { if (nodeInQuestion -> getContents() == targetValue) { return nodeInQuestion; } else nodeInQuestion = nodeInQuestion->getNext(); } return 0; }
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; }
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--; } } }
//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; } }
//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;} }
/** * 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; }
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::clear() { // Clear ALL nodes, reset count to zero, set head and tail to NULL while(head_node != NULL) { DLNode* temp = head_node; head_node = temp->getNext(); delete temp; temp = NULL; node_count--; } tail_node = NULL; head_node = NULL; if(node_count != 0) { cout << "LINK LIST CLEAR FUNCTION FAILED!!" << endl; }else{ cout << "LINKED LIST CLEARED." << endl;} }
void DLList::popFront() { // Remove current head node; do nothing if list is empty if (head_node != NULL){ // Create Temp DLNode* temp; // Point Temp to Head temp = head_node; // Head to Next head_node = temp->getNext(); // Tail to new Head tail_node->setNext(head_node); // Delete delete temp; // NULL temp = NULL; node_count--; } }
/** * 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; } } }
/** * 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 node where the tail is void DLList::popBack() { if(head != NULL) { DLNode* i = head; DLNode* trailer = NULL; while(i->getNext() != NULL) { trailer = i; i = i -> getNext(); } delete i; count--; if(trailer == NULL) { head = NULL; } else { trailer -> setNext(NULL); } } }
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++; } }
/** * 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; }