Thursday 15 April 2010

Reflect re-sizing of hash table to main in C -



Reflect re-sizing of hash table to main in C -

i have created new, larger hash table re-hashes info current table , inserts new one. however, turns out alter want create beingness reflected locally, not beingness passed main. @ bottom of snippet below, comment out line **listarray=**listarray2; because don't think that's want happening. know rest of code working should because in insert() function, different array positions storing same values.

my question: how can listarray2 overwrite listarray, reflected in main default table?

void resizetable (node **listarray, int *size) { //listarray array of node*, declared in main listarray2 //size 8, should reflect alter 11 in main int newsize= 11; node *temp; //iterating pointer node **listarray2= malloc (sizeof (node*) * newsize); //create space new array of node*'s for(i=0;i<newsize;i++) listarray2[i]=null; //set within null //go through original listarray (of size 8) for(i=0;i<(*size);i++) { if (listarray[i]==null) //ignore empty array positions continue; temp=listarray[i]; //temp points node not null while(temp!=null) //until end of list in particular array { //arg1 new list,arg2 new size 11, arg3 info to-be-old hash table insert(&*listarray2,newsize,temp->data); temp=temp->next; } } //free(listarray); //**listarray=**listarray2; *size = newsize; }//end resize()

renaming of variables clarity, this:

#include "assert.h" void resizetable (node ***p_listarray, int *p_size) { //listarray array of node*, declared in main newlistarray //size 8, should reflect alter 11 in main node **newlistarray; int newsize= 11; node **oldlistarray; int oldsize; int i; assert(p_listarray != null); /* http://en.wikipedia.org/wiki/assert.h */ assert(p_size != null); /* http://en.wikipedia.org/wiki/assert.h */ oldlistarray = *p_listarray; oldsize = *p_size; newlistarray = malloc (sizeof (node*) * newsize); //create space new array of node*'s for(i=0;i<newsize;i++) newlistarray[i]=null; //set within null //go through original listarray (of size 8) for(i=0;i<oldsize;i++) { node *temp; //iterating pointer if (oldlistarray[i]==null) //ignore empty array positions continue; temp=oldlistarray[i]; //temp points node not null while(temp!=null) //until end of list in particular array { //arg1 new list,arg2 new size 11, arg3 info to-be-old hash table insert(&*newlistarray,newsize,temp->data); temp=temp->next; } } *p_listarray = newlistarray; *p_size = newsize; free(oldlistarray); }//end resize()

and phone call like

node **listarray; int size; // initialize hash table, , when needs resizing... resizetable(&listarray, &size);

c hash

No comments:

Post a Comment