Пример #1
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");
}
Пример #2
0
void myFree(void * p, char * fn, int ln){
	memEntry *ptr;
	memEntry *before;	//memEntry for the previous block
	memEntry *after;	//memEntry for the next block
    
	if(sl == NULL) {
		printf(KRED "Error: No malloced/calloced memory in the list. Failed to free.\n" KNRM);
		return;
	} if(SLFind(sl, p) == NULL) {
		printf(KRED "Error: This memory pointer was never malloced/calloced in the list. Failed to free.\n" KNRM);
		return;
	}
    
	ptr = (memEntry *)((char *)p - sizeof(memEntry));

	if((before = ptr->prev) != 0 && before->isFree){
		before->size += sizeof(memEntry) + ptr->size;	// Combining with the previous memEntry free block
		before->next  = 	ptr->next;
		ptr->isFree   = 		1;
		before->next  = 	ptr->next;
		if(ptr->next != 0){
			ptr->next->prev = before;
		}

		SLRemove(sl, p);
	} else {   
		if (ptr->isFree == 0) {
		    SLRemove(sl, p);
		    ptr->isFree = 1;
		    before = ptr;
		    printf(KGRN "Freed block 0x%x\n" KNRM, p);
		} else {
			printf(KRED "Error: Attempted to double free a pointer. Failed to free.\n" KNRM);
		}
	}
	if((after = ptr->next) != 0 && after->isFree){
		before->size += sizeof(memEntry) + after->size;	// // Merging with the next memEntry free block
		before->next = after->next;
		before->isFree = 1;
		if(after->next != 0) {
			after->next->prev = before;
		}
		
		SLRemove(sl, p);
	}
}
Пример #3
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;
}
Пример #4
0
int SLInsert(SortedListPtr list, void *newObj, char*key)
{
	if(list->head == NULL)
	{
		NodePtr newNode = NodeCreate(newObj, key);
		list->head = newNode;
		list->head->refcount += 1;
		
			

		return 1;
	}
	else if(list->head != NULL)
	{


		NodePtr cur = list->head;
		NodePtr prev = NULL;
        int comp = 0;
         comp = (int)strcmp(cur->name, key);
         while (cur->next!=NULL && comp!=0) {
              prev = cur;
              cur = cur->next;
              printf("Entered: %s\n", prev->name);
         if(cur->name==NULL){
	comp = -1;  break;}
	     comp = (int)strcmp(cur->name, key);
            
          }		
        
        if (comp==0) {
           int * y = (int*) malloc(sizeof(int));
            *y = *((int*)cur->data) + 1;
            
           
           
		SLRemove(list, key);	
           Insert(list, (void*) y , key);
cur = list->head;            
return 1;
        }
        else{
          
            NodePtr nu = NodeCreate(newObj, key);
printf("cur: %s\n", cur->name);            
if(cur->name==NULL){
cur = NULL; prev->next = NULL;};
Insert(list, newObj, key);
        }


    }

    return 1;
}
Пример #5
0
/*
	process Token will add the (token, filename) tuple to the TokenList object.
		Duplicate (token, filename) will increment frequency.
		New tuples will create new objects to be added to the nested sorted-lists.

	@param word 	: Token to be added
	@param filename : Filename associated with word
*/
void processToken(char* word, char* filename)
{
	Token 		*searchToken;
	fileRecord 	*searchRecord;
	Token   	*newToken 	= malloc(sizeof(Token));
	fileRecord  *newRecord 	= malloc(sizeof(fileRecord));
	
	if (newRecord == NULL || newToken == NULL) {
		fprintf(stderr, "errno %d\n", errno);
		fprintf(stderr, "mesg: %s\n", strerror(errno));
		exit(-1);
	}

	newToken->word    	 	= word;
	newToken->fileList   	= NULL;
	newRecord->filename 	= copyFileName(filename);
	newRecord->frequency	= 1;

	if( (searchToken = SLSearch(TokenList, newToken, compareRecordName)) != NULL ){

		if( (searchRecord = SLSearch(searchToken->fileList, newRecord, compareRecordName)) != NULL ){
			
			int freq = searchRecord->frequency;
			newRecord->frequency = freq+1;
			SLInsert(searchToken->fileList, newRecord);
			SLRemove(searchToken->fileList, searchRecord);			

		}
		else{
			SLInsert(searchToken->fileList, newRecord);
		}
		destroyTokenStruct(newToken);
	}
	else{
		newToken->fileList = SLCreate(compareRecordStructs, destroyFileRecord);
		SLInsert(newToken->fileList, newRecord);
		SLInsert(TokenList, newToken);
	}

}
Пример #6
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;
}
Пример #7
0
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;
    
}
Пример #8
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);
}
Пример #9
0
int main(void)
{
//TEST CASE 1 WITH CHARACTERS
  SortedListPtr si =SLCreate(compareStrings, destroy);
  
  SLInsert(si, "z");
  SLInsert(si, "a");
  SLInsert(si, "a");
  SLInsert(si, "b");
  SLInsert(si, "x");
  printf("\n=-=-=-=-=-=-=- Testing Characters =-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
  printf("Completed inserting characters into list\n");
// SLRemove(si, "x");
    
  Node *ptr;
  SortedListIteratorPtr it =  SLCreateIterator(si);
  
  //iterator has been created, begin to iterate through the list and print out the values
  //we use the SLGetItem to return the current item 
  ptr =SLGetItem(it);	  
  while(ptr != NULL)
  {
	  printf("\n%s", ptr->data);
	  ptr = SLNextItem(it);  //uses the SLNextItem to move the pointer to the next item to continue iterating
  }
  printf("\nCompleted interating through the list\n");
  
  //The iterator gets destroyed 
  SLDestroyIterator(it); 
  printf("Iterator has been destroyed\n");
  
  //the list has not been affected since the iterator got destroyed
  printf("Destroyed iterator does not affect list by printing out values from list\n");
  printf("First item in the list: %s\n", si->head->data); //prints out the item in the list where head points to
  printf("Second item in the list: %s\n", si->head->next->data); //prints out the item after head
  printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");	
//+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+	


//TEST CASE 2 WITH INTEGERS 
 
 	int arr[]= {2,3,10,8,4,11,21,8,1,7,16};
	 
	int length = sizeof(arr)/sizeof(arr[0]);
	
	int i;
	
	SortedListPtr si2 =SLCreate(compareInts, destroy);
	int *intPtr;
	
	int x = 100;              //
	int *insrt;               //   to insert a number 100
	insrt = &x;              //    after iterated through list
 printf("\n=-=-=-=-=-=-=-=-=-Testing Integers=-=-=-=-=-=-=-=-=-=-=-=\n");
 
  //inserts numbers from arrray into linked list 
	for(i = 0; i < length;i++)
	{
		intPtr = &arr[i];
		SLInsert(si2,intPtr);
	}
  printf("Finished inserting numbers into linked list\n");
  	
  Node* ptr2;
  SortedListIteratorPtr it2 =  SLCreateIterator(si2);
  ptr2 = SLGetItem(it2);
 
  //iterator traverses through the list
  while(ptr2 != NULL)
  {   
      printf("%d\n", (*(int*)(ptr2->data)) );
  	  ptr2 = SLNextItem(it2);
  } 
  printf("Finished traversing through linked list\n");  
  
  SLInsert(si2, insrt);
  printf("Number 100 has been inserted into the list\n");
  
  //destroys iterator
  SLDestroyIterator(it2);
  printf("Iterator has been destroyed\n");
  
  //list has not been affectedby iterator by printing values in list
  printf("First value in list: %d\n",  (*(int*)(si2->head->data))   );
  printf("Second value in list: %d\n",  (*(int*)(si2->head->next->data))   );
  
  
  //remove item from list
 SLRemove(si2, insrt);
 printf("item containing 100 has been deleted from list\n");
 printf("First item after deletion:%d\n",  (*(int*)(si2->head->data)) );  
 printf("Second item after deletion:%d\n",  (*(int*)(si2->head->next->data)) );  
 printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
 
 //TESTING FLOATS--------------------------
float arrDbl[]= {2.2,3.6,10.9,8.3,4.7,11.3,21.7,8.1,1.8,7.2,16.5};
	 
	int size = sizeof(arrDbl)/sizeof(arrDbl[0]);
	
	int count;
	
	SortedListPtr si3 =SLCreate(compareDoubles, destroy);
	float *dblPtr;
	
	float var = 170.5;              
	float *insert;             
	insert = &var;           
 printf("\n=-=-=-=-=-=-=-=-=-Testing Floats=-=-=-=-=-=-=-=-=-=-=-=\n");
 
  //inserts numbers from arrray into linked list 
	for(count = 0; count < size;count++)
	{
		dblPtr = &arrDbl[count];
		SLInsert(si3,dblPtr);
	}
  printf("Finished inserting numbers into linked list\n");
  	
  Node* ptr3;
  SortedListIteratorPtr it3 =  SLCreateIterator(si3);
  ptr3 = SLGetItem(it3);
 
  //iterator traverses through the list
  while(ptr3 != NULL)
  {   
      printf("%f\n", (*(float*)(ptr3->data)) );
  	  ptr3 = SLNextItem(it3);
  } 
  printf("Finished traversing through linked list\n");  
  
  SLInsert(si3, insert);
  printf("Number 170.5 has been inserted into the list\n");
  
  //destroys iterator
  SLDestroyIterator(it3);
  printf("Iterator has been destroyed\n");
  
  //list has not been affectedby iterator by printing values in list
  printf("First value in list: %f\n",  (*(float*)(si3->head->data))   );
  printf("Second value in list: %f\n",  (*(float*)(si3->head->next->data))   );
  
  
  //remove item from list
 SLRemove(si3, insert);
 printf("item containing 100 has been deleted from list\n");
 printf("First item after deletion:%f\n",  (*(float*)(si3->head->data)) );  
 printf("Second item after deletion:%f\n",  (*(float*)(si3->head->next->data)) );  
 printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
 
	return 0;
}
Пример #10
0
int main(int argc, char *argv[]){
	int z = argc;
	if(z==1 || z==0 || z>2){
		printf("too few or many args");
			}
	char *qw = argv[1];					/*argv will pick the option*/
	
	if(strcmp(qw,"1")==0){						/*option 1 is int test*/
	int a = 5;
	int b = 10;
	int c = 7;
	int d = 12;
	int e = 2;
	int f = 9;
	int g = 9;
	int h = 23;
	int i = 50;
	CompareFuncT com = &compareInt;
	DestructFuncT des = &Destroy;
	SortedListPtr s1 = SLCreate(com,des);
	SLInsert(s1,(void*)&a);
	SLInsert(s1,(void*)&b);
	SLInsert(s1,(void*)&c);
	SLInsert(s1,(void*)&d);
	SLInsert(s1,(void*)&e);
	SLInsert(s1,(void*)&f);
	SLInsert(s1,(void*)&g);
	SLInsert(s1,(void*)&h);
	SLInsert(s1,(void*)&i);
	SLRemove(s1,(void*)&e);
	SLRemove(s1,(void*)&d);
	SLRemove(s1,(void*)&h);
	
	while(s1->head!=NULL){
		printf("%d\n",*(int*)s1->head->data);
		s1->head=s1->head->next;
			}
		}

	if(strcmp(qw,"2")==0){						/*option 2 is float test*/
		float ab = 5.21;
		float bc = 10.4;
		float cd = 7.2;
		float de = 12.6;
		float ef = 2.8;
	
	CompareFuncT com = &compareFloat;
	DestructFuncT des = &Destroy;
	SortedListPtr s2 = SLCreate(com,des);
	SLInsert(s2,(void*)&ab);
	SLInsert(s2,(void*)&bc);
	SLInsert(s2,(void*)&cd);
	SLInsert(s2,(void*)&de);
	SLInsert(s2,(void*)&ef);
	SLRemove(s2,(void*)&de);
	SLRemove(s2,(void*)&bc);
	
	while(s2->head!=NULL){
		printf("%.4f\n",*(float*)s2->head->data);
		s2->head=s2->head->next;
			}
		}
	
	

	if(strcmp(qw,"3")==0){						/*option 2 is float test*/
	char *ab = "hello";
	char *bc = "good";
	char *cd = "car";
	char *de = "bus";
	char *ef = "ocean";
	
	CompareFuncT com = &compareString;
	DestructFuncT des = &Destroy;
	SortedListPtr s1 = SLCreate(com,des);
	SLInsert(s1,(void*)ab);
	SLInsert(s1,(void*)bc);
	SLInsert(s1,(void*)cd);
	SLInsert(s1,(void*)de);
	SLInsert(s1,(void*)ef);
	SLRemove(s1,(void*)de);
	SLRemove(s1,(void*)bc);
	
	while(s1->head!=NULL){
		printf("%s\n",(char*)s1->head->data);
		s1->head=s1->head->next;
			}
		}


	if(strcmp(qw,"4")==0){						/*option 2 is float test*/
	char a = 'a';
	char b = 'b';
	char c = 'i';
	char d = 'h';
	char e = 'e';
	
	CompareFuncT com = &compareString;
	DestructFuncT des = &Destroy;
	SortedListPtr s1 = SLCreate(com,des);
	SLInsert(s1,(void*)&a);
	SLInsert(s1,(void*)&b);
	SLInsert(s1,(void*)&c);
	SLInsert(s1,(void*)&d);
	SLInsert(s1,(void*)&e);
	SLRemove(s1,(void*)&d);
	SLRemove(s1,(void*)&b);
	
	while(s1->head!=NULL){
		printf("%c\n",*(char*)s1->head->data);
		s1->head=s1->head->next;
			}
		}
	
	return 0;
		}
Пример #11
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;
}
Пример #12
0
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);
        int iarr[6] = {7,5,155,42,-1,6};
        if(populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,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);

        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) == 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);

        char *carr[6] = {"cat","dog","catamaran","apple","cormorant","zebra"};
        if(populate_list(s,STRING,6,0,0,carr) == 0){
            failure();
            return 1;
        }

        SortedListIteratorPtr iter = SLCreateIterator(s);
        iterprint_all(s,iter,STRING); //prints apple cat catamaran cormorant dog zebra



    }else if(choice == 4){
        //SLInsert on null list;
        int x = 5;
        if(SLInsert(NULL, &x) == 0){
            success();
        }else{
            failure();
        }
    }else if(choice == 5){
        //SLInsert null object
        SortedListPtr s = SLCreate(compareDoubles);

        if (SLInsert(s,NULL) == 0){
            success();
        }else{
            failure();
        }

    }else if(choice == 6){
        //SLRemove nonexistent element
        SortedListPtr s = SLCreate(compareInts);
        int iarr[6] = {7,5,155,42,-1,6};
        populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0);

        int toremove = 54;
        if (SLRemove(s,&toremove) != 0) {
            failure();
        }else{
            success();
        }
    }else if(choice == 7){
        //SLRemove existing element (no duplicates; didn't handle them anyway)
        SortedListPtr s = SLCreate(compareInts);
        int iarr[6] = {7,5,155,42,-1,6};
        populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0);


        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 == 8){
        //Iterate on empty list
        SortedListPtr s = SLCreate(compareInts);
        SortedListIteratorPtr iter = SLCreateIterator(s);
        iterprint_all_int(s,iter);
        //TODO: Separate into a) create iterator b) print empty list
    }else if(choice == 9){
        //Create new iterator on list, destroy old one mid-iteration
        SortedListPtr s = SLCreate(compareInts);
        int iarr[6] = {7,5,155,42,-1,6};
        populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0);



        SortedListIteratorPtr olditer = SLCreateIterator(s);
        void *olditem = SLNextItem(olditer);
        olditem = SLNextItem(olditer);
        SortedListIteratorPtr iter = SLCreateIterator(s);
        SLDestroyIterator(olditer);

        iterprint_all_int(s,iter); //prints 155 42 7 6 5 -1
    }else if(choice == 10){ //TODO
        //Create multiple iterators on same list, interleave iterations.

        SortedListPtr s = SLCreate(compareInts);
        int iarr[6] = {7,5,155,42,-1,6};
        populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0);

        SortedListIteratorPtr olditer = SLCreateIterator(s);

        SortedListIteratorPtr iter = SLCreateIterator(s);


        void *item;

        item = SLNextItem(olditer);
        printf("%d ", *((int*)item));
        item = SLNextItem(olditer);
        printf("%d ", *((int*)item));  //prints 155 42

        item = SLNextItem(iter);
        printf("%d ", *((int*)item));
        item = SLNextItem(iter);
        printf("%d ", *((int*)item));
        item = SLNextItem(iter);
        printf("%d ", *((int*)item)); //prints 155 42 7


        item = SLNextItem(iter);  //prints 6
        printf("%d ", *((int*)item));
        item = SLNextItem(olditer);
        printf("%d ", *((int*)item)); //prints 7


        iterprint_all_int(s,iter); //prints 5 -1

        iterprint_all_int(s,olditer); //prints 6 5 -1

    }else if(choice == 11){
        //SLRemove end element, iterator positioned on it

        SortedListPtr s = SLCreate(compareInts);
        int iarr[3] = {7,5,3};
        populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0);


        SortedListIteratorPtr iter = SLCreateIterator(s);

        int x3 = 3;
        void *item = SLNextItem(iter);
        item = SLNextItem(iter);
        if(SLRemove(s,&x3) && SLNextItem(iter) == NULL){
            success();
        }else{
            failure();
        }

    }else if(choice == 12){ //TODO
        //SLRemove beginning element, iterator positioned on it

        SortedListPtr s = SLCreate(compareInts);
        int iarr[3] = {7,5,3};
        populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0);

        SortedListIteratorPtr iter = SLCreateIterator(s);

        int x1 = 7;
        if(SLRemove(s,&x1) && *((int*)SLNextItem(iter)) == 5 &&
           *((int*)SLNextItem(iter)) == 3 &&
           ((int*)SLNextItem(iter)) == NULL){
            success();
        }else{
            failure();
        }
    }else if(choice == 13){ //TODO
        //SLRemove element in middle, iterator positioned on it
        SortedListPtr s = SLCreate(compareInts);
        int iarr[3] = {7,5,3};
        populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0);

        SortedListIteratorPtr iter = SLCreateIterator(s);
        void *item = SLNextItem(iter);

        if (*((int*)item) != 7) {
            failure();
            return 1;
        }

        int x1 = 5;
        if(SLRemove(s,&x1) && *((int*)SLNextItem(iter)) == 3 &&
           ((int*)SLNextItem(iter)) == NULL){
            success();
        }else{
            failure();
        }
    }else if(choice == 14){
        //Add element after iterator
        SortedListPtr s = SLCreate(compareInts);
        int iarr[3] = {7,5,3};
        populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0);

        SortedListIteratorPtr iter = SLCreateIterator(s);
        void *item = SLNextItem(iter);
        item = SLNextItem(iter);
        int x4 = 4;
        SLInsert(s,&x4); //prints 4 3

        iterprint_all_int(s,iter);


    }else if(choice == 15){
        //Remove element after iterator
        SortedListPtr s = SLCreate(compareInts);
        int iarr[4] = {7,5,3,4};
        populate_list(s,INT,sizeof(iarr)/sizeof(int),iarr,0,0);


        SortedListIteratorPtr iter = SLCreateIterator(s);
        void *item = SLNextItem(iter);
        item = SLNextItem(iter);
        int x4 = 4;
        if(SLRemove(s,&x4)){
            iterprint_all_int(s,iter); //prints 3
        }else{
            failure();
        }
    }else{
        printf("Bad input\n");
    }


    return 0;

}
Пример #13
0
int main()
{
    SortedListPtr a;
    a = SLCreate(compareStrings);
    SLInsert(a, "ccc");
    SLInsert(a, "ddd");
    SLInsert(a, "aaa");
    SLInsert(a, "bbb");

    SortedListIteratorPtr iter = SLCreateIterator(a);
    SortedListIteratorPtr iter1 = SLCreateIterator(a);
    SortedListIteratorPtr iter2 = SLCreateIterator(a);

    void* ab = SLNextItem(iter);
    printf("--------%s--------", (char*)ab);

    ab = SLNextItem(iter1);
    ab = SLNextItem(iter1);
    printf("--------%s--------", (char*)ab);

    ab = SLNextItem(iter2);
    ab = SLNextItem(iter2);
    ab = SLNextItem(iter2);
    printf("--------%s--------", (char*)ab);
    printf("\n");

    SLRemove("bbb", a);

    ab = SLNextItem(iter);
    printf("--------%s--------", (char*)ab);


    ab = SLNextItem(iter1);
    printf("--------%s--------", (char*)ab);

    ab = SLNextItem(iter2);
    printf("--------%s--------", (char*)ab);

    printf("\n");

    ab = SLNextItem(iter);
    printf("--------%s--------", (char*)ab);


    ab = SLNextItem(iter1);
    printf("--------%s--------", (char*)ab);

    ab = SLNextItem(iter2);
    printf("--------%s--------", (char*)ab);

    printf("\n");

    ab = SLNextItem(iter);
    printf("--------%s--------", (char*)ab);


    ab = SLNextItem(iter1);
    printf("--------%s--------", (char*)ab);

    ab = SLNextItem(iter2);
    printf("--------%s--------", (char*)ab);


}
Пример #14
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;
}
Пример #15
0
int main(int argc, char *argv[])
{
	// Lists
	SortedListPtr integers = SLCreate(compareIntegers, destroyList);
	SortedListPtr doubles = SLCreate(compareDoubles, destroyList);
	SortedListPtr characters = SLCreate(compareStrings, destroyList);
	SortedListPtr strings = SLCreate(compareStrings, destroyList);
	
	// Insertions
	int a = 7, b = 7, c = 8, d = 1;
	double h = 22.3, i = 98.2, j = 1.2;
	char s = 's', w = 'a', v = 'c';
	char word1[10] = "Hello";
	char word2[10] = "World";

	
	// ========================== Case 1 ========================== 
	printf("\nIntegers:\n");
	SLInsert(integers, (void*)&a);
	SLInsert(integers, (void*)&b);
	SLInsert(integers, (void*)&c);
	SLInsert(integers, (void*)&d);

	SortedListIteratorPtr itr1 = SLCreateIterator(integers);
	printIntegers(itr1);
	printf("\n");

	SLRemove(integers, (void*)&c);

	SortedListIteratorPtr itr2 = SLCreateIterator(integers);
	printIntegers(itr2);
	printf("\n");

	// ========================== Case 2 ========================== 
	printf("\nDoubles:\n");
	SLInsert(doubles, (void*)&h);
	SLInsert(doubles, (void*)&i);
	SLInsert(doubles, (void*)&j);

	SortedListIteratorPtr itr3 = SLCreateIterator(doubles);
	printDoubles(itr3);
	printf("\n");
	
	SLRemove(doubles, (void*)&h);

	SortedListIteratorPtr itr4 = SLCreateIterator(doubles);
	printDoubles(itr4);
	printf("\n");

	// ========================== Case 3 ========================== 
	printf("\nCharacters:\n");
	SLInsert(characters, (void*)&s);
	SLInsert(characters, (void*)&w);
	SLInsert(characters, (void*)&v);

	SortedListIteratorPtr itr5 = SLCreateIterator(characters);
	printChars(itr5);
	printf("\n");
	
	SLRemove(characters, (void*)&v);
	SLRemove(characters, (void*)&s);

	SortedListIteratorPtr itr6 = SLCreateIterator(characters);
	printChars(itr6);
	printf("\n");

	// ========================== Case 4 ========================== 
	printf("\nStrings:\n");
	SLInsert(strings, (void*)word1);
	SLInsert(strings, (void*)word2);

	SortedListIteratorPtr itr7 = SLCreateIterator(strings);
	printStrings(itr7);
	printf("\n");
	
	SLRemove(strings, (void*)word1);

	SortedListIteratorPtr itr8 = SLCreateIterator(strings);
	printStrings(itr8);
	printf("\n\n");

	return 0;
}
Пример #16
0
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;
    
}