954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Hashing guidance

Hey, i'm not too familiar with c and i need to write a hashing program that takes in a int and a a string. I'm trying to use separate chaining. ok here's my hash function, basic one:

int hash(int key )
{
return key mod TableSIZE

}

here's my stuct to represent an hastable with an array of linklist, i don't know much of C i don't know if this is right.
struct CITYNAME{

char school[MAX_CHAR];
struct CITYNAME *next;
}

struct HashTable
{
int keys[MAX]; // prime number mostly
struct CITYNAME schools[];
}

is that right? if not help me out guys and gals :lol: .

and one more thing to insert, well i have it started , h is my hastable
void insert ( int key, const char* school )
{
int index = hash(key);
h[index] = key; // where the key goes
// now how and where do i insert the string?
h[index]...
..........

}

like i said i don't much of C,i know i have to allocate memory, well that's all my instructor said lol, and this is our first C program can you believe that? :rolleyes: some teachers are just crazy to me. any help would be appreciated

thanks,
Mel

Meldroz
Newbie Poster
11 posts since Mar 2004
Reputation Points: 10
Solved Threads: 0
 

#include
#include
#include
#include
#include
#include

struct LList{
char name[256];

int keySpace;
int index;
LList *next;
};
class LinkList{
public:
LinkList(){head=NULL;
temp=NULL;
used=0;
runner=NULL;};
~LinkList(){};
void AddNode(char *n);
int GetKeySpace(int i);
void DeleteNode(int index);
int CountNode();
void ModifyNode(char *n,int index);
void InsertNode(char *n,int index);
void DisplayNode(int index);
void DisplayAll();
char *GetNode(int i);
void DisplayIndexes();
void BubbleSort();
int GetIndexNode(int i);


int HashKey(char *str){
char temp[256];
int key=0,i=0;

strcpy(temp,str);
key=temp[i];
return key;
}

private:
struct LList *temp;
struct LList *head;
struct LList *runner;
int used;
};

/////////////////ADD NODE////////////////////////
void LinkList::AddNode(char *n){
if(head==NULL)

{
head=(LList*) malloc(sizeof(LList));
strcpy(head->name,n);
head->keySpace=HashKey(n);
head->next=NULL;
used++;
}
else
{runner=head;
while(runner->next!=NULL){
runner=runner->next;
}
runner->next=(LList*)malloc(sizeof(LList));
runner=runner->next;
strcpy(runner->name,n);
runner->keySpace=HashKey(n);
runner->next=NULL;
used++;
}
}

//////////////////GET NODE////////////
char* LinkList::GetNode(int index){
int i=0;
if(index>CountNode())
{
cout<<"INDEX IS GREATER THAN USED IN GET NODE FUNCTION";
exit(0);
}
runner=head;
while(runner!=NULL)
{
if(i==index){
return runner->name;
break;}
runner=runner->next;
i++;
}
}


int LinkList::GetKeySpace(int index){
int i=0;
if(index>CountNode())
{
cout<<"INDEX IS GREATER THAN USED IN GET NODE FUNCTION";
exit(0);
}
runner=head;
while(runner!=NULL)
{
if(i==index){
return runner->keySpace;
break;}
runner=runner->next;
i++;
}
}


///////////////////COUNT NODE/////////////////
int LinkList::CountNode(){
return used;

}

///////////////////DISPLAY ALL NODES///////////////
void LinkList::DisplayAll(){
runner=head;
while(runner!=NULL){
cout<name<next;

}
}

/////////////////////////DISPLAY NODE//////////////////////////
void LinkList::DisplayNode(int index){
int i=0;int res=0;
int c=0;
c=CountNode();
if(index>c){
cout<<"INDEX IS GREATER THAN USED!!!";
exit(0);}

runner=head;
while(runner!=NULL){
if(i==index){
cout<name<next;
}
if(res==0)
cout<<"NOT FOUND!!!";
}

///////////////////////////MODIFY NODE//////////////////////////////
void LinkList::ModifyNode(char *n,int ind){
int i=0,res=0;
int c=0;
c=CountNode();
if(ind>c){
cout<<"INDEX IS GREATER THAN USED!!!";
exit(0);
}
runner=head;
while(runner!=NULL){
if(i==ind)
{
strcpy(runner->name,n);
runner->keySpace=HashKey(n);
res=1;
break;
}
runner=runner->next;
i++;
}

if(res==0)
cout<<"NOT FOUND!!!";

}

//////////////////////////////INSERT NODE//////////////////////////
void LinkList::InsertNode(char *n,int index){
int i=0;
runner=head;
while(runner!=NULL){
if(index==i){
temp=(LList*)malloc(sizeof(LList));
temp->next=runner->next;
strcpy(temp->name,n);
runner->next=temp;
used++;
break;
}


i++;
runner=runner->next;
}
}

/////////////////////////////DELETET NODE//////////////////////
void LinkList::DeleteNode(int index){
int i=0;
int c=CountNode()-1;
if(index>c){
cout<<"INDEX IS GREATER THAN USED!!!";
exit(0);
}
if(index==0){used--;
runner=head;
head=head->next;
free(runner);
}
else
{used--;
index=index-1;
runner=head;
i=0;
while(runner->next!=NULL){
if(i==index){
temp=runner->next;
runner->next=temp->next;

free(temp);
break;
}

runner=runner->next;
i++;

}

}
}



//////////////////BUBBLE SORT/////////////
void LinkList::BubbleSort(){
runner=head;
char a[256],t[256];
temp=head;
int n=CountNode();

for(int i=0;iGetKeySpace(j+1)){
strcpy(a,GetNode(j));
strcpy(t,GetNode(j+1));
ModifyNode(a,j+1);
ModifyNode(t,j);
}
}
}
}

//////////////////GET NODE////////////
int LinkList::GetIndexNode(int ind){
int i=0;
if(ind>CountNode())
{
cout<<"INDEX IS GREATER THAN USED IN GET NODE FUNCTION";
exit(0);
}
runner=head;
while(runner!=NULL)
{
if(i==ind){
return runner->index;
break;}
runner=runner->next;
i++;
}
}






class Hashing{


public:Hashing(int size){
maxSize=size;
a=new LinkList[maxSize];
}

~Hashing(){}


//////////////////////ADD///////////////////////
void Add(char *str){
int check=0;
int Pno=HashKey(str);

if(Pno<=maxSize){
a[Pno].AddNode(str);

}
}


////////////////////HASH search///////////////////////
void HashSearch(char *str){
char temp[256];
strcpy(temp,str);


int index=HashKey(str);
a[index].DisplayAll();

}


///////////////////HASH KEY////////////////
int HashKey(char *str){
char temp[256];
int key=0,i=0;

strcpy(temp,str);

while(temp[i]!='\0'){
key=key+temp[i];
i++;

}

return key%maxSize;


}

/////////////DELETE///////////////
void Delete(char *str,int i){

int index=HashKey(str);
a[index].DeleteNode(i);

}


/////////MODIFY///////////
void Modify(char *str,int i){

Delete(str,i);
char temp[256];
cout<<"ENTER NEW NAME : ";cout.flush();
gets(temp);
int index=HashKey(temp);
a[index].AddNode(temp);



}


void Sort(){
for(int i=0;i>no;
switch(no){

case 1:{system("cls");
cout<<"ENTER STRING : ";cout.flush();
gets(name);
ob.Add(name);
break;
}


case 2:{system("cls");
cout<<"ENTER STRING : ";cout.flush();
gets(name);
ob.HashSearch(name);
getch();
break;
}


case 3:{system("cls");
int index;
cout<<"ENTER STRING : ";cout.flush();
gets(name);
cout<<"ENTER INDEX";cout.flush();
cin>>index;

ob.Modify(name,index);
break;
}

case 4:{system("cls");
cout<<"ENTER STRING : ";cout.flush();
gets(name);
int index;
cout<<"ENTER INDEX";cout.flush();
cin>>index;

ob.Delete(name,index);
break;
}

case 5:{

system("cls");
ob.Display();
getch();
break;


}


case 6:{
system("cls");
int k=0;
cout<<"ENTER STRING : ";
cout.flush();
gets(name);

k=ob.HashKey(name);
cout<<"ITS HASH KEY IS : ";cout.flush();
cout<

hammadahmed11
Newbie Poster
1 post since Jun 2009
Reputation Points: 8
Solved Threads: 0
 

well, that sure was a helluva lot of effort you put into "solving" someone's 5-year-old homework problem.

it almost pains me to tell you that it's completely broken and won't compile on any standard C compiler. :(

[code=c]

int main(void)
   {
      // in the future, code tags would also be nice
   
   return 0;
   }


[/code]


.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You