CMSC--202 , Section Computer Science II for Majors
Fall 1997 17 October 1997 Homework 5
Assigned: 21 October
Due Date: 30 October
Late Date: 3 November
The purpose of this assignment is to give you more experience
implementing and using ADTs. You will implement the fleezle
ADT. The name fleezle has absolutely no meaning, it was chosen
for its strange sound.
A fleezle is a data storage structure that holds void
pointers. These pointers will be pointing to some type of data. The
type of a fleezle is fleezle_type
. The fleezle ADT consists of
the following operations. Note that any errors generated are to
result in an appropriate message to stderr and termination of
the program.
TRUE
if F does not contain any items, FALSE otherwise.
T
to that item. The type definition for
fleezle_xformer_type
is given on page
fleezle_print_type
is given on page
The interface for fleezle must match the ADT operations above. The implementation is entirely up to you. Choose an implementation that makes sense and that matches the ADT requirements. For example, since there is no maximum size on a fleezle, an array would not be a good choice.
Here are the principle tasks you must accomplish:
Submit your work in the usual way, using the submit command. For example, to submit the file foo.c, do:
submit cs202_01 hw5 foo.c
You must submit all the files required to successfully compile your homework using your submitted makefile. The only constraints on file names are that the executable must be named hw5 and the makefile must be named Makefile or makefile. Otherwise, you may name your files as you wish. You will be graded on your choice of files and file names. Modularity requires that you have a separate file for interfaces (for example, fleezle.h), a similarly-named implementation file (for example, fleezle.c). The main function should be in its own file. Auxiliary functions should be logically grouped in their own file (or files) with appropriate interface files.
Here are some type definitions:
typedef void * (*fleezle_xformer_type)(void *); typedef int (*fleezle_compare_type)(void *, void *); typedef void (*fleezle_print_type)(FILE *, void *);
A fleezle_xformer_type function performs a transforming
operation. It takes a pointer to data and returns a pointer to a
transformed version of the data. It does not change the original
data, it returns a transformed version. For example, the
function void * intTwoLarger(void *)
is of type
fleezle_xformer_type
.
If used in the following code,
int i = 3; int * ip = intTwoLarger(&i);then
ip
would point to an integer that has value 6.
A fleezle_compare_type function compares the data pointed to. It
returns a negative integer if the first datum precedes the second. It
returns zero if the first and second data are equivalent. It returns
a positive integer if the first datum follows the second. This is
very much like the strcmp comparator to compare ``strings.''
For example, the function int intCompare(void * p1, void * p2)
is of type fleezle_compare_type
.
If used in the following code
int i = 3; int j = 4; intCompare(&i, &j);the returned value would be a negative integer.
A fleezle_print_type
function prints a value to a file. For
example, void intPrint(FILE * fp, void * p)
is of type
fleezle_print_type
.
If used in the following code
int i = 3; intPrint(stdout, &i);the integer 3 would be printed to stdout.
Here is a driver for you to use. The driver takes a command line
argument that is the number of items to put into the fleezle.
The fleezle holds integers. The integers are randomly generated
using the rint202 function (see rint202.h in the directory
~anastasi/Public/202/Includes
).
#include "fleezle.h" #include "hw5_aux.h" #include <stdio.h> void main(int argc, char * argv[]) { fleezle_type F; int N; /* get number of entries from command line */ /* exit with usage message if command line is not */ /* of proper form (namely a single integer) */ N = getCommandLineArg(argc, argv); /* construct a fleezle that holds N random integers */ F = makeIntFleezle(N); printFleezle(stdout, F, intPrint); fprintf(stdout, "\n"); transformFleezle(F, intTwoLarger); printFleezle(stdout, F, intPrint); fprintf(stdout, "\n");
printf("Smallest is "); printFleezleItem(stdout, getSmallest(F, intCompare), intPrint); fprintf(stdout, "\n"); /* test for error condition in getSmallest */ F = createFleezle(); /* create empty fleezle */ getSmallest(F, intCompare); }
Here is the output from two typical runs of the driver. Your output may differ since the integers are randomly generated.
>> hw5 Usage: hw5 <nentries> >> hw5 4 157 629 764 451 314 1258 1528 902 Smallest is 314 Error: Attempt to getSmallest of empty fleezle