I'm working on CS59 Week 5's "Speller" assignment. Currently, I'm trying to write the unload function with a test dictionary file on my desktop. But for some reason, line 91 (while(!feof(dictionary))) is returning an error:
"request for member '_flag' in something not a structure or a union"
I'm working from a new computer. My previous computer did not return this error. Can someone please explain what's going on here and how I can fix it? Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
unsigned int HASH_MAX = 50;
unsigned int LENGTH = 20;
unsigned int hash (const char *word);
bool load(char *dictionary);
bool check (char *word);
bool unload (void);
void print (void);
typedef struct _node
{
char *word[20];
struct _node *next;
} node;
node *HASH_TABLE[50];
int main(int argc, char *argv[])
{
FILE *dictionary = fopen("C:/Users/aaron/Desktop/Dictionary.txt", "r");
if(!dictionary)
{
printf("FILE NOT FOUND\n");
return 1;
}
if (load(dictionary))
{
// print "LIST (number): {(name, address), ...}\n
print();
}
int lois = hash("Lois");
printf("\n%s\n", HASH_TABLE[lois]->word);
if (check("StEwIe"))
{
printf("STEWIE found\n");
}
else if (!check("StEwIe"))
{
printf("STEWIE not found\n");
}
if (check("Tron"))
{
printf("TRON found\n");
}
else if (!check("Tron"))
{
printf("TRON not found\n");
}
if (unload())
{
print();
}
}
unsigned int hash(const char *word) // hash code function (FIND A BETTER ALGORITHM)
{
char word_conv[LENGTH + 1]; // store converted word for uniform key
unsigned int code = 0; // hash code
for (int i = 0; i < strlen(word); i++) // set all letters in the word to lower case
{
word_conv[i] = tolower(word[i]);
}
for (int j = 0; j < strlen(word_conv); j++) // for all letters in converted word, add ascii value to code and multiply by 3
{
code += word_conv[j];
code *= 3;
}
code = code % HASH_MAX; // set code to remainder of current code divided by maximum hash table size
return code;
}
bool load (char *dictionary)
{
char word[LENGTH+1];
while(!feof(dictionary))
{
fscanf(dictionary, "%s", word);
node *new_n = malloc(sizeof(node));
strcpy(new_n->word, word);
new_n->next = NULL;
unsigned int code = hash(word);
if (HASH_TABLE[code] == NULL)
{
HASH_TABLE[code] = new_n;
}
else if (HASH_TABLE[code] != NULL)
{
node *trav = HASH_TABLE[code];
while (trav->next != NULL)
{
trav = trav->next;
}
if (trav->next == NULL)
{
trav->next = new_n;
}
}
}
return true;
}
bool check (char *word)
{
unsigned int code = hash(word);
node *check = malloc(sizeof(node));
check = HASH_TABLE[code];
while (check != NULL)
{
if (strcasecmp(check->word, word) == 0)
return true;
}
if (check == NULL)
return false;
}
bool unload (void)
{
for (int i = 0; i < HASH_MAX; i++)
{
while (strcmp(HASH_TABLE[i]->word, "FREE" != 0)
{
node *curr = HASH_TABLE[i];
node *prev = NULL;
while (curr != NULL)
{
prev = curr;
curr = curr->next;
}
strcpy(prev->word, "FREE");
}
}
return true; // freed successfully
}
void print (void)
{
for (int i = 0; i < HASH_MAX; i++)
{
node *check = HASH_TABLE[i];
printf("LIST %02d: {", i);
while (check != NULL)
{
printf("%s, ", check->word);
check = check->next;
}
printf("}\n");
}
}
Related
Problem is function bin_search. it works steady on function insert. However, it gets frozen on function search. I think if it's fine on insert, it should be fine on search, but it isn't. Here is my code...
"bst.h":
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node {
int key;
void *data;
struct Node *left, *right;
void (*destroy)(void *data);
} node;
typedef struct Tree {
node *head;
char name;
} tree;
#define key(node) node->key
#define data(node) node->data
#define left(node) node->left
#define right(node) node->right
#define destroy(node) node->destroy
#define tree_head(tree) tree->head
"functions.c":
#include "bst.h"
int bin_search(node *curr, int key, int cnt, node **found) {
cnt++; printf("cnt+\n");
if (curr == NULL) {
return -1;
} else if (curr->key == key) {
printf("curr_key = key\n"); return cnt;
}
if (curr->key < key) {
printf("curr_key < key\n");
if (curr->right == NULL) {
*found = curr;
return -(cnt + 1);
}
return bin_search(curr->right, key, cnt, found);
} else {
printf("curr_key > key\n");
if (curr->left == NULL) {
*found = curr;
return -(cnt + 1);
}
return bin_search(curr->left, key, cnt, found);
}
}
int insert(tree *root, int key, void *data, void (*destroy)(void *data)) {
if (root->head == NULL) {
node* new_node = (node *)malloc(sizeof(node));
left(new_node) = NULL; right(new_node) = NULL; destroy(new_node) = destroy; key(new_node) = key; data(new_node) = data;
tree_head(root) = new_node;
printf("created first node\n"); return 1;
}
int cnt; node **found;
if ((cnt = bin_search(root->head, key, 0, found)) < 0) {
node* new_node = (node *)malloc(sizeof(node));
left(new_node) = NULL; right(new_node) = NULL; destroy(new_node) = destroy; key(new_node) = key; data(new_node) = data;
if ((*found)->key < key) {
(*found)->right = new_node;
} else {
(*found)->left = new_node;
}
printf("created a node at %d\n", -cnt); return 1;
} else {
printf("already exists in tree"); return -1;
}
}
int search(tree *root, int key, void **data) {
if (root->head == NULL) {
printf("tree is empty\n"); return -1;
}
int cnt; node **found;
if ((cnt = bin_search(root->head, key, 0, found)) < 0) {
return -1;
} else {
if ((*found)->key < key) {
*data = (*found)->right->data;
} else {
*data = (*found)->left->data;
}
return cnt;
}
}
"main.c":
#include "bst.h"
#define MAX_NUM 8
#define MAX_LEGNTH 200
int main() {
// create a tree
tree root; root.head = NULL; root.name = 'a';
FILE *inpt = fopen("list.txt", "r"); char buffer[MAX_LEGNTH];
int count = 0;
while (fgets(buffer, MAX_LEGNTH, inpt) != NULL) {
printf("adding: %d\n", atoi(buffer)); insert(&root, atoi(buffer), buffer, NULL);
count++;
}
fclose(inpt);
int result; void **found;
result = search(&root, 2, found); printf("%d\n", result); // problem in here!!
return 0;
}
what is the problem with my 'search' function. I can't find it.
Besides the utterly non-standard use of sizeof(void), you are not providing a correct out-parameter to search. The point of the found argument is to receive the node pointer if the prospect key was discovered. This...
int cnt;
node **found; // <== LOOK HERE
if ((cnt = bin_search(root->head, key, 0, found)) < 0) {
is not the way to do that. It should be done like this:
int cnt;
node *found = NULL;
if ((cnt = bin_search(root->head, key, 0, &found)) < 0) {
// ^^^^^^
and all references to found thereafter should be through found->, not (*found)->
This mistake is made in three different places in your code. The last one is semi-broken, but still broken nonetheless.
void **found = (void *)malloc(sizeof(void));
int result = search(&root, 2, found);
printf("%d\n", result);
That should use this:
void *found = NULL;
int result = search(&root, 2, &found);
printf("%d\n", result);
Whether the rest of your code is broken I cannot say, and frankly we're not in the business of being an online-debugger. Use your local debugger tools; that's what they're for. But the items listed above are definitely a problem.
I am making a Hash Map with people's names as its key using C language. I am using separate chaining to resolve the collision.
This is my code:
#include<stdio.h>
#include<stdlib.h>
#define MinTableSize 1
#include <stdbool.h>
//Colission resolution Using linked list
struct ListNode;
typedef struct ListNode *Position;
struct HashTbl;
typedef struct HashTbl *HashTable;
typedef unsigned int Index;
Index Hash(const char *Key, int Tablesize)
{
unsigned int HashVal = 0;
while(*Key != '\0')
{
HashVal += *Key++;
}
return HashVal % Tablesize;
}
struct ListNode
{
int Element;
Position Next;
};
typedef Position List;
struct HashTbl
{
int TableSize;
List *TheLists;
};
//Function to find next prime number for the size
bool isPrime(int n)
{
if(n <= 1)
{
return false;
}
if(n <= 3)
{
return true;
}
if(n%2 == 0 || n%3 == 0)
{
return false;
}
for(int i = 5; i*i <= n; i = i + 6)
{
if(n%i == 0 || n%(i + 2) == 0)
{
return false;
}
}
return true;
}
int NextPrime(int N)
{
if(N <= 1)
{
return 2;
}
int prime = N;
bool found = false;
while(!found)
{
prime++;
if(isPrime(prime))
{
found = true;
}
}
return prime;
}
HashTable InitializeTable(int TableSize)
{
HashTable H;
int i;
if(TableSize < MinTableSize)
{
printf("Table size is too small\n");
return NULL;
}
H = malloc(sizeof(struct HashTbl));
if(H == NULL)
{
printf("Out of space\n");
return NULL;
}
H->TableSize = NextPrime(TableSize);
H->TheLists = malloc(sizeof(List) * H->TableSize);
if(H->TheLists == NULL)
{
printf("Out of space\n");
return NULL;
}
for(i = 0; i < H->TableSize; i++)
{
H->TheLists[i] = malloc(sizeof(struct ListNode));
if(H->TheLists[i] == NULL)
{
printf("Out of space\n");
return NULL;
}
else
{
H->TheLists[i]->Next = NULL;
}
}
return H;
}
//funtion to find the value
Position Find(const char *Key, HashTable H)
{
Position P;
List L;
L = H->TheLists[Hash(Key, H->TableSize)];
P = L->Next;
while(P != NULL && P->Element != Key)
{
P = P->Next;
}
return P;
}
void Insert(const char *Key, HashTable H)
{
Position Pos;
Position NewCell;
List L;
Pos = Find(Key, H);
if(Pos == NULL)
{
NewCell = malloc(sizeof(struct ListNode));
if(NewCell == NULL)
{
printf("Out of space\n");
return NULL;
}
else
{
L = H->TheLists[Hash(Key, H->TableSize)];
NewCell->Next;
NewCell->Element = Key;
L->Next = NewCell;
printf("Key inserted\n");
}
}
else
{
printf("Key already exist\n");
}
}
int main()
{
char Name[6][20] = {"Joshua", "Erica", "Elizabeth", "Monica", "Jefferson", "Andrian"};
int Size = sizeof(Name[0])/sizeof(Name[0][0]);
HashTable H = InitializeTable(Size);
Insert(Name[0], H);
Insert(Name[1], H);
Insert(Name[2], H);
Insert(Name[3], H);
}
The putout of this code is:
Key inserted
Key inserted
This means, it only successfully inserted two keys, while the other name has not been inserted. I think there's some error in my Insert() function, but I got no clue. I tried using an online compiler and it compile properly.
I'm trying to delete a value from hash table, the code works when I delete a value from the array where the linked list size is one, then segfaults if the size is greater than 1.
typedef struct hash {
char *key;
char *value;
struct hash *next;
} hashTable;
// generate hash code
unsigned int hashCode(char *key) {
int sum;
int i;
sum = 0;
i = 0;
while (key[i])
sum += key[i++];
return sum % MAX_HASH;
}
// get hash item size
int hash_size(hashTable *head) {
int i;
hashTable *list;
i = 0;
list = head;
if (list) {
while (list != NULL) {
i++;
list = list->next;
}
}
return (i);
}
// free item
void free_hash(hashTable *item) {
free(item->key);
free(item->value);
free(item);
}
// function for deleting item from hash table
void deleteItem(hashTable *table[], char *key) {
hashTable *head = table[hashCode(key)];
hashTable *tmp = head;
hashTable *prev = NULL;
if (!head)
return;
if (hash_size(tmp) == 1) {
table[hashCode(key)] = 0;
free_hash(tmp);
return;
}
while (strcmp(tmp->key, key) != 0 && tmp->next != NULL) {
prev = tmp;
tmp = tmp->next;
}
if (strcmp(tmp->key, key) == 0) {
if (prev)
prev->next = tmp->next;
else
head = tmp->next;
free_hash(tmp);
}
}
// function for inserting item into the table
void insert(hashTable *table[], char *key, char *value) {
hashTable *tmp;
hashTable *item;
unsigned int code;
item = (hashTable *)malloc(sizeof(hashTable));
if (!item)
return;
item->key = (char *)malloc(sizeof(char) * strlen(key) + 1);
item->value = (char *)malloc(sizeof(char) * strlen(value) + 1);
item->next = NULL;
code = hashCode(key);
strcpy(item->key, key);
strcpy(item->value, value);
if (!table[code])
table[code] = item;
else {
tmp = table[code];
item->next = tmp;
table[code] = item;
}
}
// displaying items
void display(hashTable *table[]) {
int i = 0;
hashTable *tmp;
while (i < MAX_HASH) {
if (table[i] != NULL) {
tmp = table[i];
if (hash_size(tmp) == 1)
printf("%s=%s\n", tmp->key, tmp->value);
else {
while (tmp != NULL) {
printf("%s=%s\n", tmp->key, tmp->value);
tmp = tmp->next;
}
}
}
i++;
}
}
int main(int argc, char const *argv[]) {
hashTable *table[MAX_HASH];
memset(table, 0, MAX_HASH * sizeof(hashTable *));
insert(table, "Bart", "first");
insert(table, "Lisa", "Second");
insert(table, "Foo", "bar");
deleteItem(table, "Lisa");
display(table);
return 0;
}
There are many issues in your code:
do include the standard header files, and define HASH_MAX:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASH_MAX 1027
the type hashTable is confusing: it is really an entry list, the hash table itself is the array.
the while loops are error prone: use the much preferred for loops where the initialization, test and increment of the loop index are conveniently located on the same line:
for (int i = 0; i < HASH_MAX; i++) {
// printf hashTable[i]
}
I know the local style conventions at 42 explicitly exclude the for loop, but you should lobby against this questionable choice.
there is no need to special case hash_size(tmp) == 1 in display_table()
there is no need to cast the return value of malloc(). sizeof(char) is 1 by definition. You could use strdup() to duplicate C strings.
in deleteItem() you always remove the entry if it is alone: this is incorrect if the entry has a different key. Furthermore, you do not link the previous node or the table slot to the next element of the list.
Here is a corrected version of this function:
// function for deleting item from hash table
void deleteItem(hashTable *table[], const char *key) {
hashTable **link = &table[hashCode(key)];
while (*link) {
hashTable *tmp = *link;
if (strcmp(tmp->key, key) == 0) {
*link = tmp->next; // unlink the list node
free_hash(tmp);
break; // remove this if you mean for deleteItem to remove all matching nodes
} else {
link = &(*link)->next;
}
}
}
Here is a simplified version of the whole program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_HASH 1027
typedef struct HashItem {
char *key;
char *value;
struct HashItem *next;
} HashItem;
// generate hash code
unsigned int hashCode(const char *key) {
unsigned int sum = 0;
for (int i = 0; key[i]; i++) {
sum += (unsigned char)key[i] * (i + 1);
}
return sum % MAX_HASH;
}
// free item
void freeItem(HashItem *item) {
free(item->key);
free(item->value);
free(item);
}
// function for deleting item from hash table
void deleteItem(HashItem *table[], const char *key) {
HashItem **link = &table[hashCode(key)];
while (*link) {
HashItem *tmp = *link;
if (strcmp(tmp->key, key) == 0) {
*link = tmp->next; // unlink the list node
freeItem(tmp);
break;
} else {
link = &(*link)->next;
}
}
}
// function for inserting item into the table
void insertItem(HashItem *table[], const char *key, const char *value) {
unsigned int code = hashCode(key);
HashItem *item = malloc(sizeof(*item));
if (item != NULL) {
item->key = strdup(key);
item->value = strdup(value);
item->next = table[code];
table[code] = item;
}
}
// displaying items
void displayHashTable(HashItem *table[]) {
for (int i = 0; i < MAX_HASH; i++) {
for (HashItem *tmp = table[i]; tmp; tmp = tmp->next) {
printf("%s=%s\n", tmp->key, tmp->value);
}
}
}
int main(int argc, char const *argv[]) {
HashItem *table[MAX_HASH] = { 0 };
insertItem(table, "Bart", "first");
insertItem(table, "Lisa", "Second");
insertItem(table, "Foo", "bar");
deleteItem(table, "Lisa");
displayHashTable(table);
return 0;
}
#include<iostream>
using namespace std;
enum Stare {DESCHIS=10, LUCRU=20, DUPLICAT=30, REZOLVAT=40, INCHIS=50};
struct Task
{
char *idTask;
char *data;
char *numeInginer;
int nivelComplexitate;
Stare stare;
};
struct List
{
Task *task;
List*next;
};
struct HashTable
{
List** vector;
int size;
};
List* creareNodLista(Task *task)
{
List*nod = (List*)malloc(sizeof(List));
nod->task = task;
nod->next = NULL;
return nod;
}
void initHashTable(HashTable &hTable, int size)
{
hTable.vector = (List**)malloc(sizeof(List*)*size);
hTable.size = size;
memset(hTable.vector, 0, sizeof(List*)*size);
}
int fhash(char c, int l)
{
return c%l;
}
void inserareNodLista(List*&list, List*nod)
{
nod->next = list;
list = nod;
}
void inserareNodHashTable(HashTable hTable, List*nod)
{ //determinarea pozitiei pe care se face inserarea
int index = fhash(nod->task->numeInginer[0],hTable.size);
//obtinerea listei in care se face inserarea
List*list = hTable.vector[index];
//inserare element nou in lista
inserareNodLista(list, nod);
//actualizare element in hashTable
hTable.vector[index] = list;
}
List* getHashTable(HashTable ht, char cheie)
{
int f = fhash(cheie, ht.size);
return ht.vector[f];
}
void printHashTable(HashTable ht)
{
for (char c = 'A'; c < 'Z'; c++)
{
List*lista = getHashTable(ht, c);
int count = 0;
while (lista)
{
count++;
printf("%d", count);
printf("%s\n", lista->task->idTask);
printf("%s\n", lista->task->data);
printf("%s\n", lista->task->numeInginer);
printf("%d\n", lista->task->nivelComplexitate);
printf("%d\n", lista->task->stare);
lista = lista->next;
}
if (count>1)
printf("\nColiziune\n\n");
}
}
int stergeNodHashTable(HashTable hTable, char*numeInginer)
{
int pozitie = 0;
pozitie = fhash(numeInginer[0], hTable.size);
if (hTable.vector[pozitie] == NULL)
return -1;
else
{
if (strcmp(hTable.vector[pozitie]->task->numeInginer, numeInginer) == 0)
{
if (hTable.vector[pozitie]->next == NULL)
{
free(hTable.vector[pozitie]->task->idTask);
free(hTable.vector[pozitie]->task->data);
free(hTable.vector[pozitie]->task->numeInginer);
free(hTable.vector[pozitie]->task);
free(hTable.vector[pozitie]);
hTable.vector[pozitie] = NULL;
}
else
{
List*lista = hTable.vector[pozitie];
hTable.vector[pozitie] = lista->next;
free(lista->task->idTask);
free(lista->task->data);
free(lista->task->numeInginer);
free(lista->task);
free(lista);
lista->next = NULL;
lista = NULL;
}
}
else
{
List*tmp = hTable.vector[pozitie];
while (tmp->next != NULL && strcmp(tmp->next->task->numeInginer, numeInginer) != 0)
tmp = tmp->next;
List*list = tmp->next;
if (list->next == NULL)
{
free(list->task->idTask);
free(list->task->numeInginer);
free(list->task->data);
free(list->task);
free(list);
tmp->next = NULL;
list = NULL;
}
else
{
List*tmp = list;
list = list->next;
free(tmp->task->idTask);
free(tmp->task->data);
free(tmp->task->numeInginer);
free(tmp->task);
free(tmp);
tmp->next = NULL;
tmp = NULL;
}
}
}
return pozitie;
}
void main()
{
FILE *pFile = fopen("Text.txt", "r");
Task *task = NULL;
HashTable ht;
initHashTable(ht, 29);
if (pFile)
{
while (!feof(pFile))
{
task = (Task*)malloc(sizeof(Task));
char id[50];
fscanf(pFile, "%s", &id);
task->idTask = (char*)malloc(strlen(id) + 1);
strcpy(task->idTask, id);
char data[50];
fscanf(pFile, "%s", data);
task->data = (char*)malloc(strlen(data) + 1);
strcpy(task->data, data);
char numeInfiner[50];
fscanf(pFile, "%s", numeInfiner);
task->numeInginer = (char*)malloc(strlen(numeInfiner) + 1);
strcpy(task->numeInginer, numeInfiner);
fscanf(pFile, "%d", &task->nivelComplexitate);
fscanf(pFile, "%d", &task->stare);
//creare element lista
List*nod = creareNodLista(task);
//inserare element in hashTable
inserareNodHashTable(ht, nod);
}
fclose(pFile);
for (char c = 'A'; c < 'Z'; c++)
{
List *lista = getHashTable(ht, c);
while (lista)
{
if (lista->task->stare == 50)
stergeNodHashTable(ht, lista->task->numeInginer);
}
}
printHashTable(ht);
}
}
I am trying to delete from hashTable all the elements with status marked as closed. The delete function is working fine but when i call it for al the hashtable, it got me error: lista->task was 0xDDDDDD. I cant understand why. Please help me!
This is a hashtable implementation.
I have the insert kinda working but how do I return the linked list?
I know that the remove is not done yet but I understand the concept, my problem is returning the adjusted list.
I tried to make the hashtable a global variable but the programming would force when I ran it.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>
struct node {
char * data;
struct node * next;
};
struct hashtable {
struct node ** table;
int size;
int nentries;
};
struct hashtable * hashtable_new(int size) {
struct hashtable * result;
result = malloc(sizeof(struct hashtable));
result -> size = size;
result -> nentries = 0;
result -> table = malloc(sizeof(struct node) * size);
int i = 0;
for (i = 0; i < result->size; i++) {
result->table[i] = NULL;
// result->table[i]->data = NULL;
}
return result;
}
unsigned hash_string(struct hashtable *this, char * str) {
unsigned hash = 0;
int i = 0;
for ( i = 0; str[i] != '\0'; i++ ) {
hash = hash * 37 + str[i];
}
//return hash;
return hash % this-> size;
}
void hashtable_free(struct hashtable * this) {
int i;
struct node *table_nodes, *current, *next;
for(i = 0; i<this->size; i++) {
table_nodes = this->table[i];
current = table_nodes;
while (current != NULL){
next = current->next;
free(current);
current = next;
}
this->table[i] = NULL;
}
free(&this->table);
free(&this->size);
free(&this->nentries);
free(this);
}
void hashtable_insert(struct hashtable * table, char * string) {
struct node * new_node;
unsigned index = hash_string(table, string);
if(table->table[index] == NULL) {
printf("\nIndex: %d", index);
new_node = malloc(sizeof(struct node));
new_node -> next = table->table[index];
new_node -> data = string;
printf("\nData: %s", new_node->data);
table -> table[index] = new_node;
table -> nentries++;
printf("\n");
} else {
new_node = malloc(sizeof(struct node));
new_node->data = string;
new_node->next = NULL;
struct node * current = table->table[index];
struct node * next;
int size = 1;
while (current != NULL) {
next = current->next;
//if(current->data == string){
//return;
//}
if(current-> next == NULL){
//last element in list
current->next = new_node;
table->nentries++;
size++;
printf("\nIndex: %d", index);
printf("\nSize: %d", size);
printf("\nData: %s", current->next->data);
printf("\n");
return;
}
current = next;
size++;
}
}
}
void remove_hash(struct hashtable * this, char * item) {
//unsigned index = hash_string(this, item);
}
int lookup(struct hashtable * this, char * item) {
struct node *temp;
unsigned int index = hash_string(this, item);
temp = this->table[index];
while(temp != NULL) {
// do something
printf("%s, ", temp->data);
if(temp->data == item) {
printf("found %s\n", temp->data);
}
temp = temp->next;
}
return 0;
}
void print(struct hashtable * this) {
int i = 0;
printf("\n Size %d \n", this->size);
if(this == NULL) {
printf("Please construct the hashtable");
return;
}
for (i = 0; i < this->size; i++) {
if(this->table[i] == NULL) {
printf("\n %d: <empty>", i);
} else {
printf("\n %d: %s ", i, this->table[i]->data);
if(this->table[i]->next != NULL) {
printf("%s ", this->table[i]->next->data);
}
}
}
}
int main(int argc, char **argv) {
//struct node *theNode;
struct hashtable *theHash;
theHash = hashtable_new(9);
hashtable_insert(theHash, "I");
hashtable_insert(theHash, "am");
hashtable_insert(theHash, "a");;
hashtable_insert(theHash, "fish");
hashtable_insert(theHash, "glub");
print(theHash);
hashtable_insert(theHash, "glub");
lookup(theHash, "I");
print(theHash);
//printf("\n\n\n");
hashtable_free(theHash);
//print(theHash);
return 0;
}
Since C doesn't let you pass by reference, you can try returning the hashtable then reassigning your variable with the result of hashtable_insert:
struct hashtable *hashtable_insert(struct hashtable *table, char *string) {
// awesome code here
return current;
}
And then call it with:
theHash = hashtable_insert(theHash, "Wow!");