Thread: How do I do this??

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    63

    How do I do this??

    Hey guys,
    I have a compare function which receives at different times either first name, last name and maybe even both? Initially when wrote my lab it was only for the first name..how do I modify the code to accomodate for both first name or last name..(first name and last name are fields in the KEY structure of a linked list)..any ideas?

    strcture KEY contains an array for first names and another array for last names..

    This was my initial code..how do I modify it?
    Code:
    int cmp(KEY k, KEY k2)
    {
    	int cmp = 0;
    	cmp = strcmp(k.names, k2.names);
    	return cmp; 
    }
    and this is my search function that gets first name OR last name OR both..

    Code:
    int searchList(NODE *pList, 
    			   NODE **pCur,
    			   NODE **pPre,
    			   KEY k)
    
    {
    	int found = 0;
    	// initialize
    	*pPre = NULL;
    	*pCur = pList;
    
    	//Start search
    	while(*pCur != NULL && (cmp(k, (*pCur)->directory.k) > 0))
    	{
    		*pPre = *pCur;
    		*pCur = (*pCur)-> next;
    	}
    
    	if(*pCur && (cmp(k, (*pCur)->directory.k) == 0))
    	{
    		found = 1;
    	}
    
    	return found; 
    }
    Currently, this search function works just fine if I want to look up the directory using first names..need to modify it so that I can search using last names as well. Any ideas?

    The main idea behind the code is that it should be generic. Thats what the purpose of the project is: to recycle code..
    Thanks
    A

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Using function pointers would be one way - see the standard function qsort for the general idea

    Code:
    int cmp_first (KEY k, KEY k2) {
      return strcmp(k.names, k2.names);
    }
    int cmp_last (KEY k, KEY k2) {
    //  your new code for comparing last names
    }
    int cmp_both (KEY k, KEY k2) {
    //  your new code for comparing both names
    }
    int searchList ( NODE *pList, NODE **pCur, NODE **pPre,
      KEY k,  int (*cmp)(KEY,KEY) ) {
    // your existing code
    }
    Then you would just choose which compare function you want to use, when you call searchList
    searchList ( ...., cmp_first );

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    hi Salem,
    do you wanna explain that a little bit more..please?
    A

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    would my calling statement look like this cos it causes a lot of errors.
    temp = searchList(pList, &pCur, &pPre, k2, (*cmp)(KEY,KEY));
    ??

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by aspand
    would my calling statement look like this cos it causes a lot of errors.
    temp = searchList(pList, &pCur, &pPre, k2, (*cmp)(KEY,KEY));
    ??
    If you want search on the first name you call:
    Code:
    temp = searchList(pList, &pCur, &pPre, k2, cmp_first);
    If you want to search on the last name you call:
    Code:
    temp = searchList(pList, &pCur, &pPre, k2, cmp_last);

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    ah..gotcha Monster..though I am still a lil fuzzy over the prototype declaration for the same..
    should it be (int)(cmp*)(KEY, KEY)? aren't I just calling one function then?? or would I change it accordingly?

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by aspand
    ah..gotcha Monster..though I am still a lil fuzzy over the prototype declaration for the same..
    should it be (int)(cmp*)(KEY, KEY)? aren't I just calling one function then?? or would I change it accordingly?
    No it's *cmp because you pass the address of the function.
    I think you can also call the functions like this:
    Code:
    temp = searchList(pList, &pCur, &pPre, k2, &cmp_first);
    But when passing a function you don't need to add the &.
    You automatically get a function pointer...

Popular pages Recent additions subscribe to a feed