b_vishwajit
asked on
integer from pointer (Part2)
Ok you can look at the history of this question at this pointer http://oldlook.experts-exchange.com/questions/20974846/error-integer-from-pointer.html
I have to finish two other functions and I am done. I wont post any questions unless and until I am stuck at some stage. Thanks in advance.
I have to finish two other functions and I am done. I wont post any questions unless and until I am stuck at some stage. Thanks in advance.
ASKER
Kdo dont worry. Just look at the history. I am trying to finish my program.
I said
>>I have to finish two other functions and I am done
If get any problems while implementing I will post them.Thanks.
I said
>>I have to finish two other functions and I am done
If get any problems while implementing I will post them.Thanks.
ASKER
Hi I am using linear probing to insert district numbers into hash table and here is the code snipptet which does that:
if(tableSize!=MAX_DISTRICT S)
{
if(keyArray[pos]==0)
{
keyArray[pos]=districtNo;
}
else
{
while(keyArray[pos]!=0&&po s<=MAX_DIS TRICTS)
{
pos++;
}
if(pos==MAX_DISTRICTS)
{
pos=0;
while(keyArray[pos]!=0)
pos++;
}
keyArray[pos]=districtNo;
}
tableSize++;
}
It works without any problems.
MAX_DISTRICTS=31
hashFunction(k)=k%MAX_DIST RICTS.
If there are any problems with this code snippet then please mention it here. Thanks.
if(tableSize!=MAX_DISTRICT
{
if(keyArray[pos]==0)
{
keyArray[pos]=districtNo;
}
else
{
while(keyArray[pos]!=0&&po
{
pos++;
}
if(pos==MAX_DISTRICTS)
{
pos=0;
while(keyArray[pos]!=0)
pos++;
}
keyArray[pos]=districtNo;
}
tableSize++;
}
It works without any problems.
MAX_DISTRICTS=31
hashFunction(k)=k%MAX_DIST
If there are any problems with this code snippet then please mention it here. Thanks.
A possible problem:
while(keyArray[pos]!=0&&po s<=MAX_DIS TRICTS)
{
pos++;
}
//if the loop runs till pos<=MAX_DISTRICTS,here pos=MAX_DISTRICTS+1
//The following if wont execute and it will try to insert at keyarray[MAX+1]
//which is not allocated for you.
if(pos==MAX_DISTRICTS)
{
pos=0;
while(keyArray[pos]!=0)
pos++;
}
keyArray[pos]=districtNo;
You store in keyarray from 0 or 1?
Since the initial value is 0 in all keyarray values,i have to assume that you dont allow 0 as a district number,in which case you'd be storing from 1.
while(keyArray[pos]!=0&&po
{
pos++;
}
//if the loop runs till pos<=MAX_DISTRICTS,here pos=MAX_DISTRICTS+1
//The following if wont execute and it will try to insert at keyarray[MAX+1]
//which is not allocated for you.
if(pos==MAX_DISTRICTS)
{
pos=0;
while(keyArray[pos]!=0)
pos++;
}
keyArray[pos]=districtNo;
You store in keyarray from 0 or 1?
Since the initial value is 0 in all keyarray values,i have to assume that you dont allow 0 as a district number,in which case you'd be storing from 1.
ASKER
In an array elements are stored from 0 right? That is they are indexed starting from 0.
ASKER
Yeha I get your point now. Instead of using <= I should just use < and that will solve that minor problem. Is that what you meant?
Thanks.
Thanks.
Yeah.
ASKER
ok. Now I have finished everything. Just gimme sometime. I am debugging my code.If there are errors I would like to know them:
http://www.geocities.com/b_vishwajit/
You can download my file from the above link and the name of the file is schools.c. If you have any problems downloading let me know. I especialy want to know if delete() function and displaySchoolList() function are working properly.Thanks a lot.
http://www.geocities.com/b_vishwajit/
You can download my file from the above link and the name of the file is schools.c. If you have any problems downloading let me know. I especialy want to know if delete() function and displaySchoolList() function are working properly.Thanks a lot.
ASKER
Input format:
INPUT FORMAT:
No duplicate district numbers into the hashtable or duplicate school names into the same linked list.
User enters the data of the type that is to be expected (this means for strings user will enter a string and for ints user will enter an int).
only use positive integers will be used to insert into the hashtable so you will not need to check that case. However, you will still need to make sure that all menu selections are in range.
Each school name has a maximum of 25 printable characters and will not contain any spaces.
School name is case-sensitive.
Thanks.
INPUT FORMAT:
No duplicate district numbers into the hashtable or duplicate school names into the same linked list.
User enters the data of the type that is to be expected (this means for strings user will enter a string and for ints user will enter an int).
only use positive integers will be used to insert into the hashtable so you will not need to check that case. However, you will still need to make sure that all menu selections are in range.
Each school name has a maximum of 25 printable characters and will not contain any spaces.
School name is case-sensitive.
Thanks.
ASKER
I see that everything is working properly. Did you find any errors.
Found 2 bugs:
First,if you delete the first school in the list for a district,it displays some junk values for the first node.
Second,If you enter districts >31,it hashes correctly but when displaying the school list,it should display the district number not the hashed index number.
For e.g. if you enter district 31 and then display the school list,it prints School list for district:0
First,if you delete the first school in the list for a district,it displays some junk values for the first node.
Second,If you enter districts >31,it hashes correctly but when displaying the school list,it should display the district number not the hashed index number.
For e.g. if you enter district 31 and then display the school list,it prints School list for district:0
Also,in your scanf after an invalid selection,you've missed out the &.
while(choice!=1.......
{
scanf("%d",&choice);
^
}
while(choice!=1.......
{
scanf("%d",&choice);
^
}
>For e.g. if you enter district 31 and then display the school list,it prints School list for >district:0
The list is ok.only the number displayed is incorrect
The list is ok.only the number displayed is incorrect
ASKER
Ok I am fixing them now. Just stay here. Thanks.
ASKER
Allright I fixed two problems. Left with one more i.e. option 4 prints a school list even thought there are no schools in the list. How can I avoid that? Thanks.
You can download the updated file from the same link.Thanks.
You can download the updated file from the same link.Thanks.
For deletion of first node,the problem is that distArray[] for that district number still points to the node that has now been deleted.
You need to pass the district number to the delete function so that you can access the distArray[] at the right position.
I have done the debugging for you but i wont give it to you.
Its better that you do it yourself.
You need to pass the district number to the delete function so that you can access the distArray[] at the right position.
I have done the debugging for you but i wont give it to you.
Its better that you do it yourself.
But it displays an empty list.
Thats allright.If you want it to display that there are no schools in the list,just check the corresponding distArray[] value for that district.If its NULL,there arent any schools.
Thats allright.If you want it to display that there are no schools in the list,just check the corresponding distArray[] value for that district.If its NULL,there arent any schools.
I dont see any changes in your delete function.
ASKER
I have almost lost my patience. But I know what changes I have to make in delete. I will work on that now. Thanks.
ASKER
I have made some changes to the delete function(). Can you please check it.
delete(struct node *q,char* findName,int dist)
{
struct node *old, *temp;
temp=q;
while(temp!=NULL)
{
if(strcmp(temp->name,findN ame)==0)
{
if(temp==q)
{
q=temp->next;
free(temp);
distArray[hashFunction(dis t)]=q;
return;
}
else
{
old->next=temp->next;
free(temp);
distArray[hashFunction(dis t)]=old->n ext;
return;
}
}
else
{
old=temp;
temp=temp->next;
}
}
printf("\nSchool named %s not found\n", findName);
}
Thanks.
delete(struct node *q,char* findName,int dist)
{
struct node *old, *temp;
temp=q;
while(temp!=NULL)
{
if(strcmp(temp->name,findN
{
if(temp==q)
{
q=temp->next;
free(temp);
distArray[hashFunction(dis
return;
}
else
{
old->next=temp->next;
free(temp);
distArray[hashFunction(dis
return;
}
}
else
{
old=temp;
temp=temp->next;
}
}
printf("\nSchool named %s not found\n", findName);
}
Thanks.
Hi,
you need to change the distArray value only for the first node.
This is what i did:
void delete(struct node *q,char* findName,int dno)
{
struct node *old, *temp;
temp=q;
while(temp!=NULL)
{
if(strcmp(temp->name,findN ame)==0)
{
if(temp==q)
{
q=temp->next;
free(temp);
distArray[dno]=q; //set distArray to point to next node
return;
}
else
{
old->next=temp->next;
free(temp);
return;
}
}
else
{
old=temp;
temp=temp->next;
}
}
printf("\nSchool named %s not found\n", findName);
}
and call it like:
delete(distArray[hashFunct ion(distri ctNo)],sna me,hashFun ction(dist rictNo));
you need to change the distArray value only for the first node.
This is what i did:
void delete(struct node *q,char* findName,int dno)
{
struct node *old, *temp;
temp=q;
while(temp!=NULL)
{
if(strcmp(temp->name,findN
{
if(temp==q)
{
q=temp->next;
free(temp);
distArray[dno]=q; //set distArray to point to next node
return;
}
else
{
old->next=temp->next;
free(temp);
return;
}
}
else
{
old=temp;
temp=temp->next;
}
}
printf("\nSchool named %s not found\n", findName);
}
and call it like:
delete(distArray[hashFunct
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Doing the same thing your way:
if(strcmp(temp->name,findN ame)==0)
{
if(temp==q)
{
q=temp->next;
free(temp);
distArray[hashFunction(dis t)]=q;
return;
}
else
{
old->next=temp->next;
free(temp);
// distArray[hashFunction(dis t)]=old->n ext;
//You dont need this so remove it
return;
}
Here,you'd call with:
delete(distArray[hashFunct ion(distri ctNo)],sna me,distric tNo);
if(strcmp(temp->name,findN
{
if(temp==q)
{
q=temp->next;
free(temp);
distArray[hashFunction(dis
return;
}
else
{
old->next=temp->next;
free(temp);
// distArray[hashFunction(dis
//You dont need this so remove it
return;
}
Here,you'd call with:
delete(distArray[hashFunct
ASKER
>>and call it like:
delete(distArray[hashFunct ion(distri ctNo)],sna me,hashFun ction(dist rictNo));
I am actualy passing the districtNo directly to delete() and then within delete() I am calling hashFucntion(). Either way it should be ok right?
delete(distArray[hashFunct
I am actualy passing the districtNo directly to delete() and then within delete() I am calling hashFucntion(). Either way it should be ok right?
Yeah.
See my last post.
See my last post.
ASKER
Ok so thats it and I am done. Now I am gonna install NFS underground and have some break. Thanks a lot ankuratvb.
ASKER
I am going to request in CS to delete all solid code in both the discussions. If you have any objections let me know. Thanks.
No objections as such but why do you want to do that.
ASKER
Actualy to go along with the rules of my course. I am not supposed to paste any part of the code of my hw on any forums. Thanks.
I'm not sure what you're trying to solve here.
Kent