Added just to help other....

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void createHashTable(void);
void getData(void);
void formatting(void);
int insertData(int);
int collision_OpenHashing(struct hashOpen*& ,int);
int generateKey(int);
void DispData(void);
void DispHash(void);

#define dataSize 15
#define empty -1
#define fail 0
#define success 1

	struct hashOpen{
			int item;
			struct hashOpen *next;
			}*head, hashTable[10];

int dataArray[dataSize];
struct hashOpen* cur;
int count, status;
int filledSpaces = 0;
float loadFactor = 0;

void main(void)
{
       clrscr();
       formatting();

       getData();
       createHashTable();

       count = 0;
       do
       {

	DispData();
	DispHash();
	printf("\ncount=%d", count);

	status = insertData(dataArray[count]);
	count++;
	}  while (count <= dataSize);

	printf("\n\n-------------Programming Successfully Completed-------------");
	getch();
}

void createHashTable(void)
{
	int i;
	/******** Initialize HashTable ************************/
	for( i = 0; i <= 9; i++)
	{
		hashTable[i].item = empty;
		hashTable[i].next = NULL;
	}
}

void getData(void)
{
	int i;
	randomize();
	for(i =0; i < dataSize; i++)
		dataArray[i] = random(1)+2;
}

void DispData(void)
{
	int i;
	printf("\nData[]={");
	for(i =0; i < dataSize; i++)
		printf("%d,", dataArray[i]);
	printf("\b}");
}

void DispHash(void)
{
       int count;
       printf("\nHash Table content = [");
       for(count = 0; count < 9 ; count++)
       {
		printf("%d, ", hashTable[count].item);
		cur++;
       }
       printf("\b\b]");
}

void formatting(void)
{
	int i;
	printf("\n");
	for(i =0; i<=79 ; i++)
		printf("*");

	printf("\n......... Prgogram title\t\t# Hash Table");
	printf("\n......... Created by\t\t        # Romasa Qasim");
	printf("\n......... Description\t\t 	# Creation of Hash Table, generation of,");
	printf("\t\t\t\t\t  Key, Insertion, Searching and Deletion");
	printf("\t\t\t\t\t  of data in the Hash Table\n");

	for( i =0; i<=79 ; i++)
		printf("*");
}

int insertData(int data)
{
	int hashIndex;
	hashIndex = generateKey(data);
	if( hashTable[hashIndex].item == empty)
	{
		hashTable[hashIndex].item = data;
		return(success);
	}

	else
		return(collision_OpenHashing(hashTable[hashIndex].next, data));

}

int collision_OpenHashing(struct hashOpen *&pNode, int data)
{
	if(pNode == NULL)
	{
		head = (struct hashOpen*)malloc(sizeof(struct hashOpen));
		head->item = data;
		head->next = NULL;
		pNode = head;
	}

	else
		collision_OpenHashing(pNode->next, data);

}

int generateKey(int data)
{
	int key;
	key = data % 10;
	return(key);
}

Recommended Answers

All 4 Replies

For better performance if the no.of elements is very large us a large primary value as the hash key for better hashing.

hey friends just come across this program and found it very fruitfull....but would be more so happy if some one coould explain what tasks are we trying to achieve....or in short how would i be able to explain this program to some one ...so if someone with good knowledge about this topic can explain to me what

void createHashTable(void);
void getData(void);
void formatting(void);
int insertData(int);
int collision_OpenHashing(struct hashOpen*& ,int);
int generateKey(int);
void DispData(void);
void DispHash(void)

what the following functions are doing in this program

please some one who can guide me through various stages of this program so i can grasp it properly and be able to explain and perform it !!

regards,
Rash

void createHashTable(void);
This function creates a hash table in which the data items are to be inserted in sorted form for better searching

void getData(void);
This function collects data from user to be inserted in the hash table

void formatting(void);
This function is just to beautify the output of the program

int insertData(int);
This function ineserts the data in the hash table

int collision_OpenHashing(struct hashOpen*& ,int);
This function works when there is a collision occured in the table e.g if two data items with same key are to be inserted in the hash table then there must be a collision, both data items are the candidate for the same cell of hash table. To remove this collision This function acts as a collision resolver between that two data items.

int generateKey(int);
This function generates the key for the data items to be inserted with the help of a formula

void DispData(void);
This function displays the data to the end user

void DispHash(void)
This function displays the state of hash table after each entry
----------------------------------------------------------------------------------------------------

this is the introduction of all the funstion's performance, if you need description of coding then do acknowledge me.

Regards,

Romasa

Thanks alot ramosa....very you`ve described all the functions very nicely....and i`ve understood most of it...Thanks once again !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.