예제 #1
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;
    
}
예제 #2
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;

}