int main() { ListArray<CD>* cds = CD::readCDs("cds.txt"); int num_items = cds->size(); cout << num_items << endl; while(true) { HeapSkew<CD>* sh = new HeapSkew<CD>(&CD::compare_items); ListArrayIterator<CD>* iter = cds->iterator(); while(iter->hasNext()) { CD* cd = iter->next(); sh->heapInsert(cd); } delete iter; /*while(!(sh->heapIsEmpty())) { CD* cd = sh->heapRemove(); cd->displayCD(); }*/ delete sh; } deleteCDs(cds); delete cds; return 0; }
int main() { ListArray<CD>* cds = CD::readCDs("cds.txt"); int num_items = cds->size(); cout << num_items << endl; TableSortedList<CD>* slt = new TableSortedList<CD>(&CD::compare_items, &CD::compare_keys); //DO THIS //thoroughly test your table for(int c = 0; c < num_items; c++) { slt->tableInsert(cds->get(c+1)); } displayTitles(slt); deleteCDs(cds); delete cds; delete slt; return 0; }
ListArrayIterator<String>* String::getTokens(char token) { int num_tokens = numTokens(token); ListArray<String>* strings = new ListArray<String>(); int start = 0; for (int j = 0; j < num_tokens; j++) { int index = indexOf(token, start); //the index of the next token char* temp = new char[index - start + 1]; for (int i = start; i < index; i++) { temp[i - start] = charAt(i); } temp[index - start] = 0; //set null terminator start = index + 1; String* str = new String(temp); strings->add(str); delete[] temp; } ListArrayIterator<String>* token_iter = strings->iterator(); delete strings; return token_iter; }
int main() { ListArray<CD>* cds = CD::readCDs("cds.txt"); int num_items = cds->size(); cout << num_items << endl; TableSortedList<CD>* slt = new TableSortedList<CD>(&CD::compare_items, &CD::compare_keys); //DO THIS //thoroughly test your table ListArrayIterator<CD>* iter = cds->iterator(); if(slt->tableIsEmpty()) { int count = 0; while(iter->hasNext()) { CD* cd = iter->next(); slt->tableInsert(cd); count++; } } ListDoublyLinkedIterator<CD>* iter2 = slt->iterator(); /*while(iter2->hasNext()) { CD* myCD = iter2->next(); myCD->getKey()->displayString(); cout << endl; }*/ String* title = new String("Manifesto"); bool rem = slt->tableRemove(title); if (rem) cout << "Removed item" << endl << endl; while(iter2->hasNext()) { CD* myCD = iter2->next(); myCD->getKey()->displayString(); cout << endl; } cout << endl; cout << slt->tableSize() << endl; delete title; deleteCDs(cds); delete cds; delete slt; return 0; }
int main() { ListArray<CD>* cds = CD::readCDs("cds.txt"); int num_items = cds->size(); cout << num_items << endl; TableSortedList<CD>* slt = new TableSortedList<CD>(&CD::compare_items, &CD::compare_keys); //DO THIS //thoroughly test your table ListArrayIterator<CD>* iter = cds -> iterator(); while (iter -> hasNext()) { CD* cd = iter -> next(); slt -> tableInsert(cd); } delete iter; ListDoublyLinkedIterator<CD>* iter2 = slt -> iterator(); while (iter2 -> hasNext()) { CD* cd2 = iter2 -> next(); cd2 -> displayCD(); } delete iter2; String title = String("Wolves"); slt -> tableRemove(&title); ListDoublyLinkedIterator<CD>* iter3 = slt -> iterator(); while (iter3 -> hasNext()) { CD* cd3 = iter3 -> next(); cd3 -> displayCD(); } delete iter3; String title2 = String("NotHere"); slt -> tableRemove(&title); ListDoublyLinkedIterator<CD>* iter4 = slt -> iterator(); while (iter4 -> hasNext()) { CD* cd4 = iter4 -> next(); cd4 -> displayCD(); } delete iter4; deleteCDs(cds); delete cds; delete slt; return 0; }
void Password::guess(int try_password, int num_matches) { ListArray<String>* list = new ListArray<String>(); for(int i = 1; i <= viable_words->size(); i++) { int matches = getNumMatches(all_words->get(try_password), viable_words->get(i)); if(matches == num_matches) { list->add(viable_words->get(i)); } } delete viable_words; viable_words = list; }
void Password::guess(int try_password, int num_matches){ String* original_word = getOriginalWord(try_password); ListArrayIterator<String>* iter_viable_words = viable_words->iterator(); ListArray<String>* temp = new ListArray<String>(); while(iter_viable_words->hasNext()){ String* comp_word = iter_viable_words->next(); int count = getNumMatches(original_word, comp_word); if(count == num_matches){ temp->add(comp_word); } } delete viable_words; viable_words = temp; }
void Password::guess(int try_password, int num_matches) { String* guessWord = all_words->get(try_password); ListArray<String>* newList = new ListArray<String>(); ListArrayIterator<String>* iter = viable_words->iterator(); while(iter->hasNext()) { { String* c_word = iter->next(); int match = getNumMatches(guessWord , c_word); if(match == num_matches) newList->add(c_word); } delete viable_words; viable_words = newList; } }
int main() { //the unsorted ListArray of cds ListArray<CD>* cds = CD::readCDs("cds.txt"); int num_items = cds->size(); cout << num_items << endl; cout << endl; //test the binary search tree //insert all of the cds ListArrayIterator<CD>* iter = cds->iterator(); BinarySearchTree<CD>* bst = new BinarySearchTree<CD>(&CD::compare_items, &CD::compare_keys); while(iter->hasNext()) { CD* cd = iter->next(); bst->insert(cd); } delete iter; //DO THIS //test your tree sort method CD** unsorted_cds = cds->toArray(); CD** sorted_cds = BinarySearchTree<CD>::treeSort(unsorted_cds, num_items, &CD::compare_items, &CD::compare_keys); for (int i = 0; i < num_items; i++) { sorted_cds[i]->getKey()->displayString(); cout << endl; } deleteCDs(cds); delete cds; return 0; }
int main() { ListArray<CD>* cds = CD::readCDs("cds.txt"); int num_items = cds->size(); cout << num_items << endl; TableSortedList<CD>* slt = new TableSortedList<CD>(&CD::compare_items, &CD::compare_keys); //DO THIS //thoroughly test your table deleteCDs(cds); delete cds; delete slt; return 0; }
int main() { ListArray<CD>* cds = CD::readCDs("cds.txt"); int num_items = cds->size(); cout << num_items << endl; TableSortedList<CD>* slt = new TableSortedList<CD>(&CD::compare_items, &CD::compare_keys); //DO THIS //thoroughly test your table ListArrayIterator<CD>* addCDs = cds->iterator(); while (addCDs->hasNext()) { slt->tableInsert(addCDs->next()); } cout << "\nSize after adding CDs: " << slt->tableSize() << endl; String* sk = new String("Omnivium"); cout << "Retrieving " << "Omnivium" << "..." << endl; slt->tableRetrieve(sk)->displayCD(); cout << "\nSize after retrieval: " << slt->tableSize() << endl; cout << "\n\nRemoving " << "Omnivium" << "..." << endl; bool isDelete = slt->tableRemove(sk); cout << "Successful? "; if (isDelete) cout << "Yes"; else cout << "No"; sk = new String("Programmed To Consume"); cout << "\n\nRemoving " << "Programmed to Consume" << "..." << endl; isDelete = slt->tableRemove(sk); cout << "Successful? "; if (isDelete) cout << "Yes"; else cout << "No"; cout << "\n\n\nSize after removal: " << slt->tableSize() << endl; sk = new String("My Little Pony Anthology"); cout << "\n\nRemoving item not in table, " << "My Little Pony Anthology" << "..." << endl; isDelete = slt->tableRemove(sk); cout << "Successful? "; if (isDelete) cout << "Yes"; else cout << "No"; cout << "\n\n\nSize after removal: " << slt->tableSize() << endl; sk = new String("Back in Black"); cout << "\n\nRetrieving item not in table, " << "Back in Black" << "..." << endl; cout << "\n\nInserting duplicates..." << endl; ListArray<CD>* cds2 = CD::readCDs("cds.txt"); ListArrayIterator<CD>* addCDs2 = cds2->iterator(); while (addCDs2->hasNext()) { slt->tableInsert(addCDs2->next()); } //finished size should be 310 since removed CDs are re-added cout << "Size after inserting duplicates: " << slt->tableSize(); cout << "\n\n\nIs the table empty? "; if (!slt->tableIsEmpty()) cout << "No"; else cout << "Yes"; cout << "\n\n\nRemoving all CDs..." << endl; slt->~TableSortedList(); //deconstruct ALL THE THINGS cout << "\n\nIs the table empty? "; if(!slt->tableIsEmpty()) cout << "No"; else cout << "Yes"; deleteCDs(cds); delete cds; delete slt; return 0; }
int main() { //the unsorted ListArray of cds ListArray<CD>* cds = CD::readCDs("cds.txt"); int numItems = cds->size(); cout << numItems << endl; cout << endl; //test the binary search tree //insert all of the cds ListArrayIterator<CD>* iter = cds->iterator(); BinarySearchTree<CD>* bst = new BinarySearchTree<CD>(&CD::compare_items, &CD::compare_keys); while(iter->hasNext()) { CD* cd = iter->next(); bst->insert(cd); } delete iter; BinaryTreeIterator<CD>* bst_iter = bst->iterator(); bst_iter->setInorder(); //takes a snapshot of the data while(bst_iter->hasNext()) { CD* cd = bst_iter->next(); //cd->displayCD(); } delete bst_iter; int height = bst->getHeight(); bool balance = bst->isBalanced(); if(balance == true) { cout << "\nThe height is " << height << " and it is balanced.\n"; } else { cout << "\nThe height is " << height << " and it is not balanced.\n"; } //create a minimum height binary search tree BinarySearchTree<CD>* min_bst = bst->minimize(); bst_iter = min_bst->iterator(); //make sure that an inorder traversal gives sorted order bst_iter->setInorder(); //takes a snapshot of the data while(bst_iter->hasNext()) { CD* cd = bst_iter->next(); //cd->displayCD(); } delete bst_iter; height = min_bst->getHeight(); balance = min_bst->isBalanced(); if(balance == true) { cout << "\nThe minimum height is " << height << " and it is balanced.\n"; } else { cout << "\nThe minimum height is " << height << " and it is not balanced.\n"; } //create a complete binary search tree BinarySearchTree<CD>* complete_bst = bst->minimizeComplete(); delete bst; //make sure that an inorder traversal gives sorted order bst_iter = complete_bst->iterator(); bst_iter->setInorder(); //takes a snapshot of the data while(bst_iter->hasNext()) { CD* cd = bst_iter->next(); //cd->displayCD(); } delete bst_iter; height = complete_bst->getHeight(); balance = complete_bst->isBalanced(); if(balance == true) { cout << "\nThe minimum height is " << height << " and it is balanced.\n"; } else { cout << "\nThe minimum height is " << height << " and it is not balanced.\n"; } delete complete_bst; deleteCDs(cds); delete cds; return 0; }
int main() { ListArray<CD>* cds = CD::readCDs("cds.txt"); int num_items = cds->size(); cout << num_items << endl; TableSortedList<CD>* slt = new TableSortedList<CD>(&CD::compare_items, &CD::compare_keys); //DO THIS //thoroughly test your table ListArrayIterator<CD>* iter = cds->iterator(); String* titles[310]; CD* cds2[310]; int count = 0; CD* duplicateTest; // test duplicate CD to be added later while(iter->hasNext()) { CD* cd = iter->next(); duplicateTest = cd; cds2[count]=cd; titles[count]=cds2[count]->getKey(); count++; slt->tableInsert(cd); } delete iter; slt->tableInsert(duplicateTest); // Program should not insert this CD because it is a duplicate cout << "Table size after inserting all CDs is " << slt->tableSize() << endl; // If insert works correctly, size should be 310 (If duplicate was added, it would be 311) for(count = 0; count < 310; count++) { CD* cdRetrieve = slt->tableRetrieve(titles[count]); // testing the retrieve function, it should be able to find all 310 CDs if (cdRetrieve == NULL) cout << "Couldn't find CD" << endl; else cout << "Found CD #" << count+1 << endl; } count = 1; ListDoublyLinkedIterator<CD>* iter2 = slt->iterator(); // Testing the SLDL iterator, every CD should be displayed while(iter2->hasNext()) { CD* cd3 = iter2->next(); cout << "\nThis is CD #" << count << ":" << endl << endl; count++; cd3->displayCD(); } for(int i = 0; i < 310; i++) { slt->tableRemove(titles[i]); } cout << "Table size after removing all CDs is " << slt->tableSize(); // Table size should be 0 after removing everything deleteCDs(cds); delete cds; delete slt; return 0; }