Skip to content

Commit

Permalink
Code has been optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
ivenstein committed Dec 29, 2016
1 parent 85974cf commit 8adb9d5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
16 changes: 16 additions & 0 deletions CircularBuffer/CircularBuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,24 @@
CircularBuffer * circularbuffer_initialize(size_t size, void * val){
size_t i;
CircularBuffer * cb = (CircularBuffer *)malloc( sizeof(CircularBuffer) );

if (cb == NULL) {

// any other implementation may be added here.

printf("\nERROR: Insufficient memory. Terminating...");
exit(EXIT_FAILURE);
}

cb->buffer = (Item *)calloc(size, sizeof(Item));

if (cb->buffer == NULL) {

// any other implementation may be added here.

printf("\nERROR: Insufficient memory. Terminating...");
exit(EXIT_FAILURE);
}

for(i=0; i<size; i++){
cb->buffer[i].data = val;
Expand Down
34 changes: 31 additions & 3 deletions HashTable/HashTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
HashTable * hashtable_initialize(size_t size, size_t mc, size_t gf, hash_function fn, compare_equal efn){
size_t i;
HashTable * ht = (HashTable *) malloc( sizeof(HashTable) );

if (ht == NULL) {
printf("\nERROR: Cannot create hash table. Terminating...");
exit(EXIT_FAILURE); // well... any other implementation could be added, instead of this.
}

ht->hf = fn;
ht->eq = efn;
Expand All @@ -25,6 +30,11 @@ HashTable * hashtable_initialize(size_t size, size_t mc, size_t gf, hash_functio
ht->growthFactor = gf;

ht->table = (Item **) malloc(size * sizeof( Item * ));

if (ht->table == NULL) {
printf("\nERROR: Cannot create hash table. Terminating...");
exit(EXIT_FAILURE); // well... any other implementation could be added, instead of this.
}

for(i=0; i<size; i++){
ht->table[i] = 0;
Expand All @@ -49,8 +59,19 @@ void * hashtable_get(HashTable * ht, void * key){
}

int hashtable_destroy(HashTable * ht){

// Avoiding runtime errors. Return a successful destroy when the
// hashtable has not been initialized using the 'hashtable_initialize'
// function.

if (ht == NULL)
return 1;

// From here we know the hashtable has been properly initialized so,
// we continue computations.

size_t i;

for(i=0; i<ht->size; i++){
free(ht->table[i]);
}
Expand Down Expand Up @@ -79,8 +100,8 @@ int hashtable_resize(HashTable * ht, size_t size){
}

free(ht->table);
ht->size = newht->size;
ht->table = newht->table;
newht->size = ht->size;
newht->table = ht->table;

return(1);
}
Expand All @@ -102,6 +123,13 @@ int hashtable_add(HashTable * ht, void * key, void * value){
}

next = (Item *)malloc( sizeof(Item) );

// Making sure that memory was properly allocated for 'next'.
if (next == NULL) {
printf("\nERROR: Insufficient memory. Terminating....");
exit(EXIT_FAILURE); // some other implementation could be used here.
}

next->key = key;
next->value = value;
next->next = ht->table[ hash % ht->size ];
Expand Down
7 changes: 7 additions & 0 deletions List/ListItem.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
ListItem_t * list_item_initialize(){
ListItem_t * li = (ListItem_t *) malloc( sizeof(ListItem_t) );

// Making sure there was enough memory for a 'ListItem_t' type 'li'.
if (li == NULL) {
printf("\nERROR: Insufficient memory. Terminating...");
exit(EXIT_FAILURE);
}

li->next = 0;

return(li);
Expand Down
8 changes: 8 additions & 0 deletions Queue/Queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ int queue_push(Queue_t * q, void * d){

if(q->size == 0){
q->head = (QueueItem_t *) malloc( sizeof(QueueItem_t) );
if (q->head == NULL) {
printf("\nERROR: Insufficient memory. Terminating...");
exit(EXIT_FAILURE);
}
q->head->next = 0;
q->head->data = d;
}else{
tmp = q->head;
q->head = (QueueItem_t *) malloc( sizeof(QueueItem_t) );
if (q->head == NULL) {
printf("\nERROR: Insufficient memory. Terminating...");
exit(EXIT_FAILURE);
}
q->head->data = d;
q->head->next = tmp;
}
Expand Down

0 comments on commit 8adb9d5

Please sign in to comment.