473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Hash table: some lines of code involving pointers

I wrote a program which inserts and finds elements in an hash table.
Each element of the table is a dinamic list, which holds all elements
having the same hash value (calculated by an int hashFunction(char
*key, int elements) ), so the table is an array of dynamic lists.
The version of the program I've attached to this message works fine;
but it's the result of an accurate debugging. I discovered that my
problems consisted in just 4 lines of code. Now I solved them, but I
still haven't realized why! I 'd like to know what exactly was wrong
with my old code (in just 4 lines, I repeat) and what is right with
the new one.
The 4 lines of code are contained in this function:

void insertElement(elementType *table[], elementType *element){

int position;
elementType *last;

position = hashFunction(element->ID, TABLE_ELEMENTS);

elementType *elem;
elem = (table[position]);

if (elem == NULL){
table[position] = element;
}
else{
last = table[position];
while (last->next != NULL){
last = last->next;
}
last->next = element;
}
}

Now try to change the lines:

while (last->next != NULL){
last = last->next;
}
last->next = element;

with that of the old version of the program:

while (last != NULL){
last = last->next;
}
last = element;

So what's wrong with this? And why do the other four lines work? (The
more I read the two versions, the more I think they are equivalent).
Thanks

Simba
/*********************************************/

Here is the complete program:
Hint: if you want to test the two versions of the program, insert at
run time these IDs: a, b, f, l, h. (a and f, b and l, have the same
hash value).

#include <stdio.h>
#include <string.h>

#define TABLE_ELEMENTS 5
#define FALSE 0
#define TRUE 1

struct elementType{
char *ID;
char *information;
struct elementType *next;
};
typedef struct elementType elementType;

/*
hashFunction
given a key, it returns the corresponding table index
input: key, table elements
output: table index
*/
int hashFunction(char *key, int elements){
int value = 0;
for (; *key; key++){
value+= (7 * (*key));
}
return (value % elements);
}

/*
inputElement
it reads a new element
input: none
output: pointer to the element just read
*/
elementType* inputElement(){

elementType *element = (elementType*)malloc(sizeof(elementType));
element->ID = (char*)malloc(20);
element->information = (char*)malloc(20);
element->next = NULL;

printf ("ID: ");
scanf ("%s", element->ID);
printf ("Informazione: ");
scanf ("%s", element->information);
printf("\n");
return element;
}

/*
insertElement
it inserts a new element in the table
input: pointer to the table, pointer to the element
to insert
output: none
*/
void insertElement(elementType *table[], elementType *element){

int position;
elementType *last;

position = hashFunction(element->ID, TABLE_ELEMENTS);

elementType *elem;
elem = (table[position]);

if (elem == NULL){
table[position] = element;
}
else{
last = table[position];
while (last->next != NULL){
last = last->next;
}
last->next = element;
/* what's wrong?
while (last != NULL){
last = last->next;
}
last = element;
*/

}

}
/*
initializeTable
it initializes the table with the NULL value for each
element
input: pointer to the table, its number of elements
output: none
*/
void initializeTable(elementType *table[], int elements){

int i;
for(i = 0; i < elements; i++){
table[i] = NULL;
}
}
/*
printElement
it prints an element's fields
input: pointer to the element to print
output: none
*/
void printElement(elementType *element){

printf ("ID: %s\n", element->ID);
printf ("Informazione: %s\n", element->information);
printf ("\n");
}
/*
printTable
prints all the not-NULL values of the table
input: pointer to the table, its number of elements
output: none
*/
void printTable(elementType *table[], int elements){
int i;
elementType *element;

for(i = 0; i < elements; i++){
element = table[i];
while (element != NULL){
printElement(element);
element = element->next;
}
}
}
/*findElement
it finds an element in the table
input: pointer to the table, number of elements, key of
the element to find
output: element's index, if it was find
-1, else
*/
int findElement(elementType *table[], int elements, char *ID){

int position;
int found;
elementType *element;

position = hashFunction(ID, elements);

found = FALSE;

if ((table[position] != NULL) && (strcmp(table[position]->ID, ID)
== 0)){ //trovata al primo colpo
found = TRUE;
}
else{
element = table[position];
while ((element != NULL) && (found == FALSE)){
if (strcmp(element->ID, ID) == 0){
found = TRUE;
}
element = element->next;
}
}

return ((found == TRUE)? position : -1);
}
int main(){

int i;
char *key = (char*)malloc(20);
int position;
elementType *table[TABLE_ELEMENTS];

initializeTable(&table, TABLE_ELEMENTS);

printf ("Insert %d elements: \n\n", TABLE_ELEMENTS);

for (i = 0; i < TABLE_ELEMENTS; i++){
insertElement(&table, inputElement());
}
printf ("\nYou inserted:\n\n");
printTable(&table, TABLE_ELEMENTS);
printf("\n\n");

printf ("Insert a key to find: ");
scanf ("%s", key);

position = findElement(&table, TABLE_ELEMENTS, key);

if (position >= 0){
printf ("The element was found at the position %d.\n\n",
position);
}
else{
printf ("The element wasn't found.\n\n");
}

system("PAUSE");
return 0;
}
Nov 14 '05 #1
3 2501
Simone Battagliero wrote:
.... snip ...
void insertElement(elementType *table[], elementType *element){

int position;
elementType *last;

position = hashFunction(element->ID, TABLE_ELEMENTS);

elementType *elem;
elem = (table[position]);
You are using some sort of non-portable extension here. You
should not declare objects after executable code. Move these up
after "elementType...".

if (elem == NULL){
table[position] = element;
}
else{
last = table[position];
while (last->next != NULL){
last = last->next;
}
last->next = element;
}
}

Now try to change the lines:

while (last->next != NULL){
last = last->next;
}
last->next = element;

with that of the old version of the program:

while (last != NULL){
last = last->next;
}
last = element;

So what's wrong with this? And why do the other four lines work? (The
more I read the two versions, the more I think they are equivalent).


last is a local object, not a member of the table.

For a portable functional hash system, see:

<http://cbfalconer.home.att.net/download/hashlib.zip>

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #2
CBFalconer <cb********@yahoo.com> wrote:
Simone Battagliero wrote:

... snip ...

void insertElement(elementType *table[], elementType *element){

int position;
elementType *last;

position = hashFunction(element->ID, TABLE_ELEMENTS);

elementType *elem;
elem = (table[position]);


You are using some sort of non-portable extension here. You
should not declare objects after executable code.


C99, perhaps?
Nov 14 '05 #3
> > Now try to change the lines:

while (last->next != NULL){
last = last->next;
}
last->next = element;

with that of the old version of the program:

while (last != NULL){
last = last->next;
}
last = element;

So what's wrong with this? And why do the other four lines work? (The
more I read the two versions, the more I think they are equivalent).
last is a local object, not a member of the table.


So the problem is with the last assignment, isn't it?
You want to say that when I write

last = element;

I'm just changing a local element; instead with

last->next = element;

I change an element of the table, rigth?
Thanks for your help, CBFalconer
You already got help with your problem, but the next lines can be
replaced with (unless you have some weird requirement that older
elements must come last?)

element->next = elem;
table[position] = element;


Thanks Daniel for this nice observation.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
4
by: flipdog | last post by:
Hello all, I didn't know there is a thread on hash function started in this newsgroup so reposted my posting from another group. Hope I can have some feedbacks. I am new to hash table. I came...
4
by: Simone Battagliero | last post by:
I wrote a program which inserts and finds elements in an hash table. Each element of the table is a dinamic list, which holds all elements having the same hash value (calculated by an int...
2
by: Pieter Claassen | last post by:
But, last few questions if I may: Here is a test program that compiles and works #include <search.h> #include <stdio.h> #include <string.h> typedef struct {
12
by: wxs | last post by:
Many times we have a bunch of enums we have from either different enums or the same enum that will have various numeric values assigned. Rarely will there be collisions in numbering between the...
7
by: Diane | last post by:
Hi everybody. Does anyone have any sample hash table implementation for separate chaining? I'm trying to implement it myself, but I'm stuck. Thanks!
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
13
by: ababeel | last post by:
Hi I am using a calloc in a hash table program on an openvms system. The calloc is used to declare the hash table char **pHashes; pHashes = calloc(hash_size,sizeof(char *)); //hash_size = 101 ...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.