Here I have a really detailed explanation of my homework and the problems in the code. There are two parts of the homework, and the code for the first part works, but after modifying it for the second part, it doesn't work anymore.
The homework: Save the person's name and fastest running time using Dictionary(with open hashing, that is, Dictionary is represented with hash-table which is an array with B elements, where each element of the array is a linked list of elements of type celltype. Hash table is using hash function h(name) where for i=1,...,B-1, if h(name)=i, then data about the person with the 'name' is saved in the linked list in the i-th element of the Dictionary. People are sorted by their name.)
Here's how it looks like.
#define B 20
typedef struct celltag{
char name[11];
double time;
struct celltag *next;
} celltype;
typedef celltype **Dictionary;
int h(char name[]){
int i, sum=0;
for(i=0;name[i]!='\0';i++) {
sum+=name[i];
}
return sum % B;
}
void DiMakeNull (Dictionary *Ap){
int i;
for(i=0;i<B;i++) (*Ap)[i]=NULL;
}
void DiInsert(char name[], double time, Dictionary *Ap){
int bucket;
celltype *oldheader;
if(DiMember(name, *Ap)==0){
bucket=h(name);
oldheader=(*Ap)[bucket];
(*Ap)[bucket]=(celltype*)malloc(sizeof(celltype));
strcpy((*Ap)[bucket]->name, name);
(*Ap)[bucket]->time= time;
(*Ap)[bucket]->next=oldheader;
}
}
(Function DiMember(char name[], Dictionary A) returns 1 if person is in the dictionary, 0 if not.)
This part works as it should.
Now, the next part of the homework: Sort the people by their running time using another pointer array, where the first element of the array points to the data of the fastest person, the second element to the second fastest person etc.
My idea was to make another Dictionary, let's call it Dictionary Array. Now, while inserting a new person's data into the original Dictionary Ap, I wanted to call another function void Sort(celltype *Data, Dictionary *Array) which takes celltype in which the new person's data is stored and the Array in which we will insert it sorted by the time.
Because I would use this in DiInsert, I modified it into
void DiInsert(char name[], double time, Dictionary *Ap, Dictionary *Array){
int bucket;
celltype *oldheader;
if(DiMember(name, *Ap, 0)==0){
bucket=h(name);
oldheader=(*Ap)[bucket];
(*Ap)[bucket]=(celltype*)malloc(sizeof(celltype));
strcpy((*Ap)[bucket]->name, name);
(*Ap)[bucket]->time= time;
(*Ap)[bucket]->next=oldheader;
celltype *send;
send=(*Ap)[bucket];
send->next=NULL;
Sort(send, Array);
}
}
(Everything is the same, except I added another parameter into the function, and I added that bit (celltype *send; ... Sort(send, Array);) at the end.)
And this is how the function looks:
void Sort(celltype* Data, Dictionary *Array){
if ((*Array)[0]==NULL){
(*Array)[0]=Data;
(*Array)[0]->next=NULL;
}
else{
int i;
for(i=0;(*Array)[i]!=NULL;i++){
if(Data->time < (*Array)[i]->time){
celltype *new;
new=(*Array)[i];
(*Array)[i]=new;
(*Array)[i]->next=NULL;
int j=i+1;
while((*Array)[j]!=NULL){
celltype *old;
old=(*Array)[j];
(*Array)[j]=new;
(*Array)[j]->next=NULL;
new=old;
j++;
}
(*Array)[j]=new;
(*Array)[j]->next=NULL;
break;
}
}
}
}
These are my problems:
There is a problem in the call of the function Sort/function parameters. I still have some problems understanding pointers so I'm not really sure how to fix it.
Before modifying the DiInsert, the first bit of code worked. Now, after modifying and adding the void Sort(), for some reason it sometimes doubles the data in the original Dictionary Ap and saves it in the correct i-th bracket, but also in some other bracket as well.
It also doubles the data in the Dictionary Array. For example, if I input 'Mark' as name and 10.1 as time, h(Mark)=15, so it saves Mark's data in the 15-th bracket of the Dictionary Ap. Then, it saves Mark's data in the 0th bracket in the Dictionary Array (as it should, because Mark's data is the first data inputed and he's the fastest one for now) but then it ALSO saves it in the 16-th bracket. Why?