• C

integer from pointer (Part2)

Ok you can look at the history of this question at this pointer http://oldlook.experts-exchange.com:8080/Programming/Programming_Languages/C/Q_20974846.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.
LVL 5
b_vishwajitAsked:
Who is Participating?
 
ankuratvbConnect With a Mentor Commented:
Because you need to change distArray only when the first node is deleted.
In other cases,the start node remains the same so no changes to the distArray.

I've tested your program and its working fine now.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi b_vishwajit,

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



Kent
0
 
b_vishwajitAuthor Commented:
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.
0
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

 
b_vishwajitAuthor Commented:
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.
0
 
ankuratvbCommented:
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.
0
 
b_vishwajitAuthor Commented:
In an array elements are stored from 0 right? That is they are indexed starting from 0.
0
 
b_vishwajitAuthor Commented:
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.
0
 
ankuratvbCommented:
Yeah.
0
 
b_vishwajitAuthor Commented:
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.
0
 
b_vishwajitAuthor Commented:
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.
0
 
b_vishwajitAuthor Commented:
I see that everything is working properly. Did you find any errors.
0
 
ankuratvbCommented:
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


0
 
ankuratvbCommented:
Also,in your scanf after an invalid selection,you've missed out the &.
while(choice!=1.......
{
scanf("%d",&choice);
                   ^
}
0
 
ankuratvbCommented:
>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
0
 
b_vishwajitAuthor Commented:
Ok I am fixing them now. Just stay here. Thanks.
0
 
b_vishwajitAuthor Commented:
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.
0
 
ankuratvbCommented:
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.
0
 
ankuratvbCommented:
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.

0
 
ankuratvbCommented:
I dont see any changes in your delete function.
0
 
b_vishwajitAuthor Commented:
I have almost lost my patience. But I know what changes I have to make in delete. I will work on that now. Thanks.
0
 
b_vishwajitAuthor Commented:
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.
0
 
ankuratvbCommented:
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));

0
 
ankuratvbCommented:
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);

0
 
b_vishwajitAuthor Commented:
>>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?
0
 
ankuratvbCommented:
Yeah.
See my last post.
0
 
b_vishwajitAuthor Commented:
Ok so thats it and I am done. Now I am gonna install NFS underground and have some break. Thanks a lot ankuratvb.
0
 
b_vishwajitAuthor Commented:
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.
0
 
ankuratvbCommented:
No objections as such but why do you want to do that.

0
 
b_vishwajitAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.