asked    Blithe     2018-10-22       c       68 view        1 Answer

[SOLVED] how to update table size inside hash function

I was learning how to implement Hash table but i am bit confused here in because in the book below code was available and i understood well enough the code, but inside book there was no definition of HASH function ,i know we have to define it by own ,but according the below code that was given give inside book HASH is taking two arguments wherever i used the HASH like in HashInsert it is taking two arguments index=HASH(data,t->size) if we assume that return type of HASH as int now for eg we can define HASH as

int HASH(int  data,int tsize){
return(data%7);
}

but according to my program how should i update t->size(table size) inside HASH function or how should i use that Please help me in proper implementation of the above HASH function

#define Load_factor 20
#include<stdio.h>
#include<stdlib.h>
struct Listnode{
 int key;
 int data;
 struct Listnode* next;
};
struct HashTableNode{
 int bcount;          /// Number of elements in block
 struct Listnode* next;
 };
struct HashTable{
 int tsize;          /// Table size
 int count;
 struct HashTableNode** Table;
};
struct HashTable* createHashTable(int size){
 struct HashTable* h;
 h=(struct HashTable*)malloc(sizeof(struct HashTable));
 h->tsize=size/Load_factor;
 h->count=0;

 h->Table=(struct HashTableNode**)malloc(sizeof(struct HashTableNode*)*h->tsize);
 if(!h->Table){
 printf("Memory Error");
  return NULL;
 }
 for(int i=0;i<h->tsize;i++){
 h->Table[i]->bcount=0;
 h->Table[i]->next=NULL;
 }
   return h;
 }

/// Hashsearch
int HashSearch(struct HashTable* h,int data){
  struct Listnode* temp;
  temp=h->Table[HASH(data,h->tsize)]->next;
  while(temp)     ///same as temp!=NULL
  {
   if(temp->data==data)
      return 1;
    temp=temp->next;
  }
    return 0;

}

int HashDelete(struct HashTable* h,int  data)
{
 int index;
 struct Listnode *temp,*prev;
 index=HASH(data,h->tsize);
 for(temp=h->Table[index]->next,prev=NULL;temp;prev=temp,temp=temp->next)
 {
    if(temp->data==data)
    {
        if(prev!=NULL)
             prev->next=temp->next;
         free(temp);
         h->Table[index]->bcount--;
         h->count--;
         return 1;
    }
 }

 return 0;

}
int HashInsert(struct HashTable *h ,int data){
 int index;
 struct Listnode* temp,*newnode;
 if(HashSearch(h,data))
    return 0;
 index = HASH(data,h->tsize);
 temp=h->Table[index]->next;
 newnode=(struct Listnode*)malloc(sizeof(struct Listnode));
 if(!newnode)
    return -1;
 newnode->key=index;
 newnode->data;
 newnode->next=h->Table[index]->next;
 h->Table[index]->next=newnode;
 h->Table[index]->bcount++;
 h->count++;
   return 1;
}

i am just learning implementation of hashing so main is looking quiet weird

int main(){
  return 0;
}

  1 Answer  

        answered    Isabel     2018-10-22      

You shouldn't! I mean you shouldn't modify it.

Instead the function gets the size of the hash-table (the number of "buckets") so it can use it to create a bucket index from the hash value. This is typically done through modulo %.

So instead of a fixed magic number 7 you modulo with the size:

return(data%tsize);




Your Answer





 2018-10-22         Ives

Azure Active Directory with Springboot : how to handle expired oauth token?

I have a springboot app which use Microsoft Azure active directory to allow authentication (oauth2).I have followed the "how to" provided by Microsoft (https://docs.microsoft.com/en-us/java/azure/spring-framework/configure-spring-boot-starter-java-app-with-azure-active-directory?view=azure-java-stable).Everything is working well except that I have no ideas on how to handle expired token (after 1 hour) in a way that it will not affect the users.I know that it is possible to get new token with the refresh token but if I take a look in NimbusAuthorizationCodeTokenResponseClien...
 spring-boot                     1 answers                     49 view
 2018-10-22         Earl

azure active directory + spring boot

I am trying to run azure-active-directory-spring-boot-backend-sample but it is not working. After opening localhost:8080 it prompts with localhost:8080/login page and Azure link. After clicking on it it goes to Azure Active Directory page. After successful login it comes back to /login page rather than /welcome page. I tried using other example as well available on internet but nothing seems to be working. Please advise. You need to register your application under the Azure Active Directory where your users are stored. Then enter the published homepage URL in the Reply U...
 spring-boot                     1 answers                     50 view
 2018-10-22         Vanessa

Developing UCWA applications for Skype for Business Online

I'm trying to develop a simple java client that needs to access Skype for Business APIs in order to setup an online meeting (i.e. I need to retrieve a simple setup meeting URL by invoking an UCWA API).I followed this Microsoft tutorial step-by-step:https://docs.microsoft.com/en-us/skype-sdk/ucwa/developingucwaapplicationsforsfbonlineI have an Office 365 Business Premium license and I configured a custom domain (correctly registered and added at the zone DNS file);I configured and registered my java client app on Azure (taking care to assign required delegated permission for...
 java                     1 answers                     51 view