/*Function for adding row The spreadsheet is iterated till the position where the row is to be added. insert() is called to insert a row above the row values in user input. If the row is added to first position (0th position), head pointer is adjusted to point the row which is just inserted. */ void addRow(LList<LList<Cell*>>& rows, int addRowValue){ int h = 0; //increments to track the height or row value LList<Cell*> empty; //create an empty LinkedList of Cell type for (LList<LList<Cell*>>::iterator I = rows.begin(); I != rows.end(); I++){ /*Insert a cell on the position if the value of the rows iterated equals to the value of row in the user input where the row is to be inserted */ if (h == addRowValue) { for (LList<Cell*>::iterator J = (*I).begin(); J != (*I).end(); J++) { I = rows.insert(I, empty, addRowValue); //inserts a Cell in first column (0th column) at specific row position //Push backs empty LList at the back of the First cell till the width of column (last column) for (int k = 0; k < width; k++){ (*I).push_back(new Cell()); } break; } } h++; } //increase the height of the spreadsheet if a row is added height += 1; }
int main() { LList<int> llist; LList<int>::iterator it = llist.begin(); LList<int>::iterator it2 = llist.end(); it2 = LList<int>::iterator(it); #if 1 it = llist.insert(it,1); it = llist.insert(it,2); it = llist.insert(it,3); it = llist.begin(); cout<<"--------insert-------------"<<endl; for(;it != llist.end();it++) cout<<*it<<endl; llist.clear(); llist.push_back(11); llist.push_back(12); llist.push_back(13); llist.erase(llist.begin()); cout<<"--------push_back-------------"<<endl; for(it=llist.begin();it != llist.end();it++) cout<<*it<<endl; llist.clear(); cout<<"--------size-------------"<<llist.size()<<endl; llist.push_back(14); llist.push_back(15); llist.push_back(16); for(it=llist.begin();it != llist.end();it++) cout<<*it<<endl; cout<<"--------transfer-------------"<<llist.size()<<endl; LList<int>::iterator first= ++llist.begin(); LList<int>::iterator last= llist.end(); llist.transfer(++llist.begin(),++first,last); for(it=llist.begin();it != llist.end();it++) cout<<*it<<endl; cout<<"--------reverse-------------"<<llist.size()<<endl; llist.reverse(); for(it=llist.begin();it != llist.end();it++) cout<<*it<<endl; #endif return 0; }
LList<char *> IRC::split(char *str,char *delim) { LList<char *> list; char *tmp = strtok(str,delim); while (tmp != NULL) { list.insert(tmp); tmp = strtok(NULL, delim); } return list; }
int TestInsertionSort_LList() { LList<int> *llist = new LList<int>(); llist->insert(4); llist->insert(2); llist->insert(0); llist->insert(3); llist->insert(1); InsertionSort<int> *is = new InsertionSort<int>(); llist->sort(is); delete is; for (int i = 0; i < 5; i++) { TEST_ASSERT(llist->get(i) == i); } delete llist; return 0; }
int main() { LList c; int k; cin>>k; for (int i=1; i<=k; ++i) { for (int j=0; j<=i; ++j) c.insert(fraction(j,i)); } c.print(); return 0; }
int TestQuickSort_LList() { LList<int> *llist = new LList<int>(); llist->insert(4); llist->insert(2); llist->insert(0); llist->insert(3); llist->insert(1); QuickSort<int> *qs = new QuickSort<int>(); llist->sort(qs); delete qs; for (int i = 0; i < 5; i++) { TEST_ASSERT(llist->get(i) == i); } delete llist; return 0; }
int TestShellSort_LList() { LList<int> *llist = new LList<int>(); llist->insert(4); llist->insert(2); llist->insert(0); llist->insert(3); llist->insert(1); ShellSort<int> *ss = new ShellSort<int>(); llist->sort(ss); delete ss; for (int i = 0; i < 5; i++) { TEST_ASSERT(llist->get(i) == i); } delete llist; return 0; }
int TestLList() { LList<char *> *llist = new LList<char *>(); TEST_ASSERT(llist); TEST_ASSERT(!llist->valid((unsigned int)-1)); TEST_ASSERT(!llist->valid(1)); TEST_ASSERT(!llist->valid(0)); llist->insert(newStr("one")); llist->insert(newStr("two")); llist->insert(newStr("three")); llist->insert(newStr("four")); TEST_ASSERT(strcmp(llist->get(0), "one") == 0); TEST_ASSERT(strcmp(llist->get(2), "three") == 0); TEST_ASSERT(strcmp(llist->get(3), "four") == 0); TEST_ASSERT(strcmp(llist->get(1), "two") == 0); delete [] llist->get(1); llist->remove(1); TEST_ASSERT(strcmp(llist->get(0), "one") == 0); TEST_ASSERT(strcmp(llist->get(1), "three") == 0); TEST_ASSERT(strcmp(llist->get(2), "four") == 0); while (llist->valid(0)) { delete [] llist->get(0); llist->remove(0); } TEST_ASSERT(!llist->valid((unsigned int)-1)); TEST_ASSERT(!llist->valid(1)); TEST_ASSERT(!llist->valid(0)); delete llist; return 0; }
int TestSort_LList(Sorter<int> *_sorter) { LList<int> *llist = new LList<int>(); for (int i = 0; i < SORT_ITEMS; i++) { llist->insert(rand()); } llist->sort(_sorter); for (int i = 0; i < SORT_ITEMS - 1; i++) { TEST_ASSERT(llist->get(i) <= llist->get(i+1)); } delete llist; return 0; }
void ParseMemoryLeakFile(const char *_inputFilename, const char *_outputFilename) { /* */ /* Start up */ /* */ RedBlackTree<char *, int> combined; RedBlackTree<char *, int> frequency; int unrecognised = 0; /* */ /* Open the file and start parsing */ /* */ FILE *memoryfile = fopen(_inputFilename, "rb"); while (memoryfile && !feof(memoryfile)) { char thisline[1024]; fgets(thisline, 1024, memoryfile); if (!strncmp(thisline, " Data:", 6) == 0) { /* This line is a data line - useless to us */ if (strchr(thisline, ':')) { /* This line does not have a source file location - useless to us */ /* Get the size */ char *lastcomma = strrchr(thisline, ','); if (lastcomma == 0) continue; char *ssize = lastcomma + 2; int size; char unused[32]; sscanf(ssize, "%d %s", &size, unused); /* Get the source file name */ char *sourcelocation = thisline; char *colon = strrchr(thisline, ':'); *(colon - 1) = '\x0'; /* Put the result into our BTree */ int result = 0; bool found = combined.find(sourcelocation, result); if (found) combined.replace(sourcelocation, result + size); else combined.insert(sourcelocation, size); found = frequency.find(sourcelocation, result); if (frequency.exists(sourcelocation)) frequency.replace(sourcelocation, result + size); else frequency.insert(sourcelocation, 1); } else { char *lastcomma = strrchr(thisline, ','); if (lastcomma) { char *ssize = lastcomma + 2; int size; char unused[32]; sscanf(ssize, "%d %s", &size, unused); unrecognised += size; } } } } fclose(memoryfile); /* */ /* Sort the results into a list */ /* */ LList<char *> sorted; DArray<char *> *dataI = combined.ConvertIndexToDArray(); DArray<int> *dataD = combined.ConvertToDArray(); int totalsize = 0; for (size_t i = 0; i < dataI->size(); i++) { if (dataI->valid(i)) { char *newsource = dataI->get(i); int newsize = dataD->get(i); totalsize += newsize; bool inserted = false; for (size_t i = 0; i < sorted.size(); i++) { char *existingsource = sorted.get(i); int existingsize; combined.find(existingsource, existingsize); if (newsize <= existingsize) { sorted.insert_at(newsource, i); inserted = true; break; } } if (!inserted) sorted.insert(newsource); } } delete dataI; delete dataD; /* */ /* Open the output file */ /* */ if (sorted.size()) { FILE *output = fopen(_outputFilename, "wt"); /* */ /* Print out our sorted list */ /* */ fprintf(output, "Total recognised memory leaks : %d Kbytes\n", int( totalsize / 1024 )); fprintf(output, "Total unrecognised memory leaks : %d Kbytes\n\n", int( unrecognised / 1024 )); for (int k = (int)sorted.size() - 1; k >= 0 && k < (int)sorted.size(); k--) { char *source = sorted.get(k); CoreAssert(source); int size; combined.find(source, size); int freq; frequency.find(source, freq); if (size > 1048576) { fprintf(output, "%-95s (%d MB in %d leaks)\n", source, int( size / 1048576 ), freq); } else if (size > 2048) { fprintf(output, "%-95s (%d KB in %d leaks)\n", source, int( size / 1024 ), freq); } else { fprintf(output, "%-95s (%d bytes in %d leaks)\n", source, size, freq); } } /* */ /* Clean up */ fclose(output); } }
int main() { LList L; srand(77); unsigned int r, e, pos; r = rand() % 12; cout << "Inserting " << r << " elements to the list...." << endl; for (int i=0; i<r; i++) { e = rand()%100; cout << "Inserting " << e << " at position 0" << endl; L.insert(e,0); } cout << "Contents of the list: " << L << endl << endl; r = rand() % r; cout << "Now erasing elements from random positions ...." << endl; for (int i=0; i<r; i++) { pos = rand()% L.getSize(); cout << "Erasing from position " << pos << endl; L.erase(pos); cout << "Contents of the list: " << L << endl; } cout << endl; /***** Uncomment after you have implemented the push function * e = rand()%100; cout << "Now lets push a " << e <<" into the list..." << endl; cout << "The list is " << L.push(e) << " long " << endl; cout << "And contains: " << L << endl << endl; ************************************************************/ /***** Uncomment after you have implemented the shift function * cout << "I have removed the element from the front of the list: " << L.shift() << endl; cout << "List now contains: " << L << endl << endl; ************************************************************/ /***** Uncomment after you have implemented the pop function * cout << "Removing element from the front of the list using pop() " << endl; cout << "The element is: " << L.pop() << endl; cout << "List now contains: " << L << endl << endl; // Error del profe! No descomente // pos = rand() % L.getSize(); // cout << "Removing element from pos " << pos // << " of the list using pop(" << pos << ")" << endl; // cout << "The element is: " << L.pop(pos) << endl; // cout << "List now contains: " << L << endl << endl; ************************************************************/ /***** Uncomment after you have implemented the splice function * r = rand() % (L.getSize()/2); r = 9; pos = rand() % (L.getSize()/2); cout << "Using the splice function to remove " << r << " elements starting at position " << pos << endl; cout << "The removed elements are: " << L.splice(pos, r) << endl; cout << "After splice, the List:" << L << endl << endl; ************************************************************/ /***** Uncomment after you have implemented the join function cout << "The following is a string of all elements, using join:" << endl; cout << L.join("-") << endl << endl; ************************************************************/ cout << endl; }