Like Tree1Likes
  • 1 Post By Salem

hashing problem

This is a discussion on hashing problem within the C Programming forums, part of the General Programming Boards category; I'm having problems with hashing. Don't mind the rehashing. Don't mind the "2" in hashfile.txt. a[m] values start with array[1] ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    9

    hashing problem

    I'm having problems with hashing. Don't mind the rehashing. Don't mind the "2" in hashfile.txt. a[m] values start with array[1] which is 13.
    My problem is that [13]->13, -721924 it prints unnecessary numbers and does segmentation fault afterwards. it should only be [13]->13 and the array is upto [16]->.

    hash.c
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    typedef struct Node
    {
       int x;
       struct Node *next;
    }node;
    
    
    void display(node *a[], int m){
    	int i;
    	for(i=0; i<m; i++){
    	printf("[%d]->", i);
    	while(a[i]!=NULL){
    			printf("%d, ", a[i]->x);
    			a[i]=a[i]->next;
    		}
    	printf("\n");
    	}
    }
    
    
    void open(node *a[], int m, int array[]){
    	int i, index;
    	
    	for(i=0; i<m; i++)
    		a[i]==NULL;
    
    
    	for(i=0; i<array[1]; i++){
    			index= array[1+i+1]%m;
    			node *temp=malloc(sizeof(node ));
    				temp->x = array[1+i+1];
    				node *temp2;
    				temp2= a[index];
    				temp->next = NULL;
    			a[index] = temp;
    			a[index]->next = temp2;
    		}
    		display(a, m);	
    }
    
    
    void linear(node *a[], int m, int array[]){
    	int i, index;
    	
    	for(i=0; i<m; i++)
    		a[i]==NULL;
    	
    		for(i=0; i<array[1]; i++){
    			int j=0;
    			do{
    				index= ((array[1+i+1]%m)+j)%m; 
    				if(a[index]!=NULL)
    					j++;
    			}while(a[index]!=NULL);
    
    
    			j=0;
    
    
    			node *temp=malloc(sizeof(node ));
    				temp->x = array[1+i+1];
    				node *temp2;
    				temp2= a[index];
    				temp->next = NULL;
    			a[index] = temp;
    			a[index]->next = temp2;
    		}
    		display(a, m);
    }
    
    
    void quadratic(node *a[], int m, int array[]){
    	int i, index;
    	
    	for(i=0; i<m; i++)
    		a[i]==NULL;
    	
    		for(i=0; i<array[1]; i++){
    			int j=0;
    			do{
    				index= ((array[1+i+1]%m)+(j*j))%m; 
    				if(a[index]!=NULL)
    					j++;
    			}while(a[index]!=NULL);
    			
    			j=0;
    			
    			node *temp=malloc(sizeof(node ));
    				temp->x = array[1+i+1];
    				node *temp2;
    				temp2= a[index];
    				temp->next = NULL;
    			a[index] = temp;
    			a[index]->next = temp2;
    		}
    		display(a, m);
    }
    
    void double(node *a[], int m, int array[]){
    int index;
    	
    	for(i=0; i<array[0]; i++)
    		a[i]==NULL;
    	
    		for(i=0; i<array[1]; i++){
    			int j=0;
    			do{
    				index= ((array[1+i+1]%m)+(j*(5-(array[1+i+1]%5))))%m;
    				if(a[index]!=NULL)
    					j++;
    			}while(a[index]!=NULL);
    			
    			j=0;
    			
    			node *temp=malloc(sizeof(node ));
    				temp->x = array[1+i+1];
    				node *temp2;
    				temp2= a[index];
    				temp->next = NULL;
    			a[index] = temp;
    			a[index]->next = temp2;
    		}
    		display(a, m);
    }
    
    
    main() {
    FILE *fp = fopen("hashfile.txt", "r");
        int array[100];
        int i;
        
        for (i = 0; i < 100; i++) {
            fscanf(fp, "%d", &array[i]);
        }
        fclose(fp);
    	
    	int m;
    	m= (array[1]*2)+1;
    	node **a=malloc(sizeof(node *)*m);
    	
    	printf("Open Hashing\n");
    	open(a, m, array);
    	
    	printf("Closed Hashing\n");
    	printf("(Linear Probing)\n");
    	linear(a, m, array);
    	printf("(Quadratic Probing)\n");
    	quadratic(a, m, array);
    	printf("(Double Hashing)\n");
    	double(a, m, array);	
    }
    hashfile.txt
    2
    8
    13
    21
    23
    15
    6
    1
    3
    56

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > a[i]==NULL;
    All these attempts to initialise the array to NULL pointers fail.

    You used == where you should have used =

    What warnings did you get from the compiler?
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘open’:
    foo.c:29:9: warning: statement with no effect [-Wunused-value]
    foo.c: In function ‘linear’:
    foo.c:50:9: warning: statement with no effect [-Wunused-value]
    foo.c: In function ‘quadratic’:
    foo.c:80:9: warning: statement with no effect [-Wunused-value]
    foo.c: At top level:
    foo.c:103:6: error: two or more data types in declaration specifiers
    foo.c:103:18: error: expected ‘)’ before ‘*’ token
    foo.c:131:1: warning: return type defaults to ‘int’ [-Wreturn-type]
    foo.c: In function ‘main’:
    foo.c:154:13: error: expected ‘)’ before ‘,’ token
    foo.c:155:1: warning: control reaches end of non-void function [-Wreturn-type]
    Do you see those "statement with no effect"?

    Also, double is a crap choice for a function name, even if your compiler manages to accept it.
    iMalc likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help on hashing please.
    By MyRedz in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2010, 07:22 AM
  2. hashing problem
    By kopros in forum C Programming
    Replies: 6
    Last Post: 12-15-2006, 05:39 PM
  3. hashing help
    By alokin in forum C Programming
    Replies: 17
    Last Post: 10-28-2002, 05:33 PM
  4. hashing
    By sweets in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2002, 04:22 AM
  5. hashing problem
    By dante in forum C Programming
    Replies: 0
    Last Post: 04-14-2002, 07:00 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21