|
-
September 15th, 2011, 09:57 AM
#1
Problem running hashtable
Hi, am creating this hashtable, but am getting a core dumped and I think it's because am not creating my table correctly. Can somebody please point me to the right direction. Here is my code
Code:
#include <iostream>
#include <string>
#include <fstream>
//#include <stdio.h>
//#include <ctype.h>
const int h_size = 10000000;
struct town
{
public:
//They are all string types because all the info being collected is just for display purpose.
string name;
string population;
string housing;
string lan_area;
string water_area;
string square_land;
string square_water;
string x_cord;
string y_cord;
town(string a, string b, string c,string d, string e, string f, string g, string h, string i)
{
name = a;
population = b;
housing = c;
lan_area = d;
water_area = e;
square_land = f;
square_water = g;
x_cord = h;
y_cord = i;
}
};
string erasestring(string x, int size)
{
int count = 0;
string temp;
for(int i = 0; i < size-1; i++)
{
if(x[i] == ' ')
count++;
}
x.erase(0,count);
temp = x;
return temp;;
}
class hashtable
{
public:
int data_item;
int hash_size;
class hashlink
{
public:
town * data;
hashlink * next;
hashlink(town * t, hashlink * n = NULL);
};
hashlink * * table;
int hash(string s);
public:
hashtable( int init_size = h_size);
void add(town * t);
};
hashtable::hashtable(int init_size)
{
hash_size = init_size;
table = new hashlink * [init_size];
for (int i = 0; i < init_size; i++)
table[i] = NULL;
}
void readfile()
{
hashtable * table;
string mainline;
string line1;
string line2;
string line3;
string line4;
string line5;
string line6;
string line7;
string line8;
string line9;
int count = 0;
string temp;
ifstream getf("alphaplaces.txt");
//while(getf.fail() != true)
// {
getline(getf,mainline);
line1 = mainline.substr(0,38);
line2 = erasestring(mainline.substr(70,13),13);
line3 = erasestring(mainline.substr(82,9),9);
line4 = erasestring(mainline.substr(91,14),14);
line5 = erasestring(mainline.substr(105,14),14);
line6 = erasestring(mainline.substr(119,12),12);
line7 = erasestring(mainline.substr(131,12),12);
line8 = erasestring(mainline.substr(144,9),9);
line9 = erasestring(mainline.substr(153,11),11);
//cout << "\n" << pos << "\n";
town * x = new town(mainline.substr(0,38),line2,line3,line4,line5,line6,line7,line8,line9);
table->add(x);
// }
}
void hashtable::add(town * t)
{
int pos = hash(t->name);
cout << endl;
hashlink * p = new hashlink(t,NULL);
cout << "hash# " << pos << endl;
cout <<"hash size " << hashtable::hash_size << endl;
if(hashtable::table[pos] == NULL)
{
hashtable::table[pos] = p;
cout << "testing";
}
//cout << table[pos]->name << endl;
//cout << table[pos]->population << endl;
}
int hashtable::hash(string s)
{
unsigned int h = 982322;
for (int i=0; i<s.length(); i+=1)
h = h * 97 + s[i];
return h % h_size;
}
hashtable::hashlink::hashlink(town * t,hashtable::hashlink * n)
{
data = t;
next = n;
}
void main()
{
//hashtable * maintable = NULL;
readfile();
return(1);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|