void ht_merge(SortedListPtr list, const char* filepath, HashTable *hashtable, SortedListPtr wordlist) { FileCountPair *fcp; WordCountPair *wcp; SortedListIteratorPtr iterator; SortedListIteratorPtr mainListIterator; char *word; WordListPair *wlp; iterator = SLCreateIterator(wordlist); while ((word = SLNextItem(iterator)) != NULL) { mainListIterator = SLCreateIterator(list); wcp = ht_get(hashtable, word); fcp = malloc(sizeof(FileCountPair)); fcp->filepath = filepath; fcp->count = wcp->count; while ((wlp = SLNextItem(mainListIterator)) != NULL) { if (strcmp(wlp->word, word) == 0) { break; } } if (wlp == NULL) { //the word is not already in the main list SortedListPtr newlist = SLCreate(&compareFileCounts); SLInsert(newlist, fcp); wlp = malloc(sizeof(WordListPair)); wlp->word = word; wlp->list = newlist; SLInsert(list, wlp); } else { SLInsert(wlp->list, fcp); } SLDestroyIterator(mainListIterator); } SLDestroyIterator(iterator); }
/* test 6: 1. Do the test 5 with SLNextItem over list */ void test6(){ SortedListPtr list = SLCreate(compare, destroy); SortedListIteratorPtr iter; int i; int* value; for (i = 1; i <= 5; i++) { value = (int*)malloc(sizeof(int)); *value = i; SLInsert(list, (void*)value); } iter = SLCreateIterator(list); value = (int*)SLGetItem(iter); while (value != NULL) { printf("Value = %d\n", *value); value = (int*)SLNextItem(iter); } value = (int*)SLNextItem(iter); if (value != NULL) { printf("Over end of list, must be null\n"); exit(0); } SLDestroy(list); SLDestroyIterator(iter); printf("Test 6 completed\n"); }
void destroyWordList() { SortedListIteratorPtr wordIterator = NULL; wordIterator = SLCreateIterator(wordList); WordNode curWord; SortedListIteratorPtr fileIterator = NULL; FileWithCount curFile; void *obj = NULL; void *objInWord = NULL; while ((obj = SLNextItem(wordIterator))) { curWord = (WordNode) obj; free (curWord->word); fileIterator = SLCreateIterator(curWord->files); while ((objInWord = SLNextItem(fileIterator))) { curFile = (FileWithCount) objInWord; free(curFile->filename); } SLDestroyIterator(fileIterator); SLDestroy(curWord->files); } SLDestroyIterator(wordIterator); SLDestroy(wordList); }
void writeWordListToFile(char *filename) { /*Traverse wordlist, grab each word*/ FILE *f = fopen(filename, "w"); if (f == NULL) { printf("Error opening file!\n"); exit(1); } /* print some text */ const char *text = "Write this to the file"; fprintf(f, "Some text: %s\n", text); SortedListIteratorPtr wordIterator = NULL; wordIterator = SLCreateIterator(wordList); WordNode curWord; SortedListIteratorPtr fileIterator = NULL; FileWithCount curFile; void *obj = NULL; void *objInWord = NULL; while ((obj = SLNextItem(wordIterator))) { curWord = (WordNode) obj; fprintf(f,"<list> %s\n",curWord->word); fileIterator = SLCreateIterator(curWord->files); while ((objInWord = SLNextItem(fileIterator))) { curFile = (FileWithCount) objInWord; fprintf(f,"%s %d ",curFile->filename,curFile->count); } SLDestroyIterator(fileIterator); fprintf(f,"\n</list>\n",curWord->word); } SLDestroyIterator(wordIterator); fclose(f); return; }
/* test 1: 1. create list, check it is empty 2. use 1, create list iterate, check empty 3. use 2, check SLGetItem 4. use 3, check SLNextItem 5. destroy list iterator 6. destroy list */ void test1(){ SortedListPtr list = SLCreate(compare, destroy); SortedListIteratorPtr iter; int* value; if (list == NULL) { printf("Cannot created list\n"); exit(0); } iter = SLCreateIterator(list); value = (int*)SLGetItem(iter); if (value != NULL) { printf("Value must be null\n"); exit(0); } value = (int*)SLNextItem(iter); if (value != NULL) { printf("Value must be null\n"); exit(0); } SLDestroy(list); SLDestroyIterator(iter); printf("Test 1 completed\n"); }
/* test 7: 1. create list with compare and destroy functions 2. check SLInsert, add 1, 2, 3, 4, 5, SLRemove 3. create list iterate 4. check SLGetItem, SLNextItem 5. destroy list iterator 6. destroy list */ void test7(){ SortedListPtr list = SLCreate(compare, destroy); SortedListIteratorPtr iter; int i; int* value; for (i = 1; i <= 5; i++) { value = (int*)malloc(sizeof(int)); *value = i; SLInsert(list, (void*)value); } iter = SLCreateIterator(list); i = SLRemove(list, value); printf("SLRemove 5 returned %d\n", i); i = SLRemove(list, value); printf("SLRemove 5 returned %d\n", i); value = (int*)SLGetItem(iter); while (value != NULL) { printf("Value = %d\n", *value); value = (int*)SLNextItem(iter); } SLDestroy(list); SLDestroyIterator(iter); printf("Test 7 completed\n"); }
void *SLNextItem(SortedListIteratorPtr iter){ //make sure all those cases are taken care of if(iter == NULL){ printf("Iterator was not allocated properly\n"); return NULL; } else if(iter->currentNode == NULL) return NULL; //check for null will terminate a loop in case it is used else{ if(iter->currentNode->isValid){ //node was not removed while this pointer pointed to it void * dataReturned = malloc(sizeof(iter->currentNode->data)); dataReturned = iter->currentNode->data; //save the data the iterator is pointing to iter->currentNode->numPointers--; //decrement curent node's # of pointers iter->currentNode = iter->currentNode->next; //update the iterator if(iter->currentNode != NULL) iter->currentNode->numPointers++; //now that the iterator was incremented we can update the number of iterators pointing to the new node (assuming it is not outside of the list) return dataReturned; } else{ //if node was removed while this pointer pointed to it iter->currentNode->numPointers--; //remove function acts as a garbage collection and will take care of this node if there are no pointers pointing to it iter->currentNode = iter->currentNode->next; if(iter->currentNode != NULL) iter->currentNode->numPointers++; return SLNextItem(iter); //does the same with the next token } } }
/* Destroy a word struct. Free all associated memory. * */ void destroyWordStruct(WordPtr wordStruct) { if(wordStruct == NULL) { return; } else { //printf("Freeing wordStruct for %s\n", wordStruct->wordArray); //Loop over filestats array and free all values if(wordStruct->filestats != NULL) { if(wordStruct->filestats->headOfList != NULL) //Free the filestats data too { SortedListIteratorPtr filestatsIter = SLCreateIterator(wordStruct->filestats); do { SLNode curNode = filestatsIter->curNode; StatsPtr value = (StatsPtr)curNode->value; destroyStatsStruct(value); }while(SLNextItem(filestatsIter) != NULL); SLDestroyIterator(filestatsIter); SLDestroy(wordStruct->filestats); } } free(wordStruct->wordArray); free(wordStruct); return; } }
int SLRemove(SortedListPtr list, void *newObj) { SortedListIteratorPtr ptr = SLCreateIterator(list); if(ptr == NULL) { return 0; } listItem* cur; listItem* prev; //checking at head int compcur = list->compareF(newObj,list->head->data); if(compcur == 0) { //move head list->head = list->head->next; //if been viewed once, free if(ptr->current->viewers==1) { freeItem(ptr->current); } SLDestroyIterator(ptr); return 1; } cur = ptr->current->next; prev = ptr->current; while(ptr->current != NULL && prev != NULL) { compcur = list->compareF(newObj, SLNextItem(ptr)); if(compcur == 0) { prev->next = cur->next; if(cur->viewers == 1) { freeItem(cur); } SLDestroyIterator(ptr); return 1; } prev = cur; cur = cur->next; } SLDestroyIterator(ptr); printf("we should not have gotten here"); return 0; }
int addWord(char * word, char * filename){ //intialisation WordCount fileToAdd; fileToAdd = (WordCount)malloc(sizeof(WordCount)); fileToAdd->filename = (char*)malloc((sizeof(char)*100)); fileToAdd->count = 1; fileToAdd->filename = filename; ll word = (ll)malloc(sizeof(ll)); word->word = (char*)malloc((sizeof(char)*100)); word->word = word; //need to look at sorted list ll wordexists; wordexists = (ll) SLFindNode(wordList, word); //if exists if(wordexists != NULL){ printf("word %s exists\n", word); SortedListIteratorPtr iter; iter = SLCreateIterator(wordexists->file); void *object = NULL; while((object = SLNextItem(iter))){ WordCount file; file = object; if(compareCounts(file, object) == 0){ file->count++; break; } //addWord(file->file->filename); } SLDestroyIterator(iter); if(object == NULL){ //does not found, add!! SLInsert(wordexists->file, fileToAdd); //adding //free free(word->word); free(word); return 1; } else{ //free everything printf("error when adding word\n"); free(fileToAdd->filename); free(fileToAdd); free(word->word); free(word); } return 0; } else{ //file not found word->file = SLCreate(compareCounts); SLInsert(word->file,fileToAdd); SLInsert(wordList, word); return 1; } }
int main(int argc, char **argv) { /* int i7,i5,i3,i4; */ /* double d1, d2, d3, d4, d5; */ char *s1, *s2, *s3, *s4, *s5; SortedListPtr sl; SortedListIteratorPtr slip; sl = SLCreate(compareInts); s1 = "Hello"; s2 = "World"; s3 = "Dawgs"; s4 = "Tacos"; s5 = "Zebra"; SLInsert(sl, s1); // => ['HELLO'] printListString(sl); SLInsert(sl, s2); // => ['WORLD', 'HELLO'] printListString(sl); SLInsert(sl, s3); // => ['WORLD', 'HELLO', 'DOGS'] printListString(sl); SLInsert(sl, s4); // => ['WORLD', 'HELLO', 'DOGS'] printListString(sl); SLInsert(sl, s5); // => ['ZZZ', 'WORLD', 'HELLO', 'DOGS'] printListString(sl); SLRemove(sl, s2); // => ['ZZZ', 'HELLO', 'DOGS'] printListString(sl); slip = SLCreateIterator(sl); printf("------------------------------\n"); printf("%s\n", (char*)SLNextItem(slip)); SLRemove(sl, s1); printf("%s\n", (char*)SLNextItem(slip)); printListString(sl); printf("%s\n", (char*)SLNextItem(slip)); SLDestroyIterator(slip); SLDestroy(sl); return 0; }
/* Given a sorted list storing strings, go through and free the value. * */ void destroyAllStrings(SortedListPtr list) { SortedListIteratorPtr listIter = SLCreateIterator(list); do { free(listIter->curNode->value); }while(SLNextItem(listIter) != NULL); SLDestroyIterator(listIter); return; }
int SLRemove(SortedListPtr list, void *newObj) { int removeVal; void* currObj; SortedListIteratorPtr remPtr = SLCreateIterator(list); if (remPtr == NULL){ printf("Failed to create iterator!\n"); return 0; } node *prev = remPtr->curr; removeVal=(*list->compF)(newObj, list->head->obj); if(removeVal == 0){ list->head = list->head->next; if (remPtr->curr->refs==2){ //remove head free(remPtr->curr); } SLDestroyIterator(remPtr); return 1; } currObj=SLNextItem(remPtr); while(remPtr->curr != NULL){ //go until eof removeVal = (*list->compF)(newObj, currObj); if(removeVal==0){ //match found prev->next = remPtr->curr->next; if(remPtr->curr->refs == 2){ free(remPtr->curr); //destroy node if not used by other iterators } SLDestroyIterator(remPtr); return 1; }else{ prev = remPtr->curr; currObj=SLNextItem(remPtr); } } SLDestroyIterator(remPtr); printf("Object not found\n"); //not found return 0; }
void printDoubles(SortedListIteratorPtr itr) { void *value; do { value = SLNextItem(itr); if(value != NULL) printf("%.1f, ", *(double*)value); } while(value != NULL); }
void printChars(SortedListIteratorPtr itr) { void *value; do { value = SLNextItem(itr); if(value != NULL) printf("%c, ", *(char*)value); } while(value != NULL); }
/* Insert all file names from a given word's node into a given list * */ void insertAllFilenames(SortedListPtr list, SLNode word) { WordPtr actualWord = (WordPtr)word->value; SortedListIteratorPtr filenameIter = SLCreateIterator(actualWord->filestats); do { StatsPtr curStat = filenameIter->curNode->value; char *word = curStat->filename; SLInsert(list, word); }while(SLNextItem(filenameIter) != NULL); SLDestroyIterator(filenameIter); }
void printStrings(SortedListIteratorPtr itr) { void *value; do { value = (char*) SLNextItem(itr); if(value != NULL) printf("%s, ", value); } while(value != NULL); }
void printIntegers(SortedListIteratorPtr itr) { void *value; do { value = SLNextItem(itr); if(value != NULL) printf("%d, ", *(int*)value); } while(value != NULL); }
int SLInsert(SortedListPtr list, void *newObj){ double compareVal; void* currObj; SortedListIteratorPtr iterPtr = SLCreateIterator(list); if (iterPtr == NULL){ return 0; } node* prev = iterPtr->curr; if (iterPtr->curr == NULL || (*list->compF)(newObj, iterPtr->curr->obj) > 0){ //empty list/first object bigger list->head = NodeCreate(newObj,iterPtr->curr); // head insert SLDestroyIterator(iterPtr); return 1; } currObj=SLNextItem(iterPtr); while(iterPtr->curr != NULL){ compareVal = (*list->compF)(newObj, currObj); //compare curr/new objs if(compareVal >= 0){ //add here break; }else if(compareVal == 0){ //duplicate, ignore SLDestroyIterator(iterPtr); printf("duplicate item!!!\n"); return 1; }else{ //increment pointers prev = iterPtr->curr; currObj=SLNextItem(iterPtr); } } prev->next = NodeCreate(newObj,iterPtr->curr); //insert node if (prev->next==NULL) { printf("Malloc fail!\n"); return 0; } SLDestroyIterator(iterPtr); return 1; }
SortedListPtr searchOr(SortedListPtr indexList, SortedListPtr inputList) { //need a result list SortedListPtr result = SLCreate(*compareStrings); //need to iterate through the inputList to check the words in the indexList SortedListIteratorPtr inputIterator = SLCreateIterator(inputList); do { SLNode inputListPtr = inputIterator->curNode; char* inputListWord = (char*)inputListPtr->value; //need to iterate through the indexListIterator for each item of inputList SortedListIteratorPtr indexListIterator = SLCreateIterator(indexList); do { //compare the word in the indexed list to the input list to see if they match SLNode indexListPtr = indexListIterator->curNode; WordPtr indexWord = (WordPtr) indexListPtr->value; int compareWords = strcmp(indexWord->wordArray, inputListWord); if(compareWords == 0) { //extract the filelist for the word SortedListPtr filePtr = indexWord->filestats; SortedListIteratorPtr fileIterator = SLCreateIterator(filePtr); //add the word to the result list do { SLNode fileHead = (SLNode)filePtr->headOfList; StatsPtr fileNamesPtr = (StatsPtr)fileHead->value; char* filenameToAdd = fileNamesPtr->filename; SLInsert(result, filenameToAdd); }while (SLNextItem(fileIterator) != NULL); SLDestroyIterator(fileIterator); } } while (SLNextItem(indexListIterator) != NULL); SLDestroyIterator(indexListIterator); } while(SLNextItem(inputIterator) != NULL); SLDestroyIterator(inputIterator); return result; }
/* Given two lists of file names, return a list of file names that * is in both lists. */ SortedListPtr findCommonElements(SortedListPtr firstList, SortedListPtr secondList) { SortedListPtr commonElements = SLCreate(*compareStrings); SortedListIteratorPtr outsideIter = SLCreateIterator(firstList); do { //printf("Grabbing outside node.\n"); SLNode outsideNode = outsideIter->curNode; SortedListIteratorPtr insideIter = SLCreateIterator(secondList); do { //For every element in the second list, compare it to whatever my outsideIter is pointing to. SLNode insideNode = insideIter->curNode; //printf("Attempting to compare file stats by file name\n"); //int compareVal = compareStatsByFilename(outsideNode->value, insideNode->value); if(outsideNode == NULL || insideNode == NULL || outsideNode->value == NULL || insideNode->value == NULL) { SLDestroyIterator(insideIter); SLDestroyIterator(outsideIter); SLDestroy(commonElements); //printf("Returning from findcommonelements because something is null.\n"); return NULL; } int compareVal = strcmp(outsideNode->value, insideNode->value); //printf("Compareval = %d\n",compareVal); if(compareVal == 0) //Same file name, add it to the results list { //Theyre the same, so I can just add the outside one char* outsideStats = outsideNode->value; //printf("Found common element: %s\n", outsideStats); SLInsert(commonElements, outsideStats); } }while(SLNextItem(insideIter) != NULL); SLDestroyIterator(insideIter); }while(SLNextItem(outsideIter) != NULL); SLDestroyIterator(outsideIter); return commonElements; }
int main(int argc, char** argv){ //***************** STRING TEST *********************** int i; const char* stringList[] = {"Hey", "Boy", "Zeus", "world", " ", "blah", " zzz", "aaa"}; char* stringPtr = (char*) malloc(sizeof(stringList)); SortedListPtr stringsl = SLCreate(compareStrings, NULL); /* for(i=0; i<(sizeof(stringList)/sizeof(stringList[0])); i++){ stringPtr = (char*) stringList[i]; SLInsert(stringsl, stringPtr); } */ SortedListIteratorPtr stringiterator2 = SLCreateIterator(stringsl); SLNextItem(stringiterator2); SLInsert(stringsl, "f"); SLInsert(stringsl, "d"); SLInsert(stringsl, "b"); //SLRemove(stringsl, "Apple"); SortedListIteratorPtr stringiterator = SLCreateIterator(stringsl); while((stringPtr = (char*) SLNextItem(stringiterator)) != NULL){ printf("%s\n", stringPtr); } //cleans mess free(stringPtr); SLDestroy(stringsl); SLDestroyIterator(stringiterator); }
/* Given an index, go through and free all memory for word structs. * */ void destroyAllWordStructs(SortedListPtr index) { if(index == NULL) { return; } if(index->headOfList == NULL) { return; } SortedListIteratorPtr wordIter = SLCreateIterator(index); do { WordPtr curWordStruct = (WordPtr)wordIter->curNode->value; destroyWordStruct(curWordStruct); }while(SLNextItem(wordIter) != NULL); SLDestroyIterator(wordIter); return; }
void iterprint_all(SortedListPtr s, SortedListIteratorPtr iter, int type){ void *item; while(1){ item = SLNextItem(iter); if (item == NULL){ break; }else{ if(type == INT){ printf("%d ", *((int*)item)); }else if(type == DOUBLE){ printf("%f ", *((double*)item)); } else if(type==STRING){ printf("%s ", ((char*)item)); } } } printf("\n"); }
void iterprint_all(SortedListPtr s, SortedListIteratorPtr iter, int type){ void *item = SLGetItem(iter); while(1){ //item = SLGetItem(iter);//modify by lezi if (item == NULL){ break; }else{ if(type == INT){ printf("%d ", *((int*)item)); }else if(type == DOUBLE){ printf("%f ", *((double*)item)); } else if(type==STRING){ printf("%s ", ((char*)item)); }else if(type == STRUCT){ printf("struct={%d} ", ((TestStruct*)item)->field); } } item = SLNextItem(iter);//modify by lezi } printf("\n"); }
void add_word(char *filename, char *wordToAdd, char *tokenString) { FileWithCount file; file = (FileWithCount) malloc( sizeof(FileWithCount) ); file->filename = (char *) malloc( BUFSIZ+1 ); snprintf(file->filename, BUFSIZ, "%s", filename); file->count = 1; printf("Filename after file %s and should be %s\n",file->filename,filename); /*Find if word exists*/ WordNode wordToFind = (WordNode) malloc(sizeof(WordNode)); wordToFind->word = (char *) malloc (BUFSIZ+1); snprintf(wordToFind->word, BUFSIZ, "%s", wordToAdd); WordNode isWordThere; isWordThere = (WordNode) SLFind(wordList, wordToFind); if (isWordThere != NULL) { /*Word already exists*/ printf("WORD EXISTS: %s\n", wordToAdd); SortedListIteratorPtr iter; iter = SLCreateIterator(isWordThere->files); void *obj = NULL; while ((obj = SLNextItem(iter))) { FileWithCount objFile; objFile = obj; if (compareFilesWithCount(objFile, file) == 0) { /*Filename present, just increase count and break*/ objFile->count++; break; } } SLDestroyIterator(iter); if(obj == NULL) { /*File not found, insert it into list*/ printf("Filename before slinsert %s\n",file->filename); SLInsert(isWordThere->files, file); printf("Inserted file %s into word %s\n",file->filename,isWordThere->word); printf("Filename actually %s\n",filename); free (wordToFind->word); free (wordToFind); } else { fprintf(stderr, "ERROR: Add Word\n"); free (file->filename); free (file); free (wordToFind->word); free (wordToFind); } return; } else { /*Word doesn't exist yet*/ wordToFind->files = SLCreate(compareFilesWithCount); /* printf("The word of the day is %s\n", w->word); */ /*Insert file path into word*/ SLInsert(wordToFind->files, file); /*Insert word into word list*/ SLInsert(wordList, wordToFind); return; } }
/* Perform logical and search * * */ SortedListPtr searchLogicalAnd(SortedListPtr indexHead, SortedListPtr words) { SortedListPtr result = SLCreate(*compareStrings); SortedListPtr nextResult = NULL; SortedListPtr testResult = NULL; //Check if the first word exists SortedListIteratorPtr wordIter = SLCreateIterator(words); char *curWord = (char*)wordIter->curNode->value; int wordSearch = SLSearchWord(indexHead, curWord); //If the word is not in the list then just screw it, they get nothing. if(wordSearch == -1) { //printf("Returning becasue wordSearch=-1\n"); SLDestroyIterator(wordIter); SLDestroy(result); return NULL; } //First word is in the list, so just copy everything from its filestats to results SLNode firstwordNode = SLGetIndex(indexHead, wordSearch); //printf("Copying all first word's stuff into new list\n"); insertAllFilenames(result, firstwordNode); //Now hop into a while loop and check over and over. int i = 0; while(SLNextItem(wordIter) != NULL) { SortedListPtr nextWordList = SLCreate(*compareStrings); curWord = (char*)wordIter->curNode->value; int searchVal = SLSearchWord(indexHead, curWord); if(searchVal == -1) //Word not in the list, return nothing immediately. { //destroyAllStrings(result); //printf("Returning because searchVal = -1 inside while.\n"); SLDestroy(nextWordList); //SLDestroy(result); if(nextResult != NULL) { SLDestroy(nextResult); } SLDestroyIterator(wordIter); return NULL; } else { SLNode nextNode = SLGetIndex(indexHead, searchVal); //printf("Copying another word's file stats shit\n"); insertAllFilenames(nextWordList, nextNode); //printf("Attempting to find common elements.\n"); if(i == 0) { testResult = findCommonElements(result, nextWordList); if(testResult != NULL) { nextResult = testResult; } else { if(nextResult != NULL) { SLDestroy(nextResult); } SLDestroyIterator(wordIter); SLDestroy(nextWordList); return NULL; } SLDestroy(result); } else { testResult = findCommonElements(nextResult, nextWordList); if(testResult != NULL) { nextResult = testResult; } else { if(nextResult != NULL) { SLDestroy(nextResult); } SLDestroyIterator(wordIter); SLDestroy(nextWordList); return NULL; } } } //destroyAllStrings(nextWordList); SLDestroy(nextWordList); i++; } SLDestroyIterator(wordIter); if(i == 0) { return result; } else { return nextResult; } }
int main(int argc, char** argv){ printf("Sample test cases, for more test cases look at testcode.txt and at testplan.txt for its expected behavior\n\n"); printf("********************************************* TEST CASE 1 ***********************************************\n\n"); SortedListPtr sl1 = SLCreate(compareInts, NULL); SLDestroy(sl1); printf("\n"); printf("********************************************* TEST CASE 6 ***********************************************\n\n"); const char * stringArray[] = {"Zoid", "Brackets", "Apple"}; void * printer; SortedListPtr sl2 = SLCreate(compareStrings, destroyBasicTypeNoAlloc); int i; for(i=0; i<(sizeof(stringArray)/sizeof(stringArray[0])); i++){ if(SLInsert(sl2, (void *)stringArray[i])==0) printf("There was an error but the list was still created anyway\n"); } SortedListIteratorPtr iter2 = SLCreateIterator(sl2); printf("List: "); while((printer=SLNextItem(iter2)) != NULL) printf("%s ", (char *)printer); SLInsert(sl2, " zoom"); printf("%s ", (char *)SLNextItem(iter2)); printf("\n\n"); SLDestroy(sl2); SLDestroyIterator(iter2); printf("********************************************* TEST CASE 8 ***********************************************\n\n"); const char * stringArr[] = {"zoom", "wii", "Soccer", "Zoid", "Brackets", " zoom"}; void * print; void * print2; SortedListPtr sl = SLCreate(compareStrings, destroyBasicTypeNoAlloc); int p; for(p=0; p<(sizeof(stringArr)/sizeof(stringArr[0])); p++){ if(SLInsert(sl, (void *)stringArr[p])==0) printf("There was an error but the list was still created anyway\n"); } SortedListIteratorPtr iterator = SLCreateIterator(sl); SortedListIteratorPtr iterator2 = SLCreateIterator(sl); printf("iterator 1: "); print = SLNextItem(iterator); printf("%s \n", (char *)iterator->currentNode->data); printf("iterator 2: "); print2 = SLNextItem(iterator2); print2 = SLNextItem(iterator2); printf("%s \n", (char *)iterator2->currentNode->data); SLRemove(sl, iterator->currentNode->data); SLRemove(sl, iterator2->currentNode->data); printf("%s \n", (char *)SLNextItem(iterator)); printf("%s \n", (char *)SLNextItem(iterator2)); SLDestroyIterator(iterator); SLDestroy(sl); return 0; }
int main() { SortedListPtr sl = SLCreate(&compareInts); int* n1 = malloc(sizeof(int*)); int* n2 = malloc(sizeof(int*)); int* n3 = malloc(sizeof(int*)); int* n4 = malloc(sizeof(int*)); int* n5 = malloc(sizeof(int*)); *n1 = 5; *n2 = 10; *n3 = 3; *n4 = 8; *n5 = 11; printf("Test Case 1: Adding\n"); SLInsert(sl, n1); SLInsert(sl, n2); SLInsert(sl, n3); SLInsert(sl, n4); SortedListIteratorPtr sp = SLCreateIterator(sl); int* foo; do{ foo = SLNextItem(sp); if (foo == NULL) { break; } printf("%d\n", *foo); } while (foo != NULL); printf("Test Case 2: Removing \n"); SortedListIteratorPtr sz = SLCreateIterator(sl); SLRemove(sl, n4); do{ foo = SLNextItem(sz); if (foo == NULL) { break; } printf("%d\n", *foo); } while (foo != NULL); printf("Test Case 3: Removing/Adding in Middle\n"); SortedListPtr t3 = SLCreate(&compareInts); SLInsert(t3, n1); SLInsert(t3, n2); SLInsert(t3, n3); SLInsert(t3, n4); SortedListIteratorPtr p3 = SLCreateIterator(t3); foo = SLNextItem(p3); printf("%d\n", *foo); printf("remove: 8, then continue iterating \n"); SLRemove(t3, n4); printf("add 11, then continue iterating \n"); SLInsert(t3, n5); do{ foo = SLNextItem(p3); if (foo == NULL) { break; } printf("%d\n", *foo); } while (foo != NULL); printf("Iterate whole list from start \n"); SortedListIteratorPtr p3a = SLCreateIterator(t3); do{ foo = SLNextItem(p3a); if (foo == NULL) { break; } printf("%d\n", *foo); } while (foo != NULL); printf("Test Case 4: Removing and Iterating Empty List\n"); SortedListPtr t4 = SLCreate(&compareInts); SLRemove(t4, n1); SortedListIteratorPtr p4 = SLCreateIterator(t4); do{ foo = SLNextItem(p4); if (foo == NULL) { break; } printf("%d\n", *foo); } while (foo != NULL); printf("Test Case 5: Strings\n"); char* c1 = malloc(sizeof(char) * 5); char* c2 = malloc(sizeof(char) * 5); char* c3 = malloc(sizeof(char) * 5); strcpy(c1, "hey\0"); strcpy(c2, "yo\0"); strcpy(c3, "kk\0"); printf("%s\n", c1); SortedListPtr t5 = SLCreate(&compareStrings); SLInsert(t5, c1); SortedListIteratorPtr p5 = SLCreateIterator(t5); char *resp = SLNextItem(p5); printf("%s\n", resp); free(n1); free(n2); free(n3); free(n4); free(n5); SLDestroyIterator(p4); SLDestroyIterator(p3); SLDestroyIterator(p3a); SLDestroyIterator(sz); SLDestroyIterator(sp); SLDestroy(sl); SLDestroy(t3); SLDestroy(t4); }
int main(int argc, char*argv[]) { int choice = atoi(argv[1]); printf("Choice: %d\n", choice); if(choice == 1){ //1. Normal SLInserts (@ beginning, middle, end of list) //a. integers SortedListPtr s = SLCreate(compareInts,destroyBasicTypeAlloc); int iarr[6] = {7,5,155,42,-1,6}; if(populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,NULL,NULL,NULL) == 0){ failure(); return 1; } SortedListIteratorPtr iter = SLCreateIterator(s); iterprint_all(s,iter,INT); //prints 155 42 7 6 5 -1 }else if(choice == 2){ //b. doubles SortedListPtr s = SLCreate(compareDoubles,destroyBasicTypeAlloc); double darr[6] = {7.43,5.55,155.26,42.1,-1.5,6.7}; if(populate_list(s,DOUBLE,sizeof(darr)/sizeof(double),NULL,darr,NULL,NULL) == 0){ failure(); return 1; } SortedListIteratorPtr iter = SLCreateIterator(s); iterprint_all(s,iter,DOUBLE); //prints 155.26 42.1 7.43 6.7 5.55 -1.5 }else if(choice == 3){ //c. strings SortedListPtr s = SLCreate(compareStrings,destroyBasicTypeAlloc); char *carr[6] = {"cat","dog","catamaran","apple","cormorant","zebra"}; if(populate_list(s,STRING,6,0,0,carr,NULL) == 0){ failure(); return 1; } SortedListIteratorPtr iter = SLCreateIterator(s); iterprint_all(s,iter,STRING); //prints apple cat catamaran cormorant dog zebra // should print zebra dog cormorant catamaran cat apple }else if(choice == 4){ //d. structs SortedListPtr s = SLCreate(compareTestStructs,destroyBasicTypeAlloc); TestStruct strtarr[6]; strtarr[0].field = 7; strtarr[1].field = 5; strtarr[2].field = 155; strtarr[3].field = 42; strtarr[4].field = -1; strtarr[5].field = 6; if(populate_list(s,STRUCT,6,0,0,0,strtarr) == 0){ failure(); return 1; } SortedListIteratorPtr iter = SLCreateIterator(s); iterprint_all(s,iter,STRUCT); //prints 155 42 7 6 5 -1 }else if(choice == 5){ //SLRemove nonexistent element SortedListPtr s = SLCreate(compareInts,destroyBasicTypeAlloc); int iarr[6] = {7,5,155,42,-1,6}; populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0,NULL); int toremove = 54; if (SLRemove(s,&toremove) != 0) { failure(); }else{ success(); } }else if(choice == 6){ //SLRemove existing element (no duplicates; didn't handle them anyway) SortedListPtr s = SLCreate(compareInts,destroyBasicTypeAlloc); int iarr[6] = {7,5,155,42,-1,6}; populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0,NULL); int toremove = 5; if (SLRemove(s,&toremove) != 0) { SortedListIteratorPtr iter = SLCreateIterator(s); iterprint_all_int(s,iter); //prints 155 42 7 6 -1 }else{ failure(); } }else if(choice == 7){ //Iterate on empty list //Should not fail SortedListPtr s = SLCreate(compareInts,destroyBasicTypeAlloc); SortedListIteratorPtr iter = SLCreateIterator(s); iterprint_all_int(s,iter); }else if (choice == 8){ //SLInsert for item beyond iterator SortedListPtr s = SLCreate(compareInts,destroyBasicTypeAlloc); int iarr[6] = {7,5,155,42,-1,6}; populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0,NULL); SortedListIteratorPtr iter = SLCreateIterator(s); void *item = SLNextItem(iter); item = SLNextItem(iter); int *toins = malloc(sizeof(int)); *toins = 4; SLInsert(s,toins); iterprint_all_int(s,iter); //prints 7 6 5 4 -1 }else if(choice == 9){ //Create multiple iterators on same list, interleave iterations. //Delete item between iterators. SortedListPtr s = SLCreate(compareInts,destroyBasicTypeAlloc); int iarr[6] = {7,5,155,42,-1,6}; populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0,NULL); SortedListIteratorPtr olditer = SLCreateIterator(s); SortedListIteratorPtr iter = SLCreateIterator(s); void *item; int i; for(i = 0; i < 2; i++){ item = SLGetItem(olditer);//modify by lezi printf("%d ", *((int*)item)); item = SLNextItem(olditer);//modify by lezi } //prints 155 42 printf("\n"); for(i = 0; i < 4; i++){ item = SLGetItem(iter); //modify by lezi printf("%d ", *((int*)item)); item = SLNextItem(iter);//modify by lezi } //prints 155 42 7 6 printf("\n"); int itoremove = 6; if (SLRemove(s,&itoremove) == 0) { failure(); return 1; } item = SLGetItem(iter); //prints 5 //modufy by lezi printf("%d\n", *((int*)item)); item = SLGetItem(olditer); printf("%d\n", *((int*)item)); //prints 7 //modify by lezi iterprint_all_int(s,iter); //prints 5 -1 iterprint_all_int(s,olditer); //prints 7 5 -1 }else if(choice == 10){ //SLRemove end element, iterator positioned on it SortedListPtr s = SLCreate(compareInts,destroyBasicTypeAlloc); int iarr[3] = {7,5,3}; populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0,NULL); SortedListIteratorPtr iter = SLCreateIterator(s); int x3 = 3; void *item = SLNextItem(iter); item = SLNextItem(iter);//iterator points to 3 if(SLRemove(s,&x3) && SLNextItem(iter) == NULL){ success(); }else{ failure(); } }else if(choice == 11){ //TODO //SLRemove element in middle, iterator positioned on it SortedListPtr s = SLCreate(compareInts,destroyBasicTypeAlloc); int iarr[3] = {7,5,3}; populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0,NULL); SortedListIteratorPtr iter = SLCreateIterator(s); void *item = SLNextItem(iter); int x1 = 5; if(SLRemove(s,&x1) && *((int*)SLNextItem(iter)) == 3 && ((int*)SLNextItem(iter)) == NULL){ success(); }else{ failure(); } }else if(choice == 12){ //SLRemove element positioned immediately after element iterator is pointing to SortedListPtr s = SLCreate(compareInts,destroyBasicTypeAlloc); int iarr[6] = {7,5,155,42,-1,6}; populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0,NULL); SortedListIteratorPtr iter = SLCreateIterator(s); void *item = SLNextItem(iter); item = SLNextItem(iter); int x1 = 6; if(SLRemove(s,&x1) && *((int*)SLGetItem(iter)) == 7 //modify by lezi && *((int*)SLNextItem(iter)) == 5 && *((int*)SLNextItem(iter)) == -1 && ((int*)SLNextItem(iter)) == NULL){ success(); }else{ failure(); } }else if(choice == 13){ //Add item between recently returned element and element about to be returned. SortedListPtr s = SLCreate(compareInts,destroyBasicTypeAlloc); int iarr[3] = {7,5,3}; populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0,NULL); SortedListIteratorPtr iter = SLCreateIterator(s); void *item = SLNextItem(iter); item = SLNextItem(iter);//point to 3 now int *x4 = malloc(sizeof(int)); *x4 = 4; SLInsert(s,x4); iterprint_all_int(s,iter); //prints 3 }else if(choice == 14){ //Add item to head and middle, after iterator has been created //modify by Lezi SortedListPtr s = SLCreate(compareInts,destroyBasicTypeAlloc); int iarr[3] = {7,5,3}; populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0,NULL); SortedListIteratorPtr iter = SLCreateIterator(s); int *toinsert = malloc(sizeof(int)); *toinsert = 8; SLInsert(s,toinsert); int *toinsert1 = malloc(sizeof(int)); *toinsert1 = 4; // add by lezi SLInsert(s,toinsert1);//add by lezi iterprint_all_int(s,iter); //prints 7 5 4 3 //modify by lezi }else if(choice == 15){ //Add item to tail after all items have been returned by iterator. SortedListPtr s = SLCreate(compareInts,destroyBasicTypeAlloc); int iarr[3] = {7,5,3}; populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0,NULL); SortedListIteratorPtr iter = SLCreateIterator(s); void *item; while((item = SLNextItem(iter)) != NULL); int *toins = malloc(sizeof(int)); *toins = -1; SLInsert(s,toins); if(SLGetItem(iter) == NULL){ //modify by lezi success(); }else{ failure(); } }else{ printf("Bad input\n"); } return 0; }