Help all! :-) I'm trying to initialize a hash table for a C program and somehow I keep getting errors when it comes to the add() function. I have an array of linked lists and can't seem to get the initilizations right.. Could anyone help me?
PHP Code:
[CODE]#include <stdio.h>
FILE *in_fptr, /* Input File Pointer; points to the file
containing the input to the program */
*out_fptr, /* Output File Pointer; points to the file to
contain all the output of the program */
*fopen();
#define TRUE 1
#define FALSE 0
#define NAME_LENGTH 10 /* maximum number of characters in name */
#define COM_LENGTH 3 /* number of characters in a command */
#define BKT_SIZE 19 /* number of entries in bucket directory */
#define FIRST_FACTOR 2 /* factor used in hashing for the first uppercase letter */
#define LAST_FACTOR 3 /* factor used in hashing for the last uppercase letter */
#define MAXSTRING 100 /* maximum length a file name can be */
/* an entry (node) in hash table */
struct ht_entry {
char ht_name[NAME_LENGTH + 1];
int ref_count; /* number of references to the name */
struct ht_entry *next;
};
struct ht_entry *hash_tbl[BKT_SIZE]; /* bucket directory (hash index) */
// Function prototypes
void init();
int search(char *name_ptr, int *ind_ptr);
int hash(char *name_ptr);
void add(char *name_ptr,int *ind_ptr);
void tbl_flush();
/********************************************************************/
main()
{
init();
printf("\n\n");
system("PAUSE");
}// end of main
/********************************************************************/
void init()
{
/*
This routine is used by the main routine.
It initializes hash_tbl.
It opens the two files (sets in_fptr and out_fptr).
*/
char inp_name[NAME_LENGTH + 1], /* the name in the input */
input_filename[MAXSTRING], /* the name of the input file */
command[COM_LENGTH + 1]; /* the command in the input */
int hash_index;
//gets the name of the input file
printf("\nInput the file name to open (including .txt): ");
scanf("%s", input_filename);
fflush(stdin);
//opens file stream with input_filename to read
if((in_fptr = fopen(input_filename, "r")) == NULL)
{
printf("Can't open %s\n", input_filename);
system("PAUSE");
exit(1);
}
//opens file stream with the output file to write the output
if((out_fptr = fopen("output_file.txt", "w")) == NULL)
{
printf("Can't open output_file\n");
system("PAUSE");
exit(1);
}
//inputs titles to the output file
fprintf(out_fptr, " Input Data\n");
fprintf(out_fptr, " Command Name\n");
//prints contents of input_filename file to output file
while((fscanf(in_fptr, "%s", command))!=EOF)
{
fflush(stdin);
fscanf(in_fptr, "%s", inp_name);
fflush(stdin);
fprintf(out_fptr, " %s", command);
if((strcmp(command, "DEF")==0))
{
//this is where you'd have to search if DEF command exists to define it
fprintf(out_fptr, "%s", " This is a DEF command. ");
hash_index = hash(inp_name);
if((search(command, &hash_index))) /* if DEF exists increment it */
{
printf("Didn't work"); //increment it
}
else /* else add it */
{
add(command, &hash_index); //add it
}
fprintf(out_fptr, "%s", " Hash Index = ");
fprintf(out_fptr, "%i", hash_index);
/**** SEARCH for definition ****/
}
else
{
//this is where you'd have to search if DEF command exists to use it
fprintf(out_fptr, "%s", " This is a USE command. ");
/**** SEARCH for definition ****/
}
fprintf(out_fptr, " %s\n", inp_name);
}// end while loop
}/* end of init */
/********************************************************************/
int search(char *name_ptr, int *ind_ptr) /* name to be searched , hash index (index into the hash table) */
{
return FALSE;
}/* end of search */
/********************************************************************/
int hash(char *name_ptr) /* name to be hashed */
{
char current; /* current letter being evaluated */
int i; /* loop indexer */
int first_upper=0, /* number digit of first uppercase letter */
last_upper, /* number digit of last uppercase letter */
hash_index; /* hash index value to be returned */
for(i=0; i<(strlen(name_ptr)); i++) /* loop for length of name */
{
current=(int)name_ptr[i]; /* parse current letter to int */
if(current>64 && current < 91 && first_upper==0) /* if uppercase letter set first and last values */
{
first_upper=current-64;
last_upper=first_upper;
}
else if(current>64 && current < 91) /* if uppercase set last value */
{
last_upper=current-64;
}
}//end for
hash_index=(first_upper * FIRST_FACTOR + last_upper * LAST_FACTOR) % BKT_SIZE; /* sets hash index value */
return hash_index;
}// end of hash
/********************************************************************/
void add(char *name_ptr,int *ind_ptr)
{
/* PROBLEMS SEEM TO BE HERE WHEN ADDING OR INITIALIZING HASH TABLE */
/* Could someone tell me how to initialize these or link the *tmp to the hash_tbl[]? */
struct ht_entry *tmp;
tmp = malloc(sizeof(*tmp));
hash_tbl[*ind_ptr]->ref_count=1;
if(hash_tbl[*ind_ptr]==NULL)
{
printf("ref_count = %i ", hash_tbl[*ind_ptr]->ref_count);
printf("It's NULL!!");
}
else
{
printf("It's not null!");
}
//while(strcmp((hash_tbl[*ind_ptr]->ht_name), name_ptr)==0){
//}
printf("It's been added!\n");
}//end add
/*******************************************************************/
void tbl_flush()
{
}/* end of tbl_flush */
[/CODE]