开发者

bsearch function in c

If I have two functions:

void SortStudents(char *studentList[], size_t studentCount) 
{
    qsort(studentList, sizeof(studentList)/sizeof(studentList[0]), sizeof(studentList[0]), Compare);
}

int Compare(const void *a, const void *b) 
{
    return (strcmp(*(char **)a, *(char **)b));
}

That sort and compare using the qsort function, how do I use bsearch to find subsets of my list. For example,开发者_运维知识库 if I have two lists:

  • (List A) Bob, Jimmy, Lee, James, Anne
  • (List B) Jen, Jon, Lee, James, Steph

How do I search in List B to find those elements in A?

Can you also do a search in List B to find those elements not in A?

Thanks.


To do a search, you have to use a one-item list as the key parameter to 'bsearch()'.

In context, searching for the entry at a_list[n] in b_list:

 void *found = bsearch(&a_list[n], b_list, b_list, b_size, Compare);

So, to find the elements in List B that are in List A, you will do:

  • Sort List B (you do not need to sort List A for this part of the exercise unless you want to)
  • For each element in List A, search for the item in (the sorted) List B.

And to find the elements in B that are not in A, you will need to sort List A after all and then for each element in List B, see whether the element is in List A using the reversed search.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char *a_list[] = { "Bob", "Jimmy", "Lee", "James", "Anne"  };
static char *b_list[] = { "Jen", "Jon",   "Lee", "James", "Steph" };
static size_t a_number = sizeof(a_list)/sizeof(a_list[0]);
static size_t b_number = sizeof(b_list)/sizeof(b_list[0]);

static int Compare(const void *a, const void *b) 
{
    return (strcmp(*(char **)a, *(char **)b));
}

void SortStudents(char *studentList[], size_t studentCount) 
{
    qsort(studentList, studentCount, sizeof(studentList[0]), Compare);
}

static void dump_list(const char *tag, char **list, size_t number)
{
    size_t i;
    printf("%s:\n", tag);
    for (i = 0; i < number; i++)
        printf(" %s%s", list[i], (i == number - 1) ? "" : ",");
    putchar('\n');
}

static char *search_list(char *name, char **list, size_t number)
{
    char **found = bsearch(&name, list, number, sizeof(*list), Compare);
    return((found == 0) ? 0 : *found);
}

static void names_in_list(char **find_list, size_t find_number, char **name_list, size_t name_number)
{
    size_t i;
    for (i = 0; i < find_number; i++)
    {
        char *name = search_list(find_list[i], name_list, name_number);
        if (name != 0)
            printf("Found %s in list at %s\n", find_list[i], name);
    }
}

static void names_not_in_list(char **find_list, size_t find_number, char **name_list, size_t name_number)
{
    size_t i;
    for (i = 0; i < find_number; i++)
    {
        char *name = search_list(find_list[i], name_list, name_number);
        if (name == 0)
            printf("Did not find %s in list\n", find_list[i]);
    }
}

int main(void)
{
    dump_list("Unsorted A list", a_list, a_number);
    dump_list("Unsorted B list", b_list, b_number);
    SortStudents(a_list, a_number);
    SortStudents(b_list, b_number);
    dump_list("Sorted A list", a_list, a_number);
    dump_list("Sorted B list", b_list, b_number);
    dump_list("Searching in B list for people in A list", b_list, b_number);
    names_in_list(a_list, a_number, b_list, b_number);
    dump_list("Searching in A list for people not in B list", a_list, a_number);
    names_not_in_list(b_list, b_number, a_list, a_number);
    return(0);
}

And the output was:

Unsorted A list:
 Bob, Jimmy, Lee, James, Anne
Unsorted B list:
 Jen, Jon, Lee, James, Steph
Sorted A list:
 Anne, Bob, James, Jimmy, Lee
Sorted B list:
 James, Jen, Jon, Lee, Steph
Searching in B list for people in A list:
 James, Jen, Jon, Lee, Steph
Found James in list at James
Found Lee in list at Lee
Searching in A list for people not in B list:
 Anne, Bob, James, Jimmy, Lee
Did not find Jen in list
Did not find Jon in list
Did not find Steph in list
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜