| | |
hash- linear probing
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2007
Posts: 17
Reputation:
Solved Threads: 1
im a student of 2nd year BS, doing my BS in Computer Science.
Here I want to include a program by me, may be someone will be helped by me
Here I want to include a program by me, may be someone will be helped by me

c Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<stdlib.h> int* createHashTable(void); void getData(void); void formatting(void); int insertData(int); float chkLoadFactor(int, int); int collision_LinearProbing(int, int,int); int generateKey(int); void rehashing(void); #define dataSize 15 #define empty -1 #define fail 0 #define success 1 int dataArray[dataSize]; int hashSize = 5; int* head; int* cur; void main(void) { int count, status; int filledSpaces = 0; float loadFactor = 0; clrscr(); formatting(); getData(); head = createHashTable(); for( count = 0; count < dataSize ; count++) { if(loadFactor > 0.5) rehashing(); status = insertData(dataArray[count]); if(status == success) filledSpaces++; loadFactor = chkLoadFactor(status, filledSpaces); if(loadFactor <= 0.5) printf("LF aftr %d is = %f\t", dataArray[count], loadFactor); if(status == fail) printf("\nNo room left for %d\n", dataArray[count]); } cur = head; printf("\n\nHash Table content = ["); for(count = 0; count < hashSize ; count++) { printf("%d, ", *cur); cur++; } printf("\b\b]"); getch(); } int* createHashTable(void) { int i; /********************** Allocating Memory for Hash Table *********************/ head = (int*)malloc(sizeof(hashSize*2)); cur = head; /********************** Initialize HashTable *********************************/ for( i = 0; i <=hashSize; i++) { *cur = empty; cur++; } cur = head; return(head); } void getData(void) { int i; randomize(); printf("\n"); printf("Data[]={"); for(i =0; i < dataSize; i++) { dataArray[i] = random(50); printf("%d,", dataArray[i]); } printf("\b}\n\n"); } 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,count = 0; hashIndex = generateKey(data); if( *(head+hashIndex) == empty) { *(head+hashIndex) = data; return(success); } else return(collision_LinearProbing((++hashIndex%hashSize), count, data)); } float chkLoadFactor(int status, int filledSpaces) { float loadFactor; if (status == success) { loadFactor = filledSpaces / (float)hashSize; return(loadFactor); } else return(1); } int collision_LinearProbing(int hashIndex, int count, int data) { if(count == hashSize) return(fail); if (*(head+hashIndex) == empty) { *(head+hashIndex) = data; return(success); } else { collision_LinearProbing((++hashIndex % hashSize), ++count, data); } } int generateKey(int data) { int key; key = data % hashSize; return(key); } void rehashing(void) { int count; int status; hashSize = hashSize * 2; head = (int*)realloc(head, sizeof(hashSize*2)); for( count = 0; count <hashSize; count++) { *(head+count) = -1; } for(count = 0; count < dataSize; count++) { status = insertData(dataArray[count]); } }
Last edited by Narue; Dec 12th, 2007 at 3:22 pm. Reason: Added code tags
Hey, we appreciate your goodwill but this is not the way to do so. Go to 'contribute code' in the 'code snippets' section. Forums are meant only for problem solving.
Last edited by Jishnu; Dec 13th, 2007 at 5:49 am.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
![]() |
Similar Threads
- Reading txt file into Hash Table (C++)
- Dictionary ADT (Computer Science)
- Help needed in designing a c++ program using hashing and linear probing!! (C++)
- exam technique? (C++)
- Array required, but java.lang.String and java.util.Vector found (Java)
- array required, but java.lang.String found and java.util.Vector found? (Community Introductions)
Other Threads in the C Forum
- Previous Thread: Binary Tree
- Next Thread: using strstr to find words
| Thread Tools | Search this Thread |
#include adobe ansi api array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware highest histogram inches include incrementoperators input iso kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming stack standard string strings structures systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






