Problem with hash tables

I have problem with this code down here. Basically, when I hash a list, the members of the said hash table do not show when I try to print them, as if the list was not hashed properly.

Thanks for the quick response.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  #include <iostream>
using namespace std;

struct CvorListe
{
	int broj;
	CvorListe*sljedeci;
	CvorListe(){broj=0; sljedeci=NULL;}
};

struct Lista
{
	int brElemenata;
	CvorListe*glava;
	Lista(){brElemenata=0; glava=NULL;}
};

void dodajElement (Lista&l, CvorListe&c)
{
	if (l.glava==NULL)
	{
		l.glava=&c;
		l.brElemenata++;
	}
	
	else
	{
		CvorListe*tmp=l.glava;
		while (tmp->sljedeci!=NULL)
		{
			tmp=tmp->sljedeci;
		}
		tmp->sljedeci=&c;
		l.brElemenata++;
	}
}

struct HashSlot
{
	int kljuc;
	int*vrijednost;
	HashSlot(){kljuc=0; vrijednost=NULL;}
};

void HashFunkcija (int*kljucevi, int d, HashSlot*tabela, int n)
{
	if (n>=d)
	{
		for (int i=0; i<d; i++)
		{
			int h=kljucevi[i]%n;
			if (tabela[h].vrijednost==NULL)
			{
				tabela[h].kljuc=kljucevi[i];
				tabela[h].vrijednost=&kljucevi[i];
			}
			
			else
			{
				bool pronaden=false;
				for (int j=h+1; j<n; j++)
				{
					if (tabela[j].vrijednost==NULL)
					{
						tabela[j].kljuc=kljucevi[i];
						tabela[j].vrijednost=&kljucevi[i];
						pronaden=true;
						break;
					}
				}
				if (pronaden==false)
				{
					for (int j=0; j<h; j++)
					{
						if (tabela[j].vrijednost==NULL)
						{
							tabela[j].kljuc=kljucevi[i];
							tabela[j].vrijednost=&kljucevi[i];
							break;
						}
					}
				}	
			}
		}
	}
}

int*pretragaHashTabele (int kljuc, HashSlot*tabela, int n)
{
	int h=kljuc%n;
	if(tabela[h].kljuc==kljuc && tabela[h].vrijednost!=NULL)
	{
		return tabela[h].vrijednost;
	}
	
	else if(tabela[h].vrijednost==NULL)
	{
		return NULL;
	}
	
	else
	{
		bool pronaden=false;
		for (int k=h+1; k<n; k++)
		{
			if (tabela[k].vrijednost==NULL)
			{
				return NULL;
			}
			else if (tabela[k].kljuc==kljuc && tabela[k].vrijednost!=NULL)
			{
				pronaden=true;
				return tabela[k].vrijednost;
			}
		}
		
		if (pronaden=false)
		{
			for (int k=0; k<h; k++)
			{
				if (tabela[k].kljuc==kljuc && tabela[k].vrijednost!=NULL)
				{
					return tabela[k].vrijednost;
				}
				
				else if (tabela[k].vrijednost==NULL)
				{
					return NULL;
				}
			}
		}
		
	}
}

void ispisTabele  (HashSlot*tabela, int n)
{
	for(int i=0; i<n; i++)
	{
		cout<<i<<". "<<"    "<<tabela[i].kljuc<<endl;
	}
}


int main()
{
	Lista l;
	int a=1;
	while (a!=0)
	{
		int *kljucevi= new int [l.brElemenata];
		HashSlot*tabela=new HashSlot[l.brElemenata+l.brElemenata];
		cout<<"Chose the option that you want"<<endl;
		cout<<"Press 0 to close the program"<<endl<<endl;
		cout<<"1. Add a number to the list."<<endl;
		cout<<"2. Hash the list."<<endl;
		cout<<"3. Print hash table."<<endl;
		cout<<"4. Search hash table."<<endl;
		cout<<endl;
		cin>>a;
		if (a==1)
		{
			cout<<"Add a number to the list"<<endl;
			CvorListe*c=new CvorListe;
			cin>>c->broj;
			dodajElement (l, *c);
		}
	
		else if (a==2)
		{
			CvorListe*tmp=l.glava;
			for(int i=0; i<l.brElemenata; i++)
			{
				kljucevi[i]=tmp->broj;
				tmp=tmp->sljedeci;
			}
			for (int i=0; i<l.brElemenata; i++)
			{
				cout<<kljucevi[i]<<endl;
			}
			HashFunkcija (kljucevi, l.brElemenata, tabela, l.brElemenata+l.brElemenata);
		}
		
		else if (a==3)
		{
			cout<<"Printing the hash table"<<endl;
			ispisTabele (tabela, l.brElemenata+l.brElemenata);
			cout<<endl<<endl;
		}
		
		else if (a==4)
		{
			cout<<"Press the number that you want to find."<<endl;
			int kljuc;
			cin>>kljuc;
			int*p=pretragaHashTabele(kljuc, tabela, l.brElemenata+l.brElemenata);
			if (p!=NULL)
			{
				cout<<"Number "<<*p<<" is in the table"<<endl;
			}
			else
			{
				cout<<"The number is not located in the table"<<endl;
			}
		}
		else if (a<0 || a>4)
		{
			cout<<"It has to be a number between 0-4."<<endl<<endl;
		}
	}
	return 0;
}
Last edited on
I am sorry if I am bothering, but, please if someone could help me and tell me where is the problem in the code.
This is rather dificult to read with this foreign words.

I can see one problem on line 114: if (pronaden=false) // Note: = <- assignment

Better write it so: if (!pronaden) // Note: !
Yeah, sorry. I tried already that. Here is the thing:
without the menu it works like a charm, but with it is utter garbage.
Topic archived. No new replies allowed.