//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(); } }
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; }
/** * 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++; } }
bool DLList::Exists(int value){ DLNode* temp = head; while (temp != NULL) { if (temp->GetContents() == value) { return true; } temp = temp->GetNext(); } return false; }
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; }
void DLList::pushFront(int newContents){ DLNode* node = new DLNode(newContents); if (head == NULL){ tail = node; } else{ node->setNextNode(head); } head = node; size++; }
/** * 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; } }
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::pushBack(int newContents){ if(head == NULL){ pushFront(newContents); } else{ DLNode* node = new DLNode(newContents); DLNode* b = head; while(b->getNextNode() != NULL){ b = b->getNextNode(); } b->setNextNode(node); size++; } }
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(); } }
/** * 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); } } }
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; }
/** * 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; }
//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; }
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;} }
//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::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--; } }
void DLList::PushBack(int add) { if (head == NULL || tail == NULL) { DLNode* temp = new DLNode; temp->SetContents(add); head = temp; tail = temp; size++; } else { DLNode* temp = new DLNode; temp->SetContents(add); tail->SetNext(temp); temp->SetPrevious(tail); tail = temp; size++; } }
string DLList::ToStringBackwards(){ if (head == NULL){ cerr << "List Empty" << endl; return ""; } stringstream ss; DLNode* temp = tail; while(temp != NULL) { ss << temp->GetContents(); if (temp != head){ ss << ", "; } temp = temp->GetPrevious(); } 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(); }
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--; } }
void DLList::popBack(){ if(head != NULL) { if(head->getNextNode() == NULL){ popFront(); } else{ DLNode* b = head; DLNode* previous = b; while(b->getNextNode() != NULL){ previous = b; b = b->getNextNode(); } delete b; previous->setNextNode(NULL); size--; } } }
string DLList::ToStringForwards(){ if (head == NULL){ cerr << "List Empty" << endl; return ""; } stringstream ss; DLNode* temp = head; while(temp != NULL) { ss << temp->GetContents(); if (temp != tail){ ss << ", "; } temp = temp->GetNext(); } return ss.str(); }
/** * 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; }
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; } }