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

#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<process.h>
#include<conio.h>







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<<runner->name<<endl;
 runner=runner->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<<runner->name<<endl;
res=1;
break;
}
i++;
runner=runner->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;i<n;i++){
for(int j=0;j<n-1;j++){
    if(GetKeySpace(j)>GetKeySpace(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<maxSize;i++)
       a[i].BubbleSort();
       }


       void Display(){


       for(int i=0;i<maxSize;i++)
           a[i].DisplayAll();

       }


       int CountNodeOfHash(int hashkey){

       return a[hashkey].CountNode();
       }


private:
    LinkList *a,priority;

int maxSize;
};



void main(){

Hashing ob(26);
int no;
char name[256];

for(;;){
system("cls");

cout<<"\n1-ADD\n2-HASH SEARCH\n3-MODIFY\n4-DELETE\n5-DISPLAY ALL\n6-HASH KEY\n7-COUNT NODES ON HASH\n8-SORT STRINGS\n9-EXIT:";
cout.flush();
cin>>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<<k;
cout.flush();
getch();
       break;


       }

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



k=ob.HashKey(name);
       temp=ob.CountNodeOfHash(k);
cout<<"NUMBER OF NODES ON HASH #  "<<k<<"   ARE : "<<temp;cout.flush();
getch();

break;


       }




case 8:{
    system("cls");
ob.Sort();

       break;

       }
case 9:{

       exit(0);
       break;

       }


}








}









}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Comments
I doubt he will care after FIVE years! And no code tags, plus "rusted" C++ code, in a C forum !!!

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. :(

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

   return 0;
   }

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.