Skip to main content
deleted 25 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

hashing Hashing a struct in C

I'm trying to learn how chained hashing works, but can't get any further than this. I think hashing works, but after finishing this little program, someone told me that what I have created so far has nothing to do with hashing. :/

Here is my code:

hashing a struct in C

I'm trying to learn how chained hashing works, but can't get any further than this. I think hashing works, but after finishing this little program, someone told me that what I have created so far has nothing to do with hashing. :/

Here is my code:

Hashing a struct in C

I'm trying to learn how chained hashing works, but can't get any further than this. I think hashing works, but after finishing this little program, someone told me that what I have created so far has nothing to do with hashing.

fixed code formatting
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 5
//This is my struct I wanted to add to the hashtable
struct customer{
    char name[20];          
    int id;             
    int pin;            
    double money;           
    struct customer *next;      
};

//creating the table
struct customer *hash_table[size];                                                                    
struct customer *next=NULL;
//my hashfunction
int hash_function(int id){
    unsigned int adress_hash;               
    adress_hash = 31 * id;          
    return adress_hash % 5; 
}

 

//Adding the cust elements to my table
void add_customer(){
 
    int id_set = rand() % 8999 + 1000;
    struct customer *start;
    struct customer *pointer; 
   
  int adress = hash_function(id_set); 

     //first element

    if(hash_table[adress]==NULL)
    {
        hash_table[adress] = malloc(sizeof(struct customer));
        hash_table[adress]->id= id_set;
        hash_table[adress]->pin= rand() % 8999 + 1000;

        hash_table[adress]->money= 0;
        hash_table[adress]->next=NULL;
 
    }
  else {
        //Adding new files if the first element of the table is NOT NULL
        pointer=hash_table[adress];
        while(pointer->next != NULL)
        {
            pointer= pointer->next;
        }
 
        pointer->next = malloc(sizeof(struct customer));
        pointer=pointer->next;
        pointer->id=id_set;
        pointer->pin= rand() % 8999 + 1000;
        pointer->money = 0;

        pointer->next=NULL;
 
    }
 }

void list_customer(int a){
    struct customer *pointer;
    int x;
    for(x=0;x<5;x++){
        pointer = hash_table[x];
        printf("%d. ----------\n", x);
        while(pointer != 0){
            printf("KNR: %d, PIN: %d\n", pointer->id, pointer->pin);
            pointer = pointer->next;
        }
        printf("-------------------------\n");
    }
}

int main(){
    int adr,x;
    srand(time(NULL));
    for(x=0;x<5;x++){
        add_customer();
    }
    list_customer(size);
    getchar();getchar();
    return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 5
//This is my struct I wanted to add to the hashtable
struct customer{
    char name[20];          
    int id;             
    int pin;            
    double money;           
    struct customer *next;      
};

//creating the table
struct customer *hash_table[size];                                                                    
struct customer *next=NULL;
//my hashfunction
int hash_function(int id){
    unsigned int adress_hash;               
    adress_hash = 31 * id;          
    return adress_hash % 5; 
}

 

//Adding the cust elements to my table
void add_customer(){
 
int id_set = rand() % 8999 + 1000;
struct customer *start;
struct customer *pointer;   
 int adress = hash_function(id_set); 

 //first element

if(hash_table[adress]==NULL)
{
    hash_table[adress] = malloc(sizeof(struct customer));
    hash_table[adress]->id= id_set;
    hash_table[adress]->pin= rand() % 8999 + 1000;

    hash_table[adress]->money= 0;
    hash_table[adress]->next=NULL;
 
}
 else{
//Adding new files if the first element of the table is NOT NULL
pointer=hash_table[adress];
while(pointer->next != NULL)
{
    pointer= pointer->next;
}
 
pointer->next = malloc(sizeof(struct customer));
pointer=pointer->next;
pointer->id=id_set;
pointer->pin= rand() % 8999 + 1000;
pointer->money = 0;

pointer->next=NULL;
 
    }
 }

void list_customer(int a){
struct customer *pointer;
int x;
for(x=0;x<5;x++){
pointer = hash_table[x];
printf("%d. ----------\n", x);
while(pointer != 0){
    printf("KNR: %d, PIN: %d\n", pointer->id, pointer->pin);
    pointer = pointer->next;
}
printf("-------------------------\n");
}
}

int main(){
int adr,x;
srand(time(NULL));
for(x=0;x<5;x++){
add_customer();
}
list_customer(size);
getchar();getchar();
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 5
//This is my struct I wanted to add to the hashtable
struct customer{
    char name[20];
    int id;
    int pin;
    double money;
    struct customer *next;
};

//creating the table
struct customer *hash_table[size];
struct customer *next=NULL;
//my hashfunction
int hash_function(int id){
    unsigned int adress_hash;
    adress_hash = 31 * id;
    return adress_hash % 5;
}

//Adding the cust elements to my table
void add_customer(){
    int id_set = rand() % 8999 + 1000;
    struct customer *start;
    struct customer *pointer; 
    int adress = hash_function(id_set);

     //first element

    if(hash_table[adress]==NULL)
    {
        hash_table[adress] = malloc(sizeof(struct customer));
        hash_table[adress]->id= id_set;
        hash_table[adress]->pin= rand() % 8999 + 1000;

        hash_table[adress]->money= 0;
        hash_table[adress]->next=NULL;
    } else {
        //Adding new files if the first element of the table is NOT NULL
        pointer=hash_table[adress];
        while(pointer->next != NULL)
        {
            pointer= pointer->next;
        }
        pointer->next = malloc(sizeof(struct customer));
        pointer=pointer->next;
        pointer->id=id_set;
        pointer->pin= rand() % 8999 + 1000;
        pointer->money = 0;

        pointer->next=NULL;
    }
}

void list_customer(int a){
    struct customer *pointer;
    int x;
    for(x=0;x<5;x++){
        pointer = hash_table[x];
        printf("%d. ----------\n", x);
        while(pointer != 0){
            printf("KNR: %d, PIN: %d\n", pointer->id, pointer->pin);
            pointer = pointer->next;
        }
        printf("-------------------------\n");
    }
}

int main(){
    int adr,x;
    srand(time(NULL));
    for(x=0;x<5;x++){
        add_customer();
    }
    list_customer(size);
    getchar();getchar();
    return 0;
}
added 487 characters in body
Source Link
Twin322
  • 51
  • 1
  • 3
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define size 5
//This is my struct I wanted to add to the hashtable
struct customer{
    char name[20];          
    int id;             
    int pin;            
    double money;           
    intstruct try;customer *next;      
};

//creating the table
struct customer *hash_table[size];        
     struct customer *next;      
};

//creating the table
struct customer *hash_table[30];                                            
struct customer *next=NULL;
//my hashfunction
int hash_function(int id){
    unsigned int adress_hash;               
    adress_hash = 31 * id;          
    return adress_hash % 2;5; 
} 



//Adding the cust elements to my table
void add_customer(){        
     
int id_set = rand() % 8999 + 1000;
    struct customer *start;
    struct customer *pointer;   
    int adress = hash_function(idid_set); 

 //first element
     
if(hash_table[adress]==NULL)
    {
        hash_table[adress] = malloc(sizeof(struct Kundecustomer));
        hash_table[adress]->id= id_set;
        hash_table[adress]->pin= rand() % 8999 + 1000;
        hash_table[adress]->try=3;
        hash_table[adress]->money= 0;
        hash_table[adress]->next=NULL;
     
}
    else
    {
        //Adding new files if the first element of the table is NOT NULL
        element=hash_table[adress];pointer=hash_table[adress];
        while(pointer->next != NULL)
        {
            pointer= pointer->next;
        }

        pointer->next = malloc(sizeof(struct Kundecustomer));
        pointer=elementpointer=pointer->next;
        pointer->id=id_set;
        pointer->pin= rand() % 8999 + 1000;
pointer->money = 0;

pointer->next=NULL;

    }
 }

void list_customer(int a){
struct customer *pointer;
int x;
for(x=0;x<5;x++){
pointer = hash_table[x];
printf("%d. ->money---------\n", x);
while(pointer != 0;0){
    printf("KNR: %d, PIN: %d\n", pointer->try=>id, 3;pointer->pin);
      pointer = pointer->next=NULL;>next;
  }
printf("-------------------------\n");
}
}

int main(){
int adr,x;
srand(time(NULL));
for(x=0;x<5;x++){
add_customer();
}
list_customer(size);
getchar();getchar();
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

//This is my struct I wanted to add to the hashtable
struct customer{
    char name[20];          
    int id;             
    int pin;            
    double money;           
    int try;            
     struct customer *next;      
};

//creating the table
struct customer *hash_table[30];
struct customer *next=NULL;
//my hashfunction
int hash_function(int id){
    unsigned int adress_hash;               
    adress_hash = 31 * id;          
    return adress_hash % 2; 
}

//Adding the cust elements to my table
void add_customer(){        
    int id_set = rand() % 8999 + 1000;
    struct customer *start;
    struct customer *pointer;   
    int adress = hash_function(id); 

 //first element
    if(hash_table[adress]==NULL)
    {
        hash_table[adress] = malloc(sizeof(struct Kunde));
        hash_table[adress]->id= id_set;
        hash_table[adress]->pin= rand() % 8999 + 1000;
        hash_table[adress]->try=3;
        hash_table[adress]->money= 0;
        hash_table[adress]->next=NULL;
    }
    else
    {
        //Adding new files if the first element of the table is NOT NULL
        element=hash_table[adress];
        while(pointer->next != NULL)
        {
            pointer= pointer->next;
        }

        pointer->next = malloc(sizeof(struct Kunde));
        pointer=element->next;
        pointer->id=id_set;
        pointer->pin= rand() % 8999 + 1000;
        pointer->money = 0;
        pointer->try= 3;
        pointer->next=NULL;
    }
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 5
//This is my struct I wanted to add to the hashtable
struct customer{
    char name[20];          
    int id;             
    int pin;            
    double money;           
    struct customer *next;      
};

//creating the table
struct customer *hash_table[size];                                                                    
struct customer *next=NULL;
//my hashfunction
int hash_function(int id){
    unsigned int adress_hash;               
    adress_hash = 31 * id;          
    return adress_hash % 5; 
} 



//Adding the cust elements to my table
void add_customer(){
 
int id_set = rand() % 8999 + 1000;
struct customer *start;
struct customer *pointer;   
int adress = hash_function(id_set); 

 //first element
 
if(hash_table[adress]==NULL)
{
    hash_table[adress] = malloc(sizeof(struct customer));
    hash_table[adress]->id= id_set;
    hash_table[adress]->pin= rand() % 8999 + 1000;

    hash_table[adress]->money= 0;
    hash_table[adress]->next=NULL;
 
}
else{
//Adding new files if the first element of the table is NOT NULL
pointer=hash_table[adress];
while(pointer->next != NULL)
{
    pointer= pointer->next;
}

pointer->next = malloc(sizeof(struct customer));
pointer=pointer->next;
pointer->id=id_set;
pointer->pin= rand() % 8999 + 1000;
pointer->money = 0;

pointer->next=NULL;

    }
 }

void list_customer(int a){
struct customer *pointer;
int x;
for(x=0;x<5;x++){
pointer = hash_table[x];
printf("%d. ----------\n", x);
while(pointer != 0){
    printf("KNR: %d, PIN: %d\n", pointer->id, pointer->pin);
    pointer = pointer->next;
}
printf("-------------------------\n");
}
}

int main(){
int adr,x;
srand(time(NULL));
for(x=0;x<5;x++){
add_customer();
}
list_customer(size);
getchar();getchar();
return 0;
}
fixed code formatting
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
Loading
added 38 characters in body
Source Link
Twin322
  • 51
  • 1
  • 3
Loading
Source Link
Twin322
  • 51
  • 1
  • 3
Loading