Sample Code
You can combine the following code with the code from each of the listings in this chapter to form a working
program you can compile and run. Each example has been compiled and run on the same data set, and the
results are compared in the introduction section of this chapter entitled “Performance of Sorting and
Searching.”
program you can compile and run. Each example has been compiled and run on the same data set, and the
results are compared in the introduction section of this chapter entitled “Performance of Sorting and
Searching.”
The first listing is a makefile, which can be used with a make utility to compile each program. Because some
make utilites don’t understand this format, and because not everyone has a make utility, you can use the information in this makefile yourself. Each nonblank line lists the name of an example followed by a colon
and the source files needed to build it. The actual compiler commands will depend on which brand of compiler you have and which options (such as memory model) you want to use. Following the makefile are
source files for the main driver programs and the linked list code used by some of the algorithms.
source files for the main driver programs and the linked list code used by some of the algorithms.
The code in driver1.c (Listing III.9a) sorts all of its command-line arguments, as strings, using whatever
sorting algorithm it is built with, and prints the sorted arguments. The code in driver2.c (Listing III.9b)
generates a table of the first 10,000 prime numbers, then searches for each of its command-line arguments
(which are numbers) in that table.
Because the algorithm in Listing III.6 does not search items in an array, it has its own main procedure. It
reads lines of input until it gets to the end-of-file, and then it prints the entire trie data structure. Then it
searches for each of its command-line arguments in the trie and prints the results.
A makefile with rules to build the programs in this chapter.
3_1: 3_1.c driver1.c
3_2a: 3_2a.c driver1.c
3_2b: 3_2b.c list.c driver1.c
3_2c: 3_2c.c list.c driver1.c
3_3: 3_3.c 3_1.c
3_4: 3_4.c 3_1.c driver2.c
3_5: 3_5.c 3_1.c driver2.c
3_6: 3_6.c 3_1.c driver2.c
3_7: 3_7.c list.c hash.c driver3.c
3_8: 3_8.c list.c hash.c driver3.c
The driver1.c driver for all the sorting algorithms except the external sort algorithm.
#include <stdio.h>
extern void sortStrings(const char **);
/* Sorts its arguments and prints them, one per line */
int
main(int argc, const char *argv[])
{
int i;
sortStrings(argv + 1);
for (i = 1; i < argc; i++)
puts(argv[i]);
return 0;
}
The driver2.c driver for the searching algorithms using bsearch(), binary, and linear
search algorithms.
#include <stdio.h>
#include <string.h>
extern const char *search(const char *, const char **,
size_t);
static int size;
static const char **array;
static void initArray(int limit)
{
char buf[1000];
array = (const char **) calloc(limit, sizeof(char *));
for (size = 0; size < limit; size++)
{
if (gets(buf) == NULL)
break;
array[size] = strdup(buf);
}
sortStrings(array, size);
}
int main(int argc, char **argv)
{
int i;
int limit;
if (argc < 2)
{
fprintf(stderr, “usage: %s size [lookups]\n”,
argv[0]);
exit(1);
}
limit = atoi(argv[1]);
initArray(limit);
for (i = 2; i < argc; i++)
{
const char *s;
if (s = search(argv[i], array, limit))
printf(“%s -> %s\n”, argv[i], s);
else
printf(“%s not found\n”, argv[i]);
}
return 0;
}
No comments:
Post a Comment