Question: I have the following code: typedef struct list_type *ListType; ListType create(int elSize, int (*compare) (void *item1, void *item2)); void destroy(ListType listP); void make_empty(ListType listP); int
I have the following code:
typedef struct list_type *ListType;
ListType create(int elSize, int (*compare) (void *item1, void *item2)); void destroy(ListType listP); void make_empty(ListType listP); int is_empty(ListType listP); int is_full(ListType listP); int size_is(ListType listP); void push(ListType listP, void *item); void delete(ListType listP, void* item); void printl(ListType listP, void (*printItem) (void *item)); void* search(ListType listP,void *item);
#endif
****************************
#include "listADTgen.h" #include
struct list_type { void *data; int size; int capacity; int elementSize; int (*comparePtr) (void *d1, void *d2); };
// returns the pointer to the list; NULL if list not created ListType create(int elSize, int (*comp) (void *item1, void * item2)) { // allocate memory for a structure variable containing all // list components ListType listptr = malloc(sizeof(struct list_type)); // if allocation was succesfull if (listptr != NULL) { listptr->size = 0; listptr->capacity = 10; listptr->elementSize = elSize; listptr->comparePtr = comp; //listptr->destroyPtr = destroy; // allocate memory for the actual array listptr->data = malloc(10*(listptr->elementSize)); // if array not created, deallocate the list too if (listptr->data == NULL) { free(listptr); listptr = NULL; } } return listptr; }
void destroy(ListType listptr) { free(listptr->data); free(listptr); listptr = NULL; }
void printl(ListType listptr, void (*printItem) (void *d)) { int i; for(i = 0; i < listptr->size; i++) { printItem(listptr->data + i * (listptr->elementSize) ); printf(" "); } printf(" "); } int size_is(ListType listptr) { return listptr->size; }
int is_empty(ListType listptr) { return listptr->size == 0; }
// assumes list elements were not allocated dynamically void make_empty(ListType listptr) { // if list is larger than 200 elements, create a smaller one if (listptr->size > 200) listptr->data = realloc(listptr->data, listptr->elementSize * 100);
listptr->size = 0; listptr->capacity = 100; }
// adds a component to the array, if enough memory available void push(ListType listptr, void *item) { if (listptr->size >= listptr->capacity) { void *temp = realloc(listptr->data, listptr->elementSize * (listptr->capacity + 100)); if (temp != NULL) { listptr->capacity += 100; listptr->data = temp; } } if (listptr->size < listptr->capacity) { memcpy(listptr->data + (listptr->size) * (listptr->elementSize), item, (listptr->elementSize)); listptr->size++; } }
int is_full(ListType listptr) { if (listptr->size >= listptr->capacity) { void * temp = malloc(listptr->elementSize* (listptr->capacity + 100)); if (temp != NULL) { free(temp); return 0; } else return 1; } return 0; }
void delete(ListType listptr, void *item) {
}
void set_ID (client c,int n) { c->ID = n; } int get_ID (client c) { return c->ID; } void set_name(client c,char *name) { strcpy(c->name,name); } char* get_name(client c) { return c->name; } void set_phone(client c,char *phoneNum) { strcpy(c->phoneNum,phoneNum); } char* get_phoneNum(client c) { return c->phoneNum; } void set_email(client c,char *email) { strcpy(c->email,email); } char* get_email(client c) { return c->email; } int get_client_size(void) { client c = malloc(sizeof(client)); return sizeof(c); } client create_client( void) { client c = malloc(sizeof(client)); return c; }
void freeup(client c) { free(c); }
****************************************
using this generic class, I created to list. one list contains information about a person( as an structure)
struct client_tag{
int ID;
char name[MAXLEN];
char phoneNum[MAXLEN];
char email[MAXLEN];
};
***********************************
the other one is string and numeric values
typedef struct stock_tag { char symbol[MAXLEN]; float price; }stock;
stock *create_stock() { stock *s= malloc (sizeof(stock)); return s; }
void destroy_stock(stock *s) { free(s); } char* get_symbol(stock *s) { return s->symbol; }
I'm trying to add a method to my generic list class that can srearch either list and return the index. my problem is that in one list Ineed to search based on ID which is an integer. on the other one I have to do the search based on Symbol which is an string.
how should can I do that??
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
