/** Question 1 **/ int Institution_reverse(LinkedList *inputList, LinkedList *outputList){ int i; Institution *tempInstitution; int counter =0 ; if(inputList->head ==NULL) { return 0; } while ((tempInstitution = (Institution*)List_removeHead(inputList)) !=NULL) { //push Element 1 Stack_push(&stack,tempInstitution); counter ++; } for(i=0;i<counter;i++) { tempInstitution = Stack_pop(&stack); List_addTail(outputList,tempInstitution); } return counter; }
int Institution_select(LinkedList *inputList, LinkedList *outputList, void *criterion, int(*compare)(void *, void*)){ int i; Institution *tempInstitution; int counter = 0; if(inputList->head==NULL) { return 0; } //Look for institution which is the particular type while((tempInstitution = (Institution*)List_removeHead(inputList)) !=NULL) { //Compare the institution with the institution type if(compare(tempInstitution,criterion)) { Stack_push(&stack,tempInstitution); counter++; } } for(i=0;i<counter;i++) { tempInstitution=Stack_pop(&stack); List_addTail(outputList,tempInstitution); } return counter; }
/** * Reverse the order of element * Input : * inputList is a list of institution * outputList is a list of institution in reversed order * * Return : * the number of element reversed */ int Institution_reverse(LinkedList *inputList,LinkedList *outputList) { int counter = 0, firstCount = 1 ; Institution *input ,*output ; Stack *newStack = Stack_create(); do { input = (Institution *)List_removeHead(inputList); if(firstCount == 1 && input == NULL) { outputList = NULL ; return 0; } else firstCount = 0 ; if(input!= NULL) Stack_push(newStack,input); }while (input != NULL); do { output = (Institution *)Stack_pop(newStack); if(output !=NULL) { List_addTail(outputList,output); counter ++ ; } }while (output != NULL); return counter ; }
/** * Reverse the order of element * Input: * inputList is a list of institutions * outputList is a list of selected institutions * criterion is a pointer to the criterion for selection * Return: * the number of element selected */ int Institution_select(LinkedList *inputList, LinkedList *outputList, void *criterion, int (*compare)(void *, void *)) { Institution *institute; int result = 0; int selectedInstitute = 0; do { institute = List_removeHead(inputList); if(institute != NULL) result = compare(institute, criterion); if(result == 1 && institute != NULL) { List_addTail(outputList, institute); printf("Name : %s \n", institute->name); printf("yearEstablished: %d \n", institute->yearEstablished); selectedInstitute++; } else printf("Address : %p \n", institute); }while(institute != NULL); return selectedInstitute++; }
/** * Reverse the order of element * Input: * inputList is a list of institutions * outputList is a list of institutions in reversed order * Return: * the number of element reversed */ int Institution_reverse(LinkedList *inputList, LinkedList *outputList) { Institution *institute; Stack *stack = Stack_create(); do { institute = List_removeHead(inputList); if(institute != NULL) { //printf("Name1 : %s \n", institute->name); Stack_push(stack, institute); } else printf("Address : %p \n", institute); }while(institute != NULL); do { institute = Stack_pop(stack); if(institute != NULL) { //printf("Name : %s \n", institute->name); List_addTail(outputList, institute); } else printf("Address : %p \n", institute); }while(institute != NULL); }
// Remove one element from a set. Poly_t Set_removeOne (T set) { Assert_ASSERT(set); if (Set_isEmpty (set)) Error_impossible (); return List_removeHead (set->list); }
/** * Select only institution of a particular type * Input : * inputList is a list of institution * outputList is a list of selected institutions * criterion is a pointer of the criterion of selection * * Return : * the number of element reversed */ int Institution_select(LinkedList *inputList,LinkedList *outputList,void *criterion, int (*compare)(void *,void *)) { Institution *head ; int selected = 0 ,result; do { head = (Institution *)List_removeHead(inputList); if (head !=NULL) // will cause bad memory without this statement result = compare(head,criterion); if (result == 1 && head !=NULL) { List_addTail(outputList,head); selected ++ ; } }while( head != NULL); return selected; }