// File: gqmain2.C // // Testing generic queues implemented by the GenQ class. #include #include #include #include #include "genq.h" // Local function prototypes // int cmp_int(void *, void *) ; void prnt_int(void *) ; int cmp_str(void *, void *) ; void prnt_str(void *) ; // Local function definitions // int cmp_int(void *vptr1, void *vptr2) { int *ptr1, *ptr2 ; ptr1 = (int *) vptr1 ; ptr2 = (int *) vptr2 ; if (ptr1 == NULL && ptr2 == NULL) return 0 ; if (ptr1 == NULL) return -1 ; if (ptr2 == NULL) return 1 ; if (*ptr1 < *ptr2) return -1 ; if (*ptr1 == *ptr2) return 0 ; return 1 ; } void prnt_int(void *vptr) { int *ptr ; if (vptr == NULL) return ; ptr = (int *) vptr ; cout << *ptr << " " ; } int cmp_str(void *vptr1, void *vptr2) { char *ptr1, *ptr2 ; ptr1 = (char *) vptr1 ; ptr2 = (char *) vptr2 ; if (ptr1 == NULL && ptr2 == NULL) return 0 ; if (ptr1 == NULL) return -1 ; if (ptr2 == NULL) return 1 ; return strcmp(ptr1, ptr2) ; } void prnt_str(void *vptr) { if (vptr == NULL) return ; cout << "\"" << (char *) vptr << "\"\n" ; } main() { GenQ L(cmp_int, prnt_int) ; GenQ S(cmp_str, prnt_str) ; int *ptr, length ; L.enqueue(new int(5)) ; L.enqueue(new int(7)) ; L.enqueue(new int(13)) ; L.enqueue(new int(12)) ; L.enqueue(new int(11)) ; L.print() ; cout << endl ; length = L.length() ; cout << "length = " << length << endl ; cout << endl ; S.enqueue(strdup("Space")) ; S.enqueue(strdup("the")) ; S.enqueue(strdup("final")) ; S.enqueue(strdup("frontier")) ; S.enqueue(strdup("These")) ; S.enqueue(strdup("are")) ; S.enqueue(strdup("the")) ; S.enqueue(strdup("voyages")) ; S.enqueue(strdup("of")) ; S.enqueue(strdup("the")) ; S.enqueue(strdup("starship")) ; S.enqueue(strdup("Enterprise")) ; S.print() ; cout << endl ; length = S.length() ; cout << "length = " << length << endl ; cout << endl ; S.remove("the") ; S.print() ; cout << endl ; }