Link to home
Start Free TrialLog in
Avatar of b_vishwajit
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.
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi b_vishwajit,

I'm not sure what you're trying to solve here.



Kent
Avatar of b_vishwajit
b_vishwajit

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.
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_DISTRICTS)
                        {
                                if(keyArray[pos]==0)
                                {
                                       keyArray[pos]=districtNo;
                                }
                                else
                                {
                                       while(keyArray[pos]!=0&&pos<=MAX_DISTRICTS)
                                       {
                                              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_DISTRICTS.
If there are any problems with this code snippet then please mention it here. Thanks.
A possible problem:

                                   while(keyArray[pos]!=0&&pos<=MAX_DISTRICTS)
                                      {
                                             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.
In an array elements are stored from 0 right? That is they are indexed starting from 0.
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.
Yeah.
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.
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.
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


Also,in your scanf after an invalid selection,you've missed out the &.
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
Ok I am fixing them now. Just stay here. Thanks.
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.
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.
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.

I dont see any changes in your delete function.
I have almost lost my patience. But I know what changes I have to make in delete. I will work on that now. Thanks.
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,findName)==0)
        {
                if(temp==q)
                {
                      q=temp->next;
                      free(temp);
                      distArray[hashFunction(dist)]=q;
                      return;
                }
               
                else
                {
                      old->next=temp->next;
                      free(temp);
                      distArray[hashFunction(dist)]=old->next;
                      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,findName)==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[hashFunction(districtNo)],sname,hashFunction(districtNo));

ASKER CERTIFIED SOLUTION
Avatar of ankuratvb
ankuratvb
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Doing the same thing your way:

       if(strcmp(temp->name,findName)==0)
       {
               if(temp==q)
               {
                     q=temp->next;
                     free(temp);
                     distArray[hashFunction(dist)]=q;
                     return;
               }
               
               else
               {
                     old->next=temp->next;
                     free(temp);
//                     distArray[hashFunction(dist)]=old->next;
//You dont need this so remove it
                     return;
               }

Here,you'd call with:
delete(distArray[hashFunction(districtNo)],sname,districtNo);

>>and call it like:
delete(distArray[hashFunction(districtNo)],sname,hashFunction(districtNo));

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.
Ok so thats it and I am done. Now I am gonna install NFS underground and have some break. Thanks a lot ankuratvb.
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.

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.