Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hashing Table

Status
Not open for further replies.

qiqinuinaifen128

Programmer
Dec 9, 2007
20
SG
Dear all,

I have a question about hashing,i have create an hash table, and inserting the word and get the hashing key already. Now i want to store my function in to the node, what should i do?
Any recommend would appreciate.

Best Regard
Thank You.

Singapore Swimming Lessons
 
Sorry, can you explain your question?
In particular: what's a function and what's a node?
 
Sorry for make you confuse..

My function mean is a series of command. And the note mean something like a stack can store words, structure, number and so on...

Actually now i have a project on hand that using hash table to store some humum language, and the humum language will point to a stack, so that it can find the command and run it...

Do you have some exercise or tultorial similar one, please kindly provide the link to me

Thank You in advance



Singapore Swimming Lessons
 
Do you mean that you want a hash list that is keyed by arrays of type char that are identifiers for particular stack-like objects?
 
The part of hash table, i have done it, now the problem is i don't know how what i am going to do,

For example, now i have a word "MoveForward". it already stored in the hash table and have a hash key of 27. What should i do in order to link the MoveForward command to the words or hash key, so that next time every time when i want to use it, i just call and it will go to hash table find what i want...
I think it is about the pointer problem , i might type the wrong title.

Sorry my English is now so good...hope you can understand.

Many Thanks

Singapore Swimming Lessons
 
What I think you may have in mind in bare outline...not at all sure. Was bored and most of the code comes from K&R anyway. You should be able to extrapolate from the framework if it's what you had in mind.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STACKSIZE 20
#define HSZ 100
#define OPS 4
#define MAXSTRLEN 50

enum { PUSH, POP, FILL, EMPTY };

typedef struct {
    int len[STACKSIZE];
    void (*method[OPS]) (int[]);
} stackObj;

struct htable {
    char *id;
    struct htable *nxt;
    stackObj *stack;
};

static int listsz = 0;
static struct htable *hlist[HSZ];

void pop(int[]);
void push(int[]);
void randomfill(int[]);
void empty(int[]);

int populatehash(char *);
void walkhash(char *);
void assignops(stackObj *);
unsigned int ahash(char *);
struct htable *alookup(char *);
struct htable *ainstall(char *, char *);

int main(int argc, char **argv)
{

    if (argc != 2) {
	printf("Please enter filename.\n");
	return 1;
    }
    if (populatehash(argv[1]) < 0) {
	printf("Error populating hashtable.\n");
	return 1;
    }
    printf("Table size: %d\n", listsz);
    return 0;
}

/*helpers & templates*/
void pop(int arr[])
{
    return;
}

void push(int arr[])
{
    return;
}

void empty(int arr[])
{
    return;
}

void randomfill(int arr[])
{
    return;
}

int populatehash(char *fname)
{
    FILE *pt;
    struct htable *hpt;
    char linename[MAXSTRLEN];

    if ((pt = fopen(fname, "r")) == NULL) {
	return -1;
    }
    while (fgets(linename, MAXSTRLEN, pt) != NULL) {
	hpt = ainstall(linename, NULL);
	printf
	    ("Key for node at %p is %s.\nstackObj pointer at %p.\nFunctional assignments at pop - %p push - %p fill - %p empty - %p.\n",
	     hpt, hpt->id, hpt->stack, hpt->stack->method[POP],
	     hpt->stack->method[PUSH], hpt->stack->method[FILL],
	     hpt->stack->method[EMPTY]);
	listsz++;
    }
    fclose(pt);
    return 0;
}


/*hashtable specific*/
void assignops(stackObj * pp)
{
    int p;
    pp->method[PUSH] = push;
    pp->method[POP] = pop;
    pp->method[FILL] = randomfill;
    pp->method[EMPTY] = empty;
}

unsigned int ahash(char *value)
{
    unsigned int hval;

    for (hval = 0; *value != '\0'; value++) {
	hval = *value + 31 * hval;
    }
    return hval % HSZ;
}

struct htable *alookup(char *value)
{
    struct htable *walk;
    for (walk = hlist[ahash(value)]; walk != NULL; walk = walk->nxt) {
	if (strcmp(value, walk->id) == 0) {
	    return walk;
	}
    }
    return NULL;
}

struct htable *ainstall(char *value, char *nvalue)
{
    struct htable *new;
    unsigned int hval;

    if ((new = alookup(value)) != NULL) {
	if (nvalue != NULL) {
	    free(new->id);
	    new->id = strdup(nvalue);
	}
	return new;
    }

    if ((new = alookup(value)) == NULL) {
	/*needs error checking */
	new = malloc(sizeof(*new));
	new->id = strdup(value);
	new->stack = malloc(sizeof(*new->stack));
	assignops(new->stack);
	hval = ahash(new->id);
	new->nxt = hlist[hval];
	hlist[hval] = new;
	return new;
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top