bool LinkedList<T>::search(T value) const { Node<T>* temp = m_front; bool isFound = false; if (m_size == 0) { return(isFound); } else { while(temp->getNext() != nullptr) { if(temp->getValue() == value) { return(true); } temp = temp->getNext(); if(temp->getValue() == value) { isFound = true; return(isFound); } } } return(isFound); }
bool LinkedList<T>::search(T value) const { Node<T>* temp = m_front; bool isFound = false; //nothing to do if list is empty if(size() == 0){ return isFound; } //otherwise, traverse else{ //while temp isn't pointing at the last node while(temp->getNext() != nullptr){ //check node by node if value is there and switch flag if(temp->getValue() == value){ isFound = true; } //move to next node temp = temp->getNext(); } //check last node if(temp->getValue() == value){ isFound = true; } } return(isFound); }
bool Hash::remove(int x) { Node* temp = m_front; int index = 0; for(int i = 0; i < m_size; i++) { temp = m_front; index = hash(x, i); //std::cout << "Val: " << i << " hash: " << index << std::endl; for (int k = 0; k < index; k++) temp = temp->getNext(); if ((temp->getValue() == -1) && (temp->getFlag() == false)) { std::cout << "No value to remove" << i << std::endl; return true; } else if (temp->getValue() == x) { temp->setValue(-1); temp->setFlag(true); active--; return true; } //else keep going while hash = -1 with true flag }//for each entry in the hash table (mod makes this cycle through every element) return false; }//remove
bool LinkedList<T>::search(T value) const { Node<T>* temp = m_front; bool isFound = false; /** TODO Fix this method */ if (temp != nullptr) //check to see if there is an existing Node { //loop until the end of the list is reached do { if (temp->getValue() == value) { //found value isFound = true; break; } else temp = temp->getNext(); //get next Node } while (temp->getNext() != nullptr); if (temp->getValue() == value) //found value isFound = true; else //value is not in the list isFound = false; } //list is empty else isFound = false; return(isFound); }
bool LinkedList<T>::search(T value) const { Node<T>* temp = m_front; bool isFound = false; if(isEmpty()) { return false; } while(temp->getNext() != nullptr) { if(temp->getValue() == value) { return true; } temp = temp->getNext(); if(temp->getValue() == value) { isFound = true; break; } } return(isFound); }
bool Graph::insertNode(int v) { if (find(v) != NULL) { cerr << "\nNode with value " << v << " already present in the graph" << endl; return false; } else { Node *toBeInserted = new Node(v); Node *temp = getRootNode(); while (temp != NULL) { if (temp->getValue() < v ) { temp = temp->getRightNode(); } else if (temp->getValue() > v ) { temp = temp->getLeftNode(); } else { temp = temp->getMidNode(); } } if (v < temp->val) temp->setLeftNode(toBeInserted); if (v > temp->val) temp->setRightNode(toBeinserted); if (v == temp->val) temp->setMidNode(toBeInserted); //FIXME: ++depth; return true; } }
void main() { // Int int n; Node<int> *node; Node<int> *nd; SingleLinkedList<int> lList; char yn; do { cout << "Enter a number to insert in the Single Linked List: "; cin >> n; node = new Node<int>(n); lList.AddNode(node); cout << "Continue (Y/N)?:"; cin >> yn; } while (yn=='Y' || yn=='y'); for (nd=lList.getFirstNode() ; nd ; nd=lList.getNextNode(nd)) { cout << nd->getValue() << ", "; } cout << "Delete node with value = "; cin >> n; int del=lList.deleteNode(n); if (!del) cout << "Node not found" << endl; else cout << "Node was deleted" << endl; for (nd=lList.getFirstNode() ; nd ; nd=lList.getNextNode(nd)) { cout << nd->getValue() << ", "; } cout << endl; // Double double d; Node<double> *nodeD; Node<double> *ndD; SingleLinkedList<double> lListD; do { cout << "Enter a number to insert in the Single Linked List: "; cin >> d; nodeD = new Node<double>(d); lListD.AddNode(nodeD); cout << "Continue (Y/N)?:"; cin >> yn; } while (yn=='Y' || yn=='y'); for (ndD=lListD.getFirstNode() ; ndD ; ndD=lListD.getNextNode(ndD)) { cout << ndD->getValue() << ", "; } }
void List::printList() { Node* node = getRoot()->getNext(); cout << getRoot()->getValue(); while(node != getRoot()) { cout << " -> " << node->getValue(); node = node->getNext(); } cout << " -> " << node->getValue() << endl; }
int main() { Node<string>* ns = new Node<string>(string("hello world!")); Node<int>* ni = new Node<int>(5); Node<double>* nd = new Node<double>(3.1459); std::cout << ns->getValue()<< std::endl; std::cout << ni->getValue()<< std::endl; std::cout << nd->getValue()<< std::endl; return 0; }
void LinkedList<T>::InsertionSort() { /* head - pointer la inceputul listei; ramane constant si de referinta */ Node<T> *head = this->pFirst; /* aux - pointer auxiliar utilizat la parcurgere */ Node<T> *aux = this->pFirst; /* sortedList - lista sortata */ LinkedList<T> sortedList; /* currentValue - variabila auxiliara in care vom retine valoarea elementului curent din lista*/ T currentValue; /* Se adauga by default primul element din lista de procesat in viitoarea lista sortata*/ sortedList.addLast( currentValue ); currentValue = aux->getValue(); sortedList.addLast( currentValue ); aux = aux->getNext(); /* Se parcurge lista, atat timp cat exista elemente */ while ( aux != NULL ) { currentValue = aux->getValue(); /* auxSort - pointer de parcurgere a listei sortate */ Node<T> *auxSort = sortedList.front(); /* Se parcurge lista deja sortata si i se cauta locul de insertie a valorii curente */ while ( auxSort != NULL ) { /* Daca ajunge la ultimul element din lista, e clar ca nu mai avem nimic in dreapta, deci inseram direct la coada si iesim */ if ( auxSort->getNext() == NULL ) { sortedList.addLast( currentValue ); break; } /* now - valoarea elementului curent */ T now = auxSort->getValue(); /* next - valoarea elementului urmator */ T next = auxSort->getNext()->getValue(); /* Daca locul valorii este intre cele doua elemente, inseram valoarea acolosa si ne oprim din cautare*/ if ( now <= currentValue && currentValue <= next ) { sortedList.insertElement( currentValue, auxSort ); break; } /* Daca nu ... mergem la urmatorul nod */ auxSort = auxSort->getNext(); } aux = aux->getNext(); } sortedList.removeFirst(); /* Se modifica proprietatile listei curente cu cele ale listei sortate */ *this = sortedList; }
int pop(){ if (stackTop == NULL) { cout << "Stack is empty." << endl; throw; } else{ Node* temp = stackTop; stackTop = stackTop->getNext(); cout << "Poping element =" << temp->getValue(); return temp->getValue(); } }
bool isPalindrome(const LinkedList& list) { LinkedList reversed = reverseLinkedList(list); Node* reversedNode = reversed.getHead(); Node* node = list.getHead(); while(node != nullptr && reversedNode != nullptr) { if(node->getValue() != reversedNode->getValue()) return false; node = node->getNext(); reversedNode = reversedNode->getNext(); } return true; }
int main() { Node* n = new Node(27); cout << n->getValue() << endl; Node* n2 = new Node(39); n->setNext(*n2); cout << n->getNext() << endl; //Node* address = n->getNext(); //cout << address->getValue() << endl; cout << (n->getNext())->getValue() << endl; //cout << "Size is " << n ->size() << endl; //cout << "Is empty: " <<n->isEmpty() << endl; /*for (int i=0; i<10; i++) { //cout << i << endl; q->enqueue(i); } cout << "Size is " << q->size() << endl; cout << "Is empty: " <<q->isEmpty() << endl; for (int i=0; i<10; ++i) { cout << q->dequeue() << endl; } cout << "Is empty: " <<q->isEmpty() << endl; delete q; //next line is 'segmentation fault, proves it's deleted //cout << "Size is " << q->size() << endl; */ return 0; }
bool remove( int val ) { if ( root == NULL ) { return false; } else { if ( root->getValue() == val ) { Node fake(0); fake.setLeft( root ); Node* removed = root->remove( val, &fake ); root = fake.getLeft(); if ( removed != NULL ) { delete removed; return true; } else { return false; } } else { Node* removed = root->remove( val, NULL ); if ( removed == NULL ) { return false; } else { delete removed; return true; } } } }
void printStatus() { cout << "STATUS: "; util->printTime(); Node<Cashier*> *node = this->cashierList->getFirst(); int i = 1; while(node != NULL) { Cashier *c = node->getValue(); cout << "C" << i++ << "[" << c->busyTime << "] -> "; Node<Customer*> *node_cust = c->customersLine->getFirst(); while(node_cust != NULL) { Customer * cust = node_cust->getValue(); cout << "(" << cust->getWaitingTime(util->time) << ") "; node_cust = node_cust->getNext(); } cout << endl; node = node->getNext(); } }
void printList(){ Node *head = this->root->getNext(); while(head){ std::cout << head->getValue() << std::endl; head = head->getNext(); } }
const char *iTunesNode::getKeyValue(const char *keyName) { Node *node = getNode(); if (node == NULL) return NULL; Node *keyValueNode = NULL; int childNodeCnt = node->getNNodes(); for (int n=0; n<childNodeCnt; n++) { Node *childNode = node->getNode(n); if (childNode == NULL) continue; if (node->isName(keyName) == false) continue; if ((childNodeCnt-1) <= n) continue; keyValueNode = node->getNode(n+1); } if (keyValueNode == NULL) return NULL; return node->getValue(); }
std::list<string>* BST<T>::createPrintQueue() { std::list<string>* printQ = new std::list<string>(); std::list< Node<T>* >* q = new std::list< Node<T>* >(); Node<T>* curr = root; printQ->push_back(toString(curr->getValue())); q->push_back(curr); while (!q->empty()) { curr = q->front(); if (curr->getLeftChild() != 0) { printQ->push_back(toString(curr->getLeftChild()->getValue())); q->push_back(curr->getLeftChild()); }else { printQ->push_back("-"); } if (curr->getRightChild() != 0) { printQ->push_back(toString(curr->getRightChild()->getValue())); q->push_back(curr->getRightChild()); }else { printQ->push_back("-"); } q->pop_front(); //printQ->push_back(toString(curr->getValue())); } delete q; return printQ; }
bool HashTable::findWord(string word) { Node* navigator = m_head; string dictWord; while (navigator != 0) { dictWord = navigator->getValue(); if (dictWord == word) { return true; } //check for omissions if (dictWord.length() <= word.length() + 1 || dictWord.length() <= word.length() - 1) { int count = 0; //check for misplaced letter for (int i = 0; i < dictWord.length() && i < word.length(); i++) { if (dictWord[i] == word[i]) { count++; } } if (count >= (word.length() - 1)) { if (m_suggestions.size() < 10) { m_suggestions.push_back(dictWord); } } } navigator = navigator->getNext(); } return false; }
void HashTable::print(void) { Node* navigator = m_head; while (navigator != 0) { cout << navigator->getValue() << endl; navigator = navigator->getNext(); } }
/********************************************************************************************* * Function: Iterates over the contents of the list, printing the value of each node in turn. *********************************************************************************************/ void print (void) { Node *N; while (N != last) { cout << N->getValue() << endl; } cout << "-----------------------------------------------" << endl; }
bool NodeTree::removeNode(Node*& subroot, Node*& parent, const int& target) { if (subroot == NULL) { return false; } else if (target < subroot->getValue()) { return removeNode(subroot->getLeftChild(), subroot, target); } else if (target > subroot->getValue()) { return removeNode(subroot->getRightChild(), subroot, target); } else { // Go all the way down until find the last middle node and then delete it if (subroot->getMiddleChild() != NULL) { Node* nodeToDelete = subroot; while(nodeToDelete->getMiddleChild() != NULL) { parent = nodeToDelete; nodeToDelete = nodeToDelete->getMiddleChild(); } delete nodeToDelete; nodeToDelete = NULL; parent->setMiddleChild(NULL); } else if (subroot->getRightChild() != NULL) { // find replacement value and then delete replacement node Node *successor = findSuccessor(subroot->getRightChild()); subroot->setValue(successor->getValue()); return removeNode(subroot->getRightChild(), subroot, subroot->getValue()); } else { // no middle or right // simply delete the node and make it's left child the subroot (root) Node* nodeToDelete = subroot; subroot = subroot->getLeftChild(); delete nodeToDelete; nodeToDelete = NULL; } } return true; }
/******************************************************************************* * displayGraph * Displays the graph as an adjacency list representation. *******************************************************************************/ void Graph::displayGraph() { if (isEmpty()) { cerr << "The graph is empty.\n"; } else { for (int i = 0; i < mAdjacencyList->size(); i++) { cout << "Node " << mAdjacencyList->at(i)->getValue() << " -> "; Node* myNode; myNode = mAdjacencyList->at(i)->getNext(); while (myNode != NULL) { cout << myNode->getValue(); myNode = myNode->getNext(); if (myNode != NULL) { cout << ", "; } } cout << endl; } } }
bool LinkedList<T>::search(T value) const { Node<T>* temp = m_front; bool isFound = false; if(isEmpty())//if it's empty juts return false { return(false); } while(temp!=nullptr) //this condition loops until the last node { if(temp->getValue()==value) { isFound=true;//return true if it matches the value break;//and break the loop when you find it } else { temp=temp->getNext();//if you don't find it, move on to the next element } } return(isFound); }
void SkewHeap::levelOrder(Node* root) { int curLevel=1; Queue* myQueue = new Queue(); Node* temp; if(root==nullptr) { return; } root->m_level=0; myQueue->enqueue(root); while(!(myQueue->isEmpty())) { temp=myQueue->dequeue(); temp->setNext(nullptr); if(temp->m_level==curLevel) { std::cout << "\n"; curLevel++; } std::cout << temp->getValue() << " "; if(temp->getLeft()!=nullptr) { temp->getLeft()->m_level=temp->m_level+1; myQueue->enqueue(temp->getLeft()); } if(temp->getRight()!=nullptr) { temp->getRight()->m_level=temp->m_level+1; myQueue->enqueue(temp->getRight()); } } return; }
void BST<T>::printTree() { std::list<Node<T>* > queue; queue.push_front(root); int currentLevel = 1; int nextLevel = 0; while(!queue.empty()){ Node<T>* val = queue.front(); std::cout << val->getValue() << " "; queue.pop_front(); currentLevel = currentLevel - 1; if(val->getLeftChild()!=0){ queue.push_back(val->getLeftChild()); nextLevel++; } if(val->getRightChild()!=0){ queue.push_back(val->getRightChild()); nextLevel++; } if(currentLevel == 0) { std::cout << std::endl; currentLevel = nextLevel; nextLevel = 0; } } }
int peek(int index) { if (index < 0) { cout << "Invalid index." << endl; throw; } if (!stackTop) { cout << "Stack is empty." << endl; throw; } int i = 0; Node* curr = stackTop; while (curr) { if (i == index) { return curr->getValue(); } curr = curr->getNext(); i++; } cout << "Index out of bound" << endl; throw; }
T Stack<T>::pop (){ Node<T> *current = first; T val = current->getValue(); first = first->getNext(); delete current; return val; }
bool MediaGate::loadPreferences(const char *fname) { File file(fname); Parser xmlParser; Node *rootNode = xmlParser.parse(&file); if (rootNode == NULL) return false; if (rootNode->isName(PREFERENCE_CYBERGARAGE) == false) return false; Node *msNode = rootNode->getNode(PREFERENCE_MEDIASERVER); if (msNode == NULL) return false; Node *conNode = msNode->getNode(PREFERENCE_CONTENT_DIRECTORY); if (conNode == NULL) return false; Node *dirListNode = conNode->getNode(PREFERENCE_DIRECTORY_LIST); if (dirListNode == NULL) return false; int dirCnt = dirListNode->getNNodes(); for (int n=0; n<dirCnt; n++) { Node *dirNode = dirListNode->getNode(n); if (dirNode->isName(PREFERENCE_DIRECTORY) == false) continue; Node *nameNode = dirNode->getNode(PREFERENCE_NAME); Node *pathNode = dirNode->getNode(PREFERENCE_PATH); if (nameNode == NULL || pathNode == NULL) continue; const char *name = nameNode->getValue(); const char *path = pathNode->getValue(); #ifdef WIN32 string pathStr = path; path = StringReplaceChars(pathStr, "'", "\\"); #endif FileDirectory *fileDir = new FileDirectory(name, path); getMediaServer()->addContentDirectory(fileDir); } delete rootNode; return true; }
Node* Graph::find(int v) { Node *temp = getRootNode(); while (temp != NULL) { if (v < temp->getValue() == v) { return temp; } else if ( v < temp->getValue() ) { temp = temp->getLeftNode(); } else { temp = temp->getRightNode(); } } return NULL; }