mylib.c
#include stdlib.h #include stdio.h #include "mylib.h" #include assert.h #include ctype.h
/* INSERT GETWORD HERE */
void *emalloc(size_t s){
void *result = malloc(s);
if (NULL == result){
fprintf(stderr, "Memory allocation failed!\n");
exit(EXIT_FAILURE);
}
return result;
}mylib.h
include
#ifndef MYLIB_H_ #define MYLIB_H_
extern void *emalloc(size_t);
extern int getword(char *s, int limit, FILE *stream);
htable_new
htable htable_new(int capacity){
int i;
htable result = emalloc(sizeof *result);
result->capacity = capacity;
result->num_keys = 0;
result->keys = emalloc(result->capacity * sizeof result->keys[0]);
for (i=0; I less than result->capacity; I++){
keys[i] = NULL;
}
return result;
}htable_insert (Part 1)
int htable_insert(htable h, char *str){
int collisions = 0;
unsigned int i = htable_word_to_int(str) % h->capacity;
if (h->keys[i] == NULL){
h->keys[i] = emalloc((strlen(str) + 1) * sizeof str[0]);
strcpy(h->keys[i], str);
h->num_keys++;
return 1;
}htable_insert (Part 2)
while (h->keys[i] != NULL && strcmp(h->keys[i],str) != 0 &&
(collisions-1) != h->capacity){
i = (i+1)% h->capacity;
collisions++;
if (h->keys[i] == NULL){
h->keys[i] = emalloc((strlen(str) + 1) * sizeof str[0]);
strcpy(h->keys[i], str);
h->num_keys++;
return 1;
}
/* IF ++collisions GREATER than capacity */
if (++collisions > h->capacity) return 0;
}
return 0;
}htable_print
void htable_print(htable h, FILE *stream){
/* GIVEN PRINT CODE */
}htable_free
void htable_free(htable h){
int i;
for (i=0;i< h->capacity; i++){
free(h->keys[i]);
}
free(h->keys);
free(h);
}bst housekeeping
#include stdio.h #include stdlib.h #include "mylib.h" #include "bst.h" #include string.h
struct bst_node {
char *key;
bst left;
bst right;
};bst_new() (SIMPLE!!)
bst bst_new(){
return NULL;
}bst_insert
bst bst_insert(bst b, char *str){
if (b == NULL){
b = emalloc(sizeof *b);
b->key = emalloc(strlen(str)+ 1);
strcpy(b->key, str);
b->left = NULL;
b->right = NULL;
} else if (strcmp(b->key, str) < 0){
b->right = bst_insert(b->right, str);
} else if (strcmp(b->key, str) > 0){
b->left = bst_insert(b->left, str);
}
return b;
}bst_inorder
void bst_inorder(bst b, void f(char *str)){
if (b== NULL){
return;
}
bst_inorder(b->left, f);
f(b->key);
bst_inorder(b->right, f);
}bst_preorder
void bst_preorder(bst b, void f(char *str)){
if (NULL == b){
return;
}
f(b->key);
bst_preorder(b->left, f);
bst_preorder(b->right, f);
}bst_search
int bst_search(bst b, char *str){
if (NULL == b){
return 0;
} else {
int compare = strcmp(b->key, str);
if (compare == 0){
return 1 ;
} else if (compare < 0){
return bst_search(b->right, str);
} else {
return bst_search(b->left, str);
}
}
return 0;
}bst_free
bst bst_free(bst b){
if (b == NULL){
return b;
}
bst_free(b->right);
bst_free(b->left);
free(b->key);
free(b);
return b;
}ARRAY queue_new
queue queue_new(){
int i;
queue result = emalloc(sizeof *result);
result->head = 0;
int default_size = 7;
result->capacity = DEFAULT_SIZE;
result->num_items = 0;
result->items = emalloc(result->capacity * sizeof result->items[0]);
for (i=0; i < result->capacity; i++){
result->items[i] = 0.0;
}
return result;
}ARRAY dequeue
double dequeue(queue q){
double target;
if (q->num_items == 0){
fprintf(stderr, "Queue is empty.\n");
return EXIT_FAILURE;
}
target = q->items[q->head];
q->head++;
q->num_items--;
return target;
}ARRAY queue_print
void queue_print(queue q){
int i = q->head;
do {
printf("%.2f\n", q->items[i]);
i = (i+1)%q->capacity;
} while (i != (q->head + q->num_items)%q->capacity);
}ARRAY queue_size and queue_free
int queue_size(queue q){
return q->num_items;
}
queue queue_free(queue q){
free(q->items);
free(q);
return q;
}ARRAY queue housekeeping
define DEFAULT_SIZE 7
struct queue {
double *items;
int head;
int capacity;
int num_items;
};LIST queue housekeeping
struct q_item{
double item;
q_item next;
};
struct queue {
q_item first;
q_item last;
int length;
};LIST queue_new
queue queue_new(){
queue q = emalloc(sizeof *q);
q->first = NULL;
q->last = NULL;
q->length = 0;
return q;
}LIST dequeue
double dequeue(queue q){
q_item head = q->first;
double result;
if (NULL == head){
return 0.0;
}
q->first = q->first->next;
result = head->item;
free(head);
q->length--;
return result;
}LIST queue_print
void queue_print(queue q){
q_item temp = q->first;
int i;
for (i=0; i < q->length;i++){
printf("%.2f\n", temp->item);
temp = temp->next;
}
} .LIST queue_size
int queue_size(queue q){
return q->length;
}