Exemplo n.º 1
0
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);

}
Exemplo n.º 2
0
/*Free functions*/
void destroySet(int lines, Cache destroyMe, int assoc){
	if(destroyMe->nextLevel!=NULL){
		/*need to determine kind*/
				if(strcmp(destroyMe->nextLevel->type, "direct")==0){
					destroyDirect(destroyMe->nextLevel->numofSet, destroyMe->nextLevel, destroyMe->nextLevel->assoc);
				}	
				if((strlen(destroyMe->nextLevel->type)>=7)&&(strncmp(destroyMe->nextLevel->type,"assoc:", 5)==0)){
				//	printf("I came here");
					destroySet(destroyMe->nextLevel->numofSet, destroyMe->nextLevel, destroyMe->nextLevel->assoc);
				}
				if((strlen(destroyMe->nextLevel->type)==5)&& (strcmp(destroyMe->nextLevel->type,"assoc")==0)){
					destroySet(destroyMe->nextLevel->numofSet, destroyMe->nextLevel, destroyMe->nextLevel->assoc);
				}
			}

	for (int i=0;i<lines;i++){
		if((strlen(destroyMe->type)>=7)&&(strncmp(destroyMe->type,"assoc:", 5)==0)){
			/*destroy dll for each individual set*/
				if(destroyMe->blocks[i]->LRU!=NULL){
					SLDestroy(destroyMe->blocks[i]->LRU);
					destroyMe->blocks[i]->LRU=NULL;
				}
				else{
					destroyQueue(destroyMe->blocks[i]->FIFO);
					destroyMe->blocks[i]->FIFO=NULL;
				}
			/*destroy queue for each individual set*/
		}
		free(destroyMe->blocks[i]);
	}
	/*destroy FA's fifo or lru*/
	if((strcmp(destroyMe->type,"FA")==0)||((strlen(destroyMe->type)==5)&& (strcmp(destroyMe->type,"assoc")==0))){
		/*only for FA*/
		if(destroyMe->LRU!=NULL){
					SLDestroy(destroyMe->LRU);
					destroyMe->LRU=NULL;
				}
		else{
				destroyQueue(destroyMe->FIFO);
				destroyMe->FIFO=NULL;
			}
	}
	free(destroyMe->blocks);
	free(destroyMe);
	destroyMe=NULL;
	return;

}
Exemplo n.º 3
0
/*
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");
}
Exemplo n.º 4
0
/*
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");
}
Exemplo n.º 5
0
/*
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");
}
Exemplo n.º 6
0
/* 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;
	}
}
Exemplo n.º 7
0
void delete_all_records(void)
{
  struct Record *current_record, *tmp;

  HASH_ITER(hh, records, current_record, tmp) {
    HASH_DEL(records, current_record);
    free(current_record->word);
    SLDestroy(current_record->filenames);
    free(current_record);
  }
Exemplo n.º 8
0
int main(int argc, char* argv[])
{
	SortedListPtr newList = parseFileToList(argv[1]);
	if(newList == NULL)
	{
		printf("No indexer file to parse\n");
		return 0;
	}
	takeInput(newList);
	destroyAllWordStructs(newList);
	SLDestroy(newList);
	return 0;
}
Exemplo n.º 9
0
/* 
	main method invokes exploreDirectories() to create the TokenList datastructure
	Then invokes writFile to write the inverted-index file.
*/
int main(int argc, char **argv)
{

	TokenList = SLCreate(compareTokenStructs, destroyTokenStruct);

	argCheck(argc, argv);
	exploreDirectories(argv[2]);

	writeFile(argv[1]);

	SLDestroy(TokenList);

	return 0;
}
Exemplo n.º 10
0
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;
}
Exemplo n.º 11
0
int main(int argc, char **argv) {

   if(argc != 3){
      printf("Error: invalid input\n");
      return -1;
   }

	CompareFuncT cf = &compareWordListPairs;
	SortedListPtr words = SLCreate(cf);
    char *filepath = argv[2];
    if (filepath[strlen(filepath)-1] == '/') {
        filepath[strlen(filepath)-1] = '\0';
    }

	scan_dir(filepath, filepath, words);

   outputPairsToFile(argv[1],words);
   SLDestroy(words);
   return 0;
}
Exemplo n.º 12
0
/* 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;
}
Exemplo n.º 13
0
/*
test 4:
1. create list with compare and destroy functions
2. check SLInsert, add 1, 2, 3, 4, 5
3. create list iterate
4. check SLGetItem
5. destroy list iterator
6. destroy list
*/
void test4(){
	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);
	printf("Value = %d\n", *value);

	SLDestroy(list);
	SLDestroyIterator(iter);

	printf("Test 4 completed\n");
}
Exemplo n.º 14
0
Arquivo: main.c Projeto: CS214/p2ll
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);
}
Exemplo n.º 15
0
Arquivo: main.c Projeto: v1ta/Academic
int main(int argc, const char * argv[]) {
    
    char * strs[7] = {"supar short","this is a short string","this is a longer string so I can test the comparator", "this is an even longer string so I can test the comparator", "this is a super duper mooper long string so I can test this stuff", "this is a short stringmk","this is an even longer string so I can test the comparator"};
    
    int numbers[5] = {5,1,3,7,5};
    
    Struct_Obj strtarr[7];
    strtarr[0].data = 200;
    strtarr[1].data = 1;
    strtarr[2].data = 3;
    strtarr[3].data = 9;
    strtarr[4].data = 45;
    strtarr[5].data = 600;
    strtarr[6].data = 324;
    
    char c;
    int i;
    
    c = getchar();
    
    if (c == '1') {
        
        /*
         * This test is simply to test order, it will populat a list,
         * and then remove a few items. The iterator is absent from this test.
         */
        
        SortedListPtr list = SLCreate(compareStrings, destroy);
        
        
        
        for(i = 0; i < 3; i++)
            SLInsert(list, strs[i]);
        
        //SLRemove(list, strs[3]);
        //SLRemove(list, strs[5]);
        Node curr = NULL;
        
        for(curr = list->next; curr != NULL; curr = curr->next)
        {
            printf("output strlen: %lu\n", strlen(((char*)curr->Object)));
        }
        
        SLDestroy(list);
    }
    else if (c == '2')
    {
        /*
         * This will test iterators traversing a list.
         * A remove will be inserted mid traversal to test the "ghost routine"
         * described in the readme.
         */
        
        SortedListPtr list = SLCreate(compareStrings, destroy);
        
        
        for(i = 0; i < 7; i++)
            SLInsert(list, strs[i]);
        
        SortedListIteratorPtr iter2= SLCreateIterator(list); // this will work because the list has elements.
        
        SLGetItem(iter2);
        SLNextItem(iter2);
        SLNextItem(iter2);
        
        SLRemove(list, strs[1]);
        SLRemove(list, strs[4]);
        Node curr = NULL;
        
        for(curr = list->next; curr != NULL; curr = curr->next)
        {
            if(!curr->Object)
                continue;
            
            printf("output: %lu\n", strlen(((char*)curr->Object)));
        }
        
        SLDestroyIterator(iter2);
        SLDestroy(list);
    }
    else if(c == '3')
    {
        /*
         * This case will test the ghost node functionality of SortedList.
         * It will create some iterators and delete the object at the target location
         * of the current iterator.
         */
        
        SortedListPtr list = SLCreate(compareStrings, destroy);
        
        
        for(i = 0; i < 7; i++)
            SLInsert(list, strs[i]);
        
        SortedListIteratorPtr iter= SLCreateIterator(list);
        SLRemove(list, strs[4]); //remove head
        SLRemove(list, strs[3]); //remove head + 1
        SLRemove(list, strs[3]); //remove head + 2
        SLNextItem(iter);
        
        Node curr = NULL;
        
        for(curr = list->next; curr != NULL; curr = curr->next)
        {
            if(!curr->Object)
                continue;
            
            printf("output: %lu\n", strlen(((char*)curr->Object)));
        }
        
        SLDestroyIterator(iter);
        SLDestroy(list);
    }
    else if(c == '4')
    {
        /*
         * This test case will create and list, assign it two iterator
         * and remove the node of the current target of the iterator.
         * The node at the end of the list will be removed. We will then
         * try to push the iterator passed the end of the list.
         */
        
        SortedListPtr list = SLCreate(compareStrings, destroy);
        
        for(i = 0; i < 7; i++)
            SLInsert(list, strs[i]);
        
        SortedListIteratorPtr iter= SLCreateIterator(list);
        SortedListIteratorPtr iter2 = SLCreateIterator(list);
        
        SLRemove(list, strs[4]); //remove head
        SLRemove(list, strs[3]); //remove head + 1
        SLRemove(list, strs[3]); //remove head + 2
        SLRemove(list, strs[0]); //remove tail
        SLNextItem(iter2);
        SLNextItem(iter2);
        SLNextItem(iter2);
        SLNextItem(iter2);
        SLNextItem(iter2); //well passed end of list
        SLNextItem(iter);
        SLNextItem(iter);
        
        Node curr = NULL;
        
        for(curr = list->next; curr != NULL; curr = curr->next)
        {
            if(!curr->Object)
                continue;
            
            printf("output: %lu\n", strlen(((char*)curr->Object)));
        }
        
        SLDestroyIterator(iter);
        SLDestroyIterator(iter2);
        SLDestroy(list);
    }
    else if(c == '5')
    {
        /*
         * This is the stress test-case we will use sting objects, it will
         * test all of the previous cases along with actions to attempt to
         * weed out an edge case.
         */
        
        SortedListPtr list = SLCreate(compareStrings, destroy);
        
        for(i = 0; i < 7; i++)
            SLInsert(list, strs[i]);
        
        SortedListIteratorPtr iter= SLCreateIterator(list);
        SortedListIteratorPtr iter2 = SLCreateIterator(list);
        
        SLRemove(list, strs[4]); //remove head
        SLRemove(list, strs[3]); //remove head + 1
        SLRemove(list, strs[3]); //remove head + 2
        SLRemove(list, strs[0]); //remove tail
        SLNextItem(iter);
        SLNextItem(iter2);
        SLNextItem(iter2);
        SLNextItem(iter2);
        SLNextItem(iter2); //well passed end of list
        SLNextItem(iter);
        SLNextItem(iter);
        
        for(i = 0; i < 7; i++)
            SLInsert(list, strs[i]);
        
        SortedListIteratorPtr iter3 = SLCreateIterator(list);
        
        SLNextItem(iter3);
        SLNextItem(iter3);
        SLRemove(list, strs[4]);
        
        Node curr = NULL;
        
        for(curr = list->next; curr != NULL; curr = curr->next)
        {
            if(!curr->Object)
                continue;
            
            printf("output: %lu\n", strlen(((char*)curr->Object)));
        }
        
        SLNextItem(iter);
        SLDestroyIterator(iter);
        SLDestroyIterator(iter3);
        SLDestroyIterator(iter2);
        SLDestroy(list);
        
    }
    else if (c == '6')
    {
        SortedListPtr list = SLCreate(compareStrings, destroy);
        
        for(i = 0; i < 7; i++)
            SLInsert(list, strs[i]);
        
        SortedListIteratorPtr iter = SLCreateIterator(list);
        
        for(i=0;i<7;i++)
            SLRemove(list, strs[i]);
        
        for (i = 0; i < 100; i++) //try to break iterator
            SLNextItem(iter);
        
        SLRemove(list, strs[0]); //try to remove object that isn't in the list
        
        Node curr = NULL;
        
        for(curr = list->next; curr != NULL; curr = curr->next)
        {
            if(!curr->Object)
                continue;
            printf("output strlen: %lu\n", strlen(((char*)curr->Object)));
        }
        
        SLDestroyIterator(iter);
        SLDestroy(list);
    }
    else if (c =='7')
    {
        /*
         * This case tests a new object type.
         * This is a simple load, no deletes or iterators are used.
         */
        
        SortedListPtr list = SLCreate(compareInts, destroy);
        
        for (i=0; i< 5; i++) {
            int *ptr;
            ptr = &numbers[i];
            SLInsert(list, ptr);
        }

        Node curr = NULL;
        
        for(curr = list->next; curr != NULL; curr = curr->next)
        {
            printf("output int: %d\n", *((int *)curr->Object));
        }
        
        SLDestroy(list);
    }
    else if (c == '8')
    {
        /*
         * Load the LL with int object, then proceed to
         * created an iterator. Nuke the current target of the iterator
         * which is also the head. Increment iterator and check valid LL
         */
        
        SortedListPtr list = SLCreate(compareInts, destroy);
        int *ptr;
        
        for (i=0; i< 5; i++) {

            ptr = &numbers[i];
            SLInsert(list, ptr);
        }
        
        SortedListIteratorPtr iter = SLCreateIterator(list);
        int x = 7;
        ptr = &x; //remove head while iterator is targeting
       // SLRemove(list, ptr);
        SLNextItem(iter); //advance iterator after making target ghost node
        Node curr = NULL;
        
        for(curr = list->next; curr != NULL; curr = curr->next)
        {
            if(!curr->Object)
                continue;
            
            printf("output int: %d\n", *((int *)curr->Object));
        }
        
        SLDestroy(list);
        SLDestroyIterator(iter);
    }
    else if(c == '9')
    {
        /*
         * This case is for testing structs for storing in the LL.
         * Load structs into LL and test for correct order.
         */
        
        SortedListPtr list = SLCreate(compareStrings, destroy);
        Struct_Obj *ptr;
        
        for (i=0; i< 7; i++) {
            
            ptr = &strtarr[i];
            SLInsert(list, ptr);
        }
        

        Node curr = NULL;
        
        for(curr = list->next; curr != NULL; curr = curr->next)
        {
            if(!curr->Object)
                continue;
            printf("output int: %d\n", *((int *)curr->Object));
        }
        
        SLDestroy(list);
    }
    else if(c == '0')
    {
        SortedListPtr list = SLCreate(compareStrings, destroy);
        Struct_Obj *ptr;
        
        for (i=0; i< 7; i++) {
            
            ptr = &strtarr[i];
            SLInsert(list, ptr);
        }
        
        SortedListIteratorPtr iter = SLCreateIterator(list);
        ptr = &strtarr[5];
        
        SLRemove(list, ptr); //remove head / iterator target
        
        for(i=0;i<20;i++) //iterate past end of LL
            SLNextItem(iter);
        
        
        Node curr = NULL;
    
        for(curr = list->next; curr != NULL; curr = curr->next)
        {
            if(!curr->Object)
                continue;
            printf("output int: %d\n", *((int *)curr->Object));
        }
        
        SLDestroy(list);
        SLDestroyIterator(iter);
    }
    else
    {
        printf("bad input, please run the program again, proper input is 0-9\n");
    }
    return 0;
    
}
Exemplo n.º 16
0
void takeInput(SortedListPtr indexList)
{
	int maxInputSize = 128;

	char line[maxInputSize];
	char *p;
	int done = 0;
	while(done == 0)
	{
		p = fgets(line, maxInputSize, stdin); 
		if (p == NULL)
		{
			return;
		}
		else
		{
			int last = strlen(line);
			if(line[last-1] == '\n')
			{
				line[last-1] = '\0';
			}
			printf("Input: %s\n", line);
		}
		int quit = strcmp("q",line);
		if(quit == 0)
		{
			done = 1;
			printf("Gracefully shutting down...\n");
		}
		else
		{
			//check to see if the operation is sa or so
			char* subbuff = strtok(p, " ");
			
			char* newsa = "sa";
			char* newso = "so";
			int sa = strcmp(newsa, subbuff);
			int so = strcmp(newso, subbuff);
			if((sa == 0) || (so == 0))
			{

				//need to add all words given into a linked list
				subbuff = strtok(NULL, " ");
				
				if(subbuff != NULL)
				{
					SortedListPtr inputList = inputToLinkedList(subbuff);
					if(sa == 0)
					{

							//do search and
							SortedListPtr SAList = searchLogicalAnd(indexList, inputList);
							if(SAList == NULL)
							{
								printf("No files found\n");
							}
							else
							{
								printList(SAList);
								/*if(SAList->headOfList != NULL)
								{
									//destroyAllStrings(SAList);
									SLDestroy(SAList);
								}*/
								SLDestroy(SAList);
							}
							
					}
					else
					{
						//do search or
						SortedListPtr SOList = searchOr(indexList, inputList);
						if(SOList->headOfList == NULL)
						{
							printf("No files found\n");
							SLDestroy(SOList);
						}
						else
						{
							printList(SOList);
							SLDestroy(SOList);
						}
					}
					//destroyAllStrings(inputList);
					SLDestroy(inputList);
				}
				//no words given
				else 
				{
					printf("No words added\n");
				}
				//check if it is searchand or searchor

			}
			//not an argument
			else
			{
				printf("Not an argument\n");
			}	
		}
	}
}
Exemplo n.º 17
0
/* 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;
	}
}
Exemplo n.º 18
0
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;
}
Exemplo n.º 19
0
int main()
{
  SortedListPtr list = SLCreate(&compareInts);
  SortedListIteratorPtr iterator, iterator_nulltest, iterator_1, iterator_2;
  int *buffer;
  char *buffer_char;
  int i;
  int  *a, *b, *c, *d, *e;
  char *a_char, *b_char, *c_char, *d_char;
  if (list->head == NULL) { // Make sure list has been created correctly
    fprintf(stdout, "\nList created\n");
  }

  // Setting up {7,5,3} example from announcements
  // Will remove 5, then insert 4
  fprintf(stdout, "\nTest Case 1\n");
  a = (int*)malloc(sizeof(int));
  *a = 7;
  b = (int*)malloc(sizeof(int));
  *b = 5;
  c = (int*)malloc(sizeof(int));
  *c = 3;
  d = (int*)malloc(sizeof(int));
  *d = 4;
  SLInsert(list, a); 
  SLInsert(list, b); 
  SLInsert(list, c); 
  iterator = SLCreateIterator(list);
  fprintf(stdout, "Next Item: %d\n", *(int*)(SLNextItem(iterator)));
  fprintf(stdout, "Next Item: %d\n", *(int*)(SLNextItem(iterator)));
  i = SLRemove(list, b); fprintf(stdout, "Remove: %d ", *b);
  fprintf(stdout, "Remove returns (%d)\n", i);
  SLInsert(list, d); 
  fprintf(stdout, "Next Item: %d\n", *(int*)(SLNextItem(iterator)));
  SLDestroy(list);
  SLDestroyIterator(iterator);
  
  // Setting up {7,5,3} example from announcements
  // Will remove 5, then insert 6
  fprintf(stdout, "\nTest Case 2\n");
  list = SLCreate(&compareInts);
  iterator = SLCreateIterator(list);
  a = (int*)malloc(sizeof(int));
  *a = 7;
  b = (int*)malloc(sizeof(int));
  *b = 5;
  c = (int*)malloc(sizeof(int));
  *c = 3;
  d = (int*)malloc(sizeof(int));
  *d = 6;
  SLInsert(list, a); 
  SLInsert(list, b); 
  SLInsert(list, c); 
  fprintf(stdout, "Next Item: %d\n", *(int*)(SLNextItem(iterator)));
  fprintf(stdout, "Next Item: %d\n", *(int*)(SLNextItem(iterator)));
  i = SLRemove(list, b); fprintf(stdout, "Remove: %d ", *b);
  fprintf(stdout, "Remove returns (%d)\n", i);
  SLInsert(list, d); 
  fprintf(stdout, "Next Item: %d\n", *(int*)(SLNextItem(iterator)));
  SLDestroyIterator(iterator);


  // Removal Test
  // Already removed data, non-existent data, NULL pointer
  fprintf(stdout, "\nTest Case 3\n");
  e = (int*)malloc(sizeof(int));
  *e = 1000;
  i = SLRemove(list, b);
  fprintf(stdout, "Remove returns (%d)\n", i);
  i = SLRemove(list, e);
  fprintf(stdout, "Remove returns (%d)\n", i);
  i = SLRemove(list, NULL);
  fprintf(stdout, "Remove returns (%d)\n", i);

  SLDestroy(list);

  /*
  // Destroying something already destroyed
  fprintf(stdout, "Destruction Testing\n");
  fprintf(stdout, "Second List Destroy...\n");
  //SLDestroy(list);
  fprintf(stdout, "Second Iterator Destroy...\n");
  // SLDestroyIterator(iterator);
  */

  // Setting up to check sorting
  // insert 5, 7, 3, 10
  fprintf(stdout, "\nTest Case 4\n");
  list = SLCreate(&compareInts);
  iterator = SLCreateIterator(list);
  a = (int*)malloc(sizeof(int));
  *a = 1;
  b = (int*)malloc(sizeof(int));
  *b = 20;
  c = (int*)malloc(sizeof(int));
  *c = 17;
  d = (int*)malloc(sizeof(int));
  *d = 34;
  SLInsert(list, a); 
  SLInsert(list, b); 
  SLInsert(list, c); 
  SLInsert(list, d); 
  fprintf(stdout, "Next Item: %d\n", *(int*)(SLNextItem(iterator)));
  fprintf(stdout, "Next Item: %d\n", *(int*)(SLNextItem(iterator)));
  fprintf(stdout, "Next Item: %d\n", *(int*)(SLNextItem(iterator)));
  fprintf(stdout, "Next Item: %d\n", *(int*)(SLNextItem(iterator)));
  SLDestroy(list);
  SLDestroyIterator(iterator);

  // Push Iterator to end of list
  fprintf(stdout, "\nTest Case 5\n");
  list = SLCreate(&compareInts);
  iterator = SLCreateIterator(list);
  a = (int*)malloc(sizeof(int));
  *a = 1;
  b = (int*)malloc(sizeof(int));
  *b = 2;
  c = (int*)malloc(sizeof(int));
  *c = 3;
  d = (int*)malloc(sizeof(int));
  *d = 4;
  SLInsert(list, a); 
  SLInsert(list, b); 
  SLInsert(list, c); 
  SLInsert(list, d); 
  while ( ( buffer = (int*)(SLNextItem(iterator)) ) != NULL ) {
    fprintf(stdout, "Next Item: %d\n", *buffer);
  }
  SLDestroy(list);
  SLDestroyIterator(iterator);

  // Two iterators on single list
  fprintf(stdout, "\nTest Case 6\n");
  list = SLCreate(&compareInts);
  iterator_1 = SLCreateIterator(list);
  iterator_2 = SLCreateIterator(list);
  a = (int*)malloc(sizeof(int));
  *a = 1;
  b = (int*)malloc(sizeof(int));
  *b = 2;
  c = (int*)malloc(sizeof(int));
  *c = 3;
  d = (int*)malloc(sizeof(int));
  *d = 4;
  SLInsert(list, a); 
  SLInsert(list, b); 
  SLInsert(list, c); 
  SLInsert(list, d); 
  for (i = 0; i < 2; i++) {
    buffer = (int*)(SLNextItem(iterator_1));
    fprintf(stdout, "Iterator 1 Next Item: %d\n", *buffer);
  }
  while ( (buffer = (int*)(SLNextItem(iterator_2))) != NULL ) {
    fprintf(stdout, "Iterator 2 Next Item: %d\n", *buffer);
  }  
  while ( (buffer = (int*)(SLNextItem(iterator_1))) != NULL ) {
    fprintf(stdout, "Iterator 1 Next Item: %d\n", *buffer);
  }  
  SLDestroy(list);
  SLDestroyIterator(iterator_1);
  SLDestroyIterator(iterator_2);

  // Using different data types
  fprintf(stdout, "\nTest Case 7\n");
  list = SLCreate(&compareInts);
  iterator = SLCreateIterator(list);
  a_char = (char*)malloc(sizeof(char));
  *a_char = 'a';
  b_char = (char*)malloc(sizeof(char));
  *b_char = 'b';
  c_char = (char*)malloc(sizeof(char));
  *c_char = 'c';
  d_char = (char*)malloc(sizeof(char));
  *d_char = 'd';
  SLInsert(list, a_char); 
  SLInsert(list, b_char); 
  SLInsert(list, c_char); 
  SLInsert(list, d_char); 
  while ( (buffer_char = (char*)(SLNextItem(iterator))) != NULL ) {
    fprintf(stdout, "Next Item: %c\n", *buffer_char);
  }  
  SLDestroy(list);
  SLDestroyIterator(iterator);



  // Attempt to insert duplicate
  fprintf(stdout, "\nTest Case 8\n");
  list = SLCreate(&compareInts);
  iterator = SLCreateIterator(list);
  a = (int*)malloc(sizeof(int));
  *a = 1;
  b = (int*)malloc(sizeof(int));
  *b = 2;
  c = (int*)malloc(sizeof(int));
  *c = 3;
  d = (int*)malloc(sizeof(int));
  *d = 3;
  SLInsert(list, a); //fprintf(stdout, "Insert: %d\n", *a);
  SLInsert(list, b); //fprintf(stdout, "Insert: %d\n", *b);
  SLInsert(list, c); //fprintf(stdout, "Insert: %d\n", *c);
  i = SLInsert(list, d);
  fprintf(stdout, "Insert returns (%d)\n", i);
  while ( (buffer = (int*)(SLNextItem(iterator))) != NULL ) {
    fprintf(stdout, "Next Item: %d\n", *buffer);
  }  


  // Test some NULL pointers
  fprintf(stdout, "\nTest Case 9\n");
  a = (int*)malloc(sizeof(int));
  *a = 1;
  SLInsert(NULL, a);
  i = SLRemove(NULL, a);
  fprintf(stdout, "Remove returns (%d)\n", i);
  i = SLRemove(list, NULL);
  fprintf(stdout, "Remove returns (%d)\n", i);
  iterator_nulltest = SLCreateIterator(NULL);
  if (SLNextItem(iterator_nulltest) == NULL) {
    fprintf(stdout, "NULL Iterator\n");
  }
  return 1;
}
Exemplo n.º 20
0
/* the main method where the program runs from */ 
int main(int argc, char* argv[]){
	
	/* error checking */ 
	if(argc==2 && strcmp(argv[1], "-h")==0){
		printf("You have opened the help menu.\nTo run the program, call the program name with the following parameters: the file to write the results to, and the file or directory to read from.\nIf these are not given, or an invalid directory or file is given, the program will exit.\nThank You\nBye Bye\n"); 
		return 0; 
	}
	else if (argc!=3){
		printf("ERROR: Improper number of parameters. Input the proper number of parameters when you run it again\nBye Bye\n"); 
		return 0; 
	}

	/* declaring a few basic variables and initializing them */ 
	SortedListPtr ptr; 
	ptr = NULL; 
	char input; 
	FILE *file = NULL;

	file = fopen(argv[1], "r"); 

	/* file was not found so a new file with that name is created 
	 * else the file was found and the user options are given to either write it over or quit the program*/
	if (file ==NULL){
		file = fopen(argv[1], "w");
	}
	else {
		printf("\nThe file to write to already exists.\nWhat do you want to do?\n 1 Clear content and write over\n 2 Quit and start all over\nType the number next to the choice that you want:\n"); 
		scanf("%c", &input); 
		if (input =='2'){
			SLDestroy(ptr); 
			fclose(file); 
			return 0; 
		}	
		else if (input =='1'){
			fclose(file); 
			file = fopen(argv[1], "w"); 
		}
		else {
			printf("\nWrong Option. Bye bye\n");
			SLDestroy(ptr); 
			fclose(file); 
			return 0; 
		}
	}

	/* the second paramter is assumed as a directory and the dirtraverse function is called to search */
	/* if NULL is returned then it was not a directory and it assumes it was a file and calls filesearch function */
	ptr = dirtraverse(".", ptr, argv[2], 0, 0); 
	if (ptr==NULL){	
		ptr = filesearch(".", ptr, argv[2], 0);
	}
	
	/* if ptr is still NULL. the 2nd paramter is neither an object nor a file so an error is printed and the program quits */ 
	if (ptr==NULL){
		printf("directory or file to read from not found\nBYE BYE\n");
		fclose(file); 
		return 0; 
	}

	/* the function insertofile is called and given the linked so it can write it to the given file*/ 
	InsertToFile(file,ptr); 

	/* program is done running*/
	printf("GREAT SUCCESS!!!! The program ran successfully! Very Good.\nBYE BYE!\n"); 
	SLDestroy(ptr); 
	fclose(file); 
	return 0; 
}
Exemplo n.º 21
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);
}
Exemplo n.º 22
0
int main(int argc, char** argv)
{
	if(argc != 3){
		printf("Illegal number of arguments\n");
		exit(1);
	}
	else{
		char * outputFileName = argv[1];
		char * inputDirName = argv[2];
		FILE * ifp; //input file sream
		FILE * ofp; //output file stream
		//sl is used as a sort of "sorted keyset" for the hashmap since key values are returned
		//by time added, this makes sure keys are sorted
		SortedListPtr sl = SLCreate(compareStrings, destroyBasicTypeAlloc);
		hashPtr indexHash = NULL;
		char option = '\0';

		if((ifp = fopen(inputDirName,"r+")) == NULL)
			if(errno == EISDIR){
				//if a slash is added to the end of directory remove it
				//only for formatting purposes
				truncateSlash(inputDirName);
				traverseDir(inputDirName, NULL, sl, &indexHash);
			}
			else{
				perror("Error encountered while trying to open file or directory");
				exit(2);
			}
		else{
			readsFile(ifp, inputDirName, sl, &indexHash);
		}

		if((ofp = fopen(outputFileName,"r")) == NULL){ //check if file exists
			if(errno == ENOENT){ //if it doesnt
				fclose(ofp); //close the file opened for reading
				ofp = fopen(outputFileName,"w"); //and create it for writing
				if(SortedWriteToFile(ofp, sl, &indexHash) == -2)
				printf("No words were found.\n");
			}
			else{
				perror("Error trying to open output file");
				exit(2);	
			}
		}
		else{
			printf("File exists, override file (y/n)? "); //if it exists ask user to override
			scanf("%c", &option);
			if(option == 'y'){
				fclose(ofp);
				ofp = fopen(outputFileName,"w");
				if(SortedWriteToFile(ofp, sl, &indexHash) == -2)
					printf("No words were found.\n");
			}
		}
		
		//clean mess
		if(ifp!=NULL)
			fclose(ifp);
		if(ofp!=NULL)
			fclose(ofp);
		clearHash(&indexHash);
		SLDestroy(sl);
	}	

	return 0;
}
Exemplo n.º 23
0
int main(void)
{
  BOOK Book[] =
  {
    {"Expert C Programming", "van der Linden"},
    {"C Programming FAQs", "Summit"},
    {"C++ Programming Language", "Stroustrup"},
    {"Algorithms in C", "Sedgewick"},
    {"Teach Yourself BCB", "Reisdorph"},
    {"The Standard C Library", "Plauger"},
    {"C++ Unleashed", "Liberty"},
    {"Data Structures & Algorithms", "Lafore"},
    {"C Programming Language", "Kernighan & Ritchie"},
    {"Linux Unleashed", "Husain and Parker"},
    {"C Unleashed", "Heathfield & Kirby"},
    {"C : A Reference Manual", "Harbison & Steele"},
    {"DOS Programmers Reference", "Dettmann & Johnson"},
    {"C: How to Program", "Deitel & Deitel"},
    {"Builder Unleashed", "Calvert"},
    {"UNIX Unleashed", "Burk and Horvath"}

  };

  SLLIST *List = NULL;
  SLLIST *Removed = NULL;

  BOOK *Data;

  FIELD_INFO FldInfo = { 30, 30};
  size_t NumBooks = sizeof Book / sizeof Book[0];

  size_t i;

  /* Populate the list */
  for(i = 0; i < NumBooks; i++)
  {
    if(SL_SUCCESS !=
          SLFront(&List, 0, Book + i, sizeof(BOOK)))
    {
      puts("Couldn't allocate enough memory.");
      SLDestroy(&List);
      exit(EXIT_FAILURE);
    }
  }

  /* Print the list */
  SLWalk(List, PrintBook, &FldInfo);

  /* Remove one item */
  Removed = List;

  for(i = 0; i < NumBooks / 2; i++)
  {
    Removed = Removed->Next;
  }

  Data = SLGetData(Removed->Next, NULL, NULL);
  printf("\nRemoving title %s\n\n", Data->Title);
  SLDeleteNext(Removed);

  /* Print the list again to confirm deletion */
  SLWalk(List, PrintBook, &FldInfo);

  /* Destroy the list */
  SLDestroy(&List);

  return 0;
}
Exemplo n.º 24
0
/* the main method where the program runs from */
int main(int argc, char* argv[]) {

    /* error checking */
    if(argc==2 && strcmp(argv[1], "-h")==0) {
        printf("You have opened the help menu.\nTo run the program, call the program name with the following parameter:the file to read from.\nIf these are not given, or an invalid file is given, the program will exit.\nThank You\nBye Bye\n");
        return 0;
    }
    else if (argc!=2) {
        printf("ERROR: Improper number of parameters. Input the proper number of parameters when you run it again\nBye Bye\n");
        return 0;
    }

    /* declaring a few basic variables and initializing them */
    SortedListPtr ptr;
    ptr = NULL;
    char *str;
    str= calloc(1000, sizeof(char));
    FILE *file = NULL;
    int length =0;

    /* searches for the file through the directories*/
    file = fopen(argv[1], "r");

    /* file to read could not be found*/
    if (file ==NULL) {
        printf("ERROR: The file to read from could not be found.\n Bye Bye!\n");
        free(str);
        return 0;
    }

    /* the sorted list is built by calling Addtolist function */
    ptr = AddToList(file,ptr);

    /* if ptr is null, the file could not be read from because it is in the wrong format */
    if (ptr->value==NULL) {
        printf("ERROR: Could not read from the file.\nBYE BYE\n");
        free(str);
        fclose(file);
        return 0;
    }

    /* user interface for the program
     * gives options for "sa" "So" and quit*/
    printf("The file was read successfully.\n");
    for (;;) {
        printf("**************************************************************************\nWhat do you want to do?\nInput \"sa\" or \"so\" followed by the terms you want to search for.\n\"sa\" prints the files that contain all the terms.\n\"so\" prints the files that contains at least one of the terms.\nInput your choice now:\n");
        fgets(str, 1000, stdin);

        /* checks user input*/
        if (str[0]=='s') {
            if (str[1]=='a' && str[2]==' ') {
                sa(ptr,str);
            }
            else if (str[1]=='o' && str[2]==' ') {
                so(ptr, str);
            }
        }
        else if (str[0]=='q') {
            printf("The program will now quit\nBye Bye!\n");
            break;
        }
        else {
            printf("This input is not allowed.\nEnter proper input please.\n");
            continue;
        }
    }

    /* program is done running*/
    SLDestroy(ptr);
    fclose(file);
    free(str);
    return 0;
}
Exemplo n.º 25
0
int main()
{
	Employee *a=(Employee*)malloc(sizeof(Employee));
	a->name="Albert";
	a->salary=48000;

	Employee *b=(Employee*)malloc(sizeof(Employee));
	b->name="Bob";
	b->salary=47000;
	
	Employee *c=(Employee*)malloc(sizeof(Employee));
	c->name="Carter";
	c->salary=46000;
	
	Employee *d=(Employee*)malloc(sizeof(Employee));
	d->name="Dudley";
	d->salary=45000;

	//////////////////////// INITIALISING THE SORTED LIST ///////////////////////////
	SortedListPtr ptr=SLCreate(&compareEmployees, &destroyEmployee);
	printf("Creating SortedList...\n");
	if(SLInsert(ptr, (void *)b))
		printf("Inserted employee with name %s and salary %d \n",b->name,b->salary);
	if(SLInsert(ptr, (void *)a))
		printf("Inserted employee with name %s and salary %d \n",a->name,a->salary);
	if(SLInsert(ptr, (void *)c))
		printf("Inserted employee with name %s and salary %d \n",c->name,c->salary);
	if(SLInsert(ptr, (void *)d))
		printf("Inserted employee with name %s and salary %d \n",d->name,d->salary);
	printf("Finished inserting.\n\n");

	Employee* firstEmp=(Employee*)(ptr->head->data);
  	printf("%s is the first employee in the sorted list. \n\n",firstEmp->name);
  	
  	////////////////////// CREATING ITERATORS //////////////////////////////////////
  	printf("Creating iterator 1 and 2\n");
	SortedListIteratorPtr walker = SLCreateIterator(ptr);
	SortedListIteratorPtr walker2 = SLCreateIterator(ptr);


	printf("Testing SLGetItem for Iterator 1: %s \n", ((Employee*)(SLGetItem(walker)))->name);

	//the walker1 will traverse through the list to the last element
	while(walker->current->next!=NULL){
		SLNextItem(walker);	
		printf("Testing Iterator 1 NextItem: %s \n\n", ((Employee *)(SLGetItem(walker)))->name);
	}

	///////////////// OPERATIONS ON SORTED LISTS //////////////////////////
	printf("Attempting to remove Bob from the list.\n");
	if(SLRemove(ptr, (void *)b))
		printf("Element removed successfully.\n");
	
	//trying to remove from list when iterator points to it
	printf("Attempting to remove Dudley from the SortedList (iterator 1 points to it).\n");
	SLRemove(ptr, (void *)d);
	
  	
  	//destroyed the list but an iterator still points to one of the elements.
  	printf("Attempting to destroy the list. (Note that iterators 1 and 2 are still active.\n\n");
  	SLDestroy(ptr);
  	// testing iterators
  	printf("Iterator 1 is pointing to: %s \n", ((Employee *)(SLGetItem(walker)))->name);
	printf("Iterator 2 is pointing to: %s\n",((Employee *)(SLGetItem(walker2)))->name );
	printf("CHecking next element for each iterator\n");
	if(SLNextItem(walker)==NULL)
		printf("Iterator 1 reaches null\n");
	if(SLNextItem(walker2)==NULL)
		printf("Iterator 2 reaches null\n");
	
	SLDestroyIterator(walker);
	SLDestroyIterator(walker2);

  	return 0;
}